Skip to content

Commit

Permalink
fix: trigger search value change when reset controlled combobox (#821)
Browse files Browse the repository at this point in the history
* fix: trigger search value change when reset controlled combobox

* chore: code clean
  • Loading branch information
MadCcc committed Sep 1, 2022
1 parent cd8ef41 commit 9abb1f2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
17 changes: 17 additions & 0 deletions docs/examples/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ class Combobox extends React.Component {
reset
</button>
</p>
<Select
value={value}
mode="combobox"
onChange={this.onChange}
filterOption={(inputValue, option) => {
if (!inputValue) {
return true;
}
return (option.value as string).includes(inputValue);
}}
>
{['1', '2', '3'].map((i) => (
<Option value={i} key={i}>
{i}
</Option>
))}
</Select>
<div>
<Select
disabled={disabled}
Expand Down
9 changes: 3 additions & 6 deletions src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import OptGroup from './OptGroup';
import Option from './Option';
import OptionList from './OptionList';
import SelectContext from './SelectContext';
import { toArray, hasValue } from './utils/commonUtil';
import { hasValue, toArray } from './utils/commonUtil';
import { fillFieldNames, flattenOptions, injectPropsWithOption } from './utils/valueUtil';
import warningProps, { warningNullOptions } from './utils/warningPropsUtil';

Expand Down Expand Up @@ -295,7 +295,7 @@ const Select = React.forwardRef(
const values = convert2LabelValues(internalValue);

// combobox no need save value when it's no value
if (mode === 'combobox' && !hasValue(values[0]?.value)) {
if (mode === 'combobox' && !values[0]?.value) {
return [];
}

Expand Down Expand Up @@ -333,10 +333,7 @@ const Select = React.forwardRef(
React.useEffect(() => {
if (mode === 'combobox') {
const strValue = mergedValues[0]?.value;

if (strValue !== undefined && strValue !== null) {
setSearchValue(String(strValue));
}
setSearchValue(hasValue(strValue) ? String(strValue) : '');
}
}, [mergedValues]);

Expand Down
11 changes: 11 additions & 0 deletions tests/Combobox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ describe('Select.Combobox', () => {
expect(wrapper.render()).toMatchSnapshot();
});

it('renders controlled correctly', () => {
const wrapper = mount(
<Select value="" mode="combobox" placeholder="Search">
<Option value="1">1</Option>
<Option value="2">2</Option>
</Select>,
);

expect(wrapper.render()).toMatchSnapshot();
});

it('set inputValue based on value', () => {
const wrapper = mount(
<Select mode="combobox" value="1">
Expand Down
33 changes: 33 additions & 0 deletions tests/__snapshots__/Combobox.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Select.Combobox renders controlled correctly 1`] = `
<div
class="rc-select rc-select-single rc-select-show-search"
>
<div
class="rc-select-selector"
>
<span
class="rc-select-selection-search"
>
<input
aria-activedescendant="rc_select_TEST_OR_SSR_list_0"
aria-autocomplete="list"
aria-controls="rc_select_TEST_OR_SSR_list"
aria-haspopup="listbox"
aria-owns="rc_select_TEST_OR_SSR_list"
autocomplete="off"
class="rc-select-selection-search-input"
id="rc_select_TEST_OR_SSR"
role="combobox"
type="search"
value=""
/>
</span>
<span
class="rc-select-selection-placeholder"
>
Search
</span>
</div>
</div>
`;

exports[`Select.Combobox renders correctly 1`] = `
<div
class="rc-select rc-select-single rc-select-show-search"
Expand Down

1 comment on commit 9abb1f2

@vercel
Copy link

@vercel vercel bot commented on 9abb1f2 Sep 1, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

select – ./

select.vercel.app
select-react-component.vercel.app
select-git-master-react-component.vercel.app

Please sign in to comment.