Skip to content

Commit

Permalink
fix: Select should reset when value change to undefined (#443)
Browse files Browse the repository at this point in the history
* Should reset when value reset to undefined

* update test case

* skip init value
  • Loading branch information
zombieJ committed Jan 18, 2020
1 parent e3180cc commit ee508fb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/generate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,15 @@ export default function generateSelector<
const baseValue =
value !== undefined && value !== null ? value : innerValue;

// Should reset when controlled to be uncontrolled
const prevValueRef = React.useRef(value);
React.useEffect(() => {
if (prevValueRef.current !== value && value === undefined) {
setInnerValue(undefined);
}
prevValueRef.current = value;
}, [value]);

/** Unique raw values */
const mergedRawValue = React.useMemo<RawValueType[]>(
() =>
Expand Down
9 changes: 9 additions & 0 deletions tests/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1448,4 +1448,13 @@ describe('Select.Basic', () => {
wrapper.find('input').simulate('change', 'Z');
expect(wrapper.find('List').props().data).toHaveLength(1);
});

it('reset value to undefined should reset display value', () => {
const wrapper = mount(<Select value="light" />);
expect(wrapper.find('.rc-select-selection-item').text()).toEqual('light');

wrapper.setProps({ value: undefined });
wrapper.update();
expect(wrapper.find('.rc-select-selection-item')).toHaveLength(0);
});
});

1 comment on commit ee508fb

@vercel
Copy link

@vercel vercel bot commented on ee508fb Jan 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.