Skip to content

Commit 3147942

Browse files
committed
feat: add activeOptionFilter prop and update combobox examples
1 parent b0b7473 commit 3147942

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export default () => (
132132
| direction | direction of dropdown | 'ltr' \| 'rtl' | 'ltr' |
133133
| optionRender | Custom rendering options | (oriOption: FlattenOptionData\<BaseOptionType\> , info: { index: number }) => React.ReactNode | - |
134134
| labelRender | Custom rendering label | (props: LabelInValueType) => React.ReactNode | - |
135+
| activeOptionFilter | Custom filter function for active option when searching. | (searchValue: string, option: OptionType) => boolean | - |
135136
| maxCount | The max number of items can be selected | number | - |
136137

137138
### Methods

docs/examples/combobox.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,40 @@ class Combobox extends React.Component {
150150
options={this.state.options}
151151
onChange={this.onAsyncChange}
152152
/>
153+
154+
<h3>Active Option Filter - Middle Match</h3>
155+
<Select
156+
style={{ width: 500 }}
157+
showSearch
158+
allowClear
159+
mode="combobox"
160+
placeholder="Search value can be matched anywhere in the option's value. Try input 'ran'"
161+
activeOptionFilter={(searchValue, option) => {
162+
return String(option.value).includes(searchValue);
163+
}}
164+
>
165+
{['apple', 'banana', 'orange', 'grape'].map((i) => (
166+
<Option value={i} key={i}>
167+
{i}
168+
</Option>
169+
))}
170+
</Select>
171+
172+
<h3>No Active Highlight</h3>
173+
<Select
174+
style={{ width: 500 }}
175+
showSearch
176+
allowClear
177+
mode="combobox"
178+
placeholder="No option will be actively highlighted."
179+
activeOptionFilter={() => false}
180+
>
181+
{['apple', 'banana', 'orange', 'grape'].map((i) => (
182+
<Option value={i} key={i}>
183+
{i}
184+
</Option>
185+
))}
186+
</Select>
153187
</div>
154188
</div>
155189
);

src/OptionList.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, r
6060
listHeight,
6161
listItemHeight,
6262
optionRender,
63+
activeOptionFilter,
6364
classNames: contextClassNames,
6465
styles: contextStyles,
6566
} = React.useContext(SelectContext);
@@ -159,9 +160,12 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, r
159160
if (!multiple && open && rawValues.size === 1) {
160161
const value: RawValueType = Array.from(rawValues)[0];
161162
// Scroll to the option closest to the searchValue if searching.
162-
const index = memoFlattenOptions.findIndex(({ data }) =>
163-
searchValue ? String(data.value).startsWith(searchValue) : data.value === value,
164-
);
163+
const index = memoFlattenOptions.findIndex(({ data }) => {
164+
if (activeOptionFilter) {
165+
return activeOptionFilter(searchValue, data);
166+
}
167+
return searchValue ? String(data.value).startsWith(searchValue) : data.value === value;
168+
});
165169

166170
if (index !== -1) {
167171
setActive(index);

src/Select.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ export interface SelectProps<ValueType = any, OptionType extends BaseOptionType
151151
listHeight?: number;
152152
listItemHeight?: number;
153153
labelRender?: (props: LabelInValueType) => React.ReactNode;
154+
activeOptionFilter?: (searchValue: string, option: OptionType) => boolean;
154155

155156
// >>> Icon
156157
menuItemSelectedIcon?: RenderNode;
@@ -213,6 +214,7 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp
213214
maxCount,
214215
classNames,
215216
styles,
217+
activeOptionFilter,
216218
...restProps
217219
} = props;
218220

@@ -647,6 +649,7 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp
647649
optionRender,
648650
classNames,
649651
styles,
652+
activeOptionFilter,
650653
};
651654
}, [
652655
maxCount,
@@ -667,6 +670,7 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp
667670
optionRender,
668671
classNames,
669672
styles,
673+
activeOptionFilter,
670674
]);
671675

672676
// ========================== Warning ===========================

src/SelectContext.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface SelectContextProps {
3737
listItemHeight?: number;
3838
childrenAsData?: boolean;
3939
maxCount?: number;
40+
activeOptionFilter?: SelectProps['activeOptionFilter'];
4041
}
4142

4243
const SelectContext = React.createContext<SelectContextProps>(null);

0 commit comments

Comments
 (0)