Skip to content

Commit b3c332a

Browse files
committed
feat: [tabSelectsValue api]支持配置是否启用tab快捷键控制选中选项的逻辑
1 parent e8096c0 commit b3c332a

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export default () => (
113113
| onFocus | called when focus | function | - |
114114
| onPopupScroll | called when menu is scrolled | function | - |
115115
| onSelect | called when a option is selected. param is option's value and option instance | Function(value, option:Option) | - |
116+
| tabSelectsValue | whether to enable the hot key for tab selection | boolean | true |
116117
| onDeselect | called when a option is deselected. param is option's value. only called for multiple or tags | Function(value, option:Option) | - |
117118
| onInputKeyDown | called when key down on input | Function(event) | - |
118119
| defaultActiveFirstOption | whether active first option by default | boolean | true |

src/OptionList.tsx

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, r
5252
onActiveValue,
5353
defaultActiveFirstOption,
5454
onSelect,
55+
tabSelectsValue,
5556
menuItemSelectedIcon,
5657
rawValues,
5758
fieldNames,
@@ -185,6 +186,20 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, r
185186
};
186187

187188
// ========================= Keyboard =========================
189+
const selectOptionHotKeyLogic = (event) => {
190+
// value
191+
const item = memoFlattenOptions[activeIndex];
192+
if (item && !item?.data?.disabled && !overMaxCount) {
193+
onSelectValue(item.value);
194+
} else {
195+
onSelectValue(undefined);
196+
}
197+
198+
if (open) {
199+
event.preventDefault();
200+
}
201+
};
202+
188203
React.useImperativeHandle(ref, () => ({
189204
onKeyDown: (event) => {
190205
const { which, ctrlKey } = event;
@@ -217,20 +232,12 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, r
217232
}
218233

219234
// >>> Select (Tab / Enter)
220-
case KeyCode.TAB:
235+
case KeyCode.TAB: {
236+
tabSelectsValue && selectOptionHotKeyLogic(event);
237+
break;
238+
}
221239
case KeyCode.ENTER: {
222-
// value
223-
const item = memoFlattenOptions[activeIndex];
224-
if (item && !item?.data?.disabled && !overMaxCount) {
225-
onSelectValue(item.value);
226-
} else {
227-
onSelectValue(undefined);
228-
}
229-
230-
if (open) {
231-
event.preventDefault();
232-
}
233-
240+
selectOptionHotKeyLogic(event);
234241
break;
235242
}
236243

src/Select.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export interface SelectProps<ValueType = any, OptionType extends BaseOptionType
124124
// >>> Select
125125
onSelect?: SelectHandler<ArrayElementType<ValueType>, OptionType>;
126126
onDeselect?: SelectHandler<ArrayElementType<ValueType>, OptionType>;
127+
tabSelectsValue?: boolean;
127128

128129
// >>> Options
129130
/**
@@ -181,6 +182,7 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp
181182
onSelect,
182183
onDeselect,
183184
popupMatchSelectWidth = true,
185+
tabSelectsValue = true,
184186

185187
// Options
186188
filterOption,
@@ -616,6 +618,7 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp
616618
onActiveValue,
617619
defaultActiveFirstOption: mergedDefaultActiveFirstOption,
618620
onSelect: onInternalSelect,
621+
tabSelectsValue,
619622
menuItemSelectedIcon,
620623
rawValues,
621624
fieldNames: mergedFieldNames,
@@ -634,6 +637,7 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp
634637
onActiveValue,
635638
mergedDefaultActiveFirstOption,
636639
onInternalSelect,
640+
tabSelectsValue,
637641
menuItemSelectedIcon,
638642
rawValues,
639643
mergedFieldNames,

src/SelectContext.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface SelectContextProps {
1717
onActiveValue: OnActiveValue;
1818
defaultActiveFirstOption?: boolean;
1919
onSelect: OnInternalSelect;
20+
tabSelectsValue: boolean;
2021
menuItemSelectedIcon?: RenderNode;
2122
rawValues: Set<RawValueType>;
2223
fieldNames?: FieldNames;

0 commit comments

Comments
 (0)