Skip to content

Commit 7032db0

Browse files
author
Beth Glenfield
authored
(MAINT) Add onUpdate prop to filter (#344)
1 parent b91d09e commit 7032db0

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

packages/react-components/source/react/library/filters/FilterForm.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const propTypes = {
1313
removable: PropTypes.bool,
1414
onSubmit: PropTypes.func,
1515
onCancel: PropTypes.func,
16+
onUpdate: PropTypes.func,
1617
cancellable: PropTypes.bool,
1718
fields: PropTypes.arrayOf(PropTypes.string),
1819
/** Defaults to the standard set as defined in constants. */
@@ -58,6 +59,7 @@ const defaultStrings = {
5859
const defaultProps = {
5960
onSubmit: () => {},
6061
onCancel: () => {},
62+
onUpdate: filter => filter,
6163
removable: false,
6264
fields: [],
6365
filter: {},
@@ -115,14 +117,16 @@ class FilterForm extends React.Component {
115117
}
116118

117119
onUpdate(field, values) {
118-
const { operators } = this.props;
120+
const { operators, onUpdate } = this.props;
119121
const value = values[field];
120-
const filter = { ...values };
122+
let filter = { ...values };
121123

122124
if (isValueless(value, operators)) {
123125
delete filter.value;
124126
}
125127

128+
filter = onUpdate(filter);
129+
126130
this.setState({ filter });
127131
}
128132

packages/react-components/source/react/library/filters/Filters.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const propTypes = {
1919
),
2020
addCTA: PropTypes.string,
2121
onChange: PropTypes.func,
22+
onUpdate: PropTypes.func,
2223
onSwitchView: PropTypes.func,
2324
removableToggle: PropTypes.bool,
2425
/** Defaults to the standard set as defined in constants. */
@@ -56,6 +57,7 @@ const defaultStrings = {
5657
const defaultProps = {
5758
fields: [],
5859
filters: [],
60+
onUpdate: filter => filter,
5961
onChange: () => {},
6062
addCTA: 'Add filter',
6163
onSwitchView: () => {},
@@ -195,7 +197,14 @@ class Filters extends React.Component {
195197
}
196198

197199
renderForm() {
198-
const { removableToggle, fields, operators, strings, filters } = this.props;
200+
const {
201+
removableToggle,
202+
fields,
203+
operators,
204+
strings,
205+
filters,
206+
onUpdate,
207+
} = this.props;
199208
const { filter } = this.state;
200209
let cancellable = true;
201210

@@ -210,6 +219,7 @@ class Filters extends React.Component {
210219
fields={fields}
211220
filter={filter}
212221
operators={operators}
222+
onUpdate={onUpdate}
213223
onCancel={this.onCancel}
214224
onSubmit={this.onSubmitFilter}
215225
strings={strings}

packages/react-components/test/filters/FilterForm.js

+50
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,54 @@ describe('<FilterForm />', () => {
5050
);
5151
});
5252
});
53+
54+
describe('onUpdate', () => {
55+
it('should default if onUpdate is not supplied', () => {
56+
const fields = ['Name', 'Age'];
57+
const wrapper = mount(<FilterForm fields={fields} />);
58+
59+
expect(wrapper.find('FilterForm').prop('onUpdate')).to.exist;
60+
61+
const field = wrapper.find('FormField[name="field"]');
62+
field.simulate('click');
63+
const value = field.find('FormFieldElement');
64+
value.simulate('click');
65+
const select = value.find('OptionMenuList');
66+
select.simulate('click');
67+
const item = select.find('ForwardRef[id*="Name"]').find('li');
68+
item.simulate('click');
69+
70+
expect(
71+
wrapper.find('form>FormField[name="field"]').prop('value'),
72+
'default onUpdate is called',
73+
).to.eql('Name');
74+
});
75+
76+
it('should override default if onUpdate supplied', () => {
77+
const onUpdate = () => {
78+
return {
79+
field: 'Age',
80+
op: '<',
81+
value: 'Test value',
82+
};
83+
};
84+
const fields = ['Name', 'Age'];
85+
const wrapper = mount(<FilterForm fields={fields} onUpdate={onUpdate} />);
86+
expect(wrapper.find('FilterForm').prop('onUpdate')).to.exist;
87+
88+
const field = wrapper.find('FormField[name="field"]');
89+
field.simulate('click');
90+
const value = field.find('FormFieldElement');
91+
value.simulate('click');
92+
const select = value.find('OptionMenuList');
93+
select.simulate('click');
94+
const item = select.find('ForwardRef[id*="Name"]').find('li');
95+
item.simulate('click');
96+
97+
expect(
98+
wrapper.find('form>FormField[name="field"]').prop('value'),
99+
'custom onUpdate is called',
100+
).to.eql('Age');
101+
});
102+
});
53103
});

0 commit comments

Comments
 (0)