Skip to content

Commit

Permalink
fix: preserve previous condition field value on constraint change (#1969
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sadakchap committed May 12, 2022
1 parent e87bb6f commit f4c3060
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/components/BrowserFilter/BrowserFilter.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ export default class BrowserFilter extends React.Component {
if (Filters.Constraints[filter.get('constraint')].hasOwnProperty('field')) {
type = Filters.Constraints[filter.get('constraint')].field;
}*/

// since we are preserving previous compareTo value
// remove compareTo for constraints which are not comparable
let isComparable = Filters.Constraints[filter.get('constraint')].comparable;
if (!isComparable) {
return filter.delete('compareTo')
}
return filter;
});
this.props.onChange(formatted);
Expand Down
2 changes: 1 addition & 1 deletion src/components/BrowserFilter/FilterRow.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ let FilterRow = ({
color={active ? 'blue' : 'purple'}
value={Constraints[currentConstraint].name}
options={constraints.map((c) => Constraints[c].name)}
onChange={(c) => onChangeConstraint(constraintLookup[c])} />
onChange={(c) => onChangeConstraint(constraintLookup[c], compareTo)} />
{compareValue(compareInfo, compareTo, onChangeCompareTo, active, parentContentId)}
<button type='button' className={styles.remove} onClick={onDeleteRow}><Icon name='minus-solid' width={14} height={14} fill='rgba(0,0,0,0.4)' /></button>
</div>
Expand Down
8 changes: 4 additions & 4 deletions src/components/Filter/Filter.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function changeField(schema, filters, index, newField) {
return filters.set(index, newFilter);
}

function changeConstraint(schema, filters, index, newConstraint) {
function changeConstraint(schema, filters, index, newConstraint, prevCompareTo) {
let field = filters.get(index).get('field');
let compareType = schema[field].type;
if (Object.prototype.hasOwnProperty.call(Filters.Constraints[newConstraint], 'field')) {
Expand All @@ -30,7 +30,7 @@ function changeConstraint(schema, filters, index, newConstraint) {
let newFilter = new Map({
field: field,
constraint: newConstraint,
compareTo: Filters.DefaultComparisons[compareType]
compareTo: prevCompareTo ?? Filters.DefaultComparisons[compareType]
})
return filters.set(index, newFilter);
}
Expand Down Expand Up @@ -110,8 +110,8 @@ let Filter = ({ schema, filters, renderRow, onChange, blacklist, className }) =>
onChangeField: newField => {
onChange(changeField(schema, filters, i, newField));
},
onChangeConstraint: newConstraint => {
onChange(changeConstraint(schema, filters, i, newConstraint));
onChangeConstraint: (newConstraint, prevCompareTo) => {
onChange(changeConstraint(schema, filters, i, newConstraint, prevCompareTo));
},
onChangeCompareTo: newCompare => {
onChange(changeCompareTo(schema, filters, i, compareType, newCompare));
Expand Down

0 comments on commit f4c3060

Please sign in to comment.