Skip to content

Commit

Permalink
Added filtering undefined values in array of SelectField (fixes #691).
Browse files Browse the repository at this point in the history
  • Loading branch information
kestarumper authored Mar 25, 2020
1 parent 381541f commit 71f8e87
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/uniforms-antd/__tests__/SelectField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,21 @@ test('<SelectField> - renders a select which correctly reacts on change (array)'
expect(onChange).toHaveBeenLastCalledWith('x', ['b']);
});

test('<SelectField> - renders a select (undefined values)', () => {
const element = <SelectField name="x" value={[undefined, 'a', undefined]} />;
const wrapper = mount(
element,
createContext({
x: { type: Array },
'x.$': { type: String, allowedValues: ['a', 'b'] },
}),
);

expect(wrapper.find(Select)).toHaveLength(1);
expect(wrapper.find(Select).prop('value')).not.toContain(undefined);
expect(wrapper.find(Select).prop('value')).toContain('a');
});

test('<SelectField> - renders a select which correctly reacts on change (empty)', () => {
const onChange = jest.fn();

Expand Down
8 changes: 7 additions & 1 deletion packages/uniforms-antd/src/SelectField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ const renderSelect = props => (
onChange={value => props.onChange(value)}
placeholder={props.placeholder}
ref={props.inputRef}
value={props.value || (props.fieldType === Array ? [] : undefined)}
value={
props.fieldType === Array
? props.value
? props.value.filter(value => value !== undefined)
: []
: props.value
}
{...filterDOMProps(props)}
>
{props.allowedValues.map(value => (
Expand Down

0 comments on commit 71f8e87

Please sign in to comment.