Skip to content

Commit b2dc612

Browse files
committed
fix(inputpicker): fix the input does not work when focused by keyboard
1 parent cfaa93e commit b2dc612

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

src/InputPicker/InputAutosize.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface InputAutosizeProps {
3131
placeholder?: string;
3232
style?: React.CSSProperties;
3333
value?: string;
34+
tabIndex?: number;
3435
onChange?: React.ChangeEventHandler<HTMLInputElement>;
3536
onAutosize?: (inputWidth: number) => void;
3637
}
@@ -91,7 +92,8 @@ const InputAutosize = React.forwardRef(
9192
placeholder,
9293
inputClassName,
9394
inputStyle,
94-
inputId = uniqueId
95+
inputId = uniqueId,
96+
tabIndex
9597
} = props;
9698

9799
const rootRef = useRef();
@@ -140,6 +142,7 @@ const InputAutosize = React.forwardRef(
140142

141143
htmlInputProps.className = inputClassName;
142144
htmlInputProps.style = nextInputStyle;
145+
htmlInputProps.tabIndex = tabIndex;
143146

144147
if (isIE()) {
145148
// On Internet Explorer, an `x` symbol will appear in the input box.

src/InputPicker/InputPicker.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ export type ValueType = any;
5757
export interface InputPickerProps<T = ValueType>
5858
extends FormControlPickerProps<T, InputPickerLocale, InputItemDataType>,
5959
SelectProps<T> {
60+
tabIndex?: number;
61+
6062
multi?: boolean;
6163

6264
/** Settings can create new options */
@@ -126,6 +128,7 @@ const InputPicker: PickerComponent<InputPickerProps> = React.forwardRef(
126128
labelKey,
127129
listProps,
128130
id,
131+
tabIndex,
129132
sort,
130133
renderMenu,
131134
renderExtraFooter,
@@ -164,8 +167,6 @@ const InputPicker: PickerComponent<InputPickerProps> = React.forwardRef(
164167
const [maxWidth, setMaxWidth] = useState(100);
165168
const [newData, setNewData] = useState([]);
166169
const [uncontrolledOpen, setOpen] = useState(defaultOpen);
167-
// Use component active state to support keyboard events.
168-
const [active, setActive] = useState(false);
169170
const open = isUndefined(controlledOpen) ? uncontrolledOpen : controlledOpen;
170171

171172
const getAllData = useCallback(() => [].concat(uncontrolledData, newData), [
@@ -516,11 +517,12 @@ const InputPicker: PickerComponent<InputPickerProps> = React.forwardRef(
516517
}, [setFocusItemValue, setSearchKeyword, onClose, value, multi]);
517518

518519
const handleFocus = useCallback(() => {
519-
setActive(true);
520+
setOpen(true);
521+
triggerRef.current?.open();
520522
}, []);
521523

522524
const handleBlur = useCallback(() => {
523-
setActive(false);
525+
setOpen(false);
524526
}, []);
525527

526528
const handleEnter = useCallback(() => {
@@ -675,7 +677,7 @@ const InputPicker: PickerComponent<InputPickerProps> = React.forwardRef(
675677

676678
const classes = merge(pickerClasses, {
677679
[prefix`tag`]: multi,
678-
[prefix`focused`]: open || active
680+
[prefix`focused`]: open
679681
});
680682
const searching = !!searchKeyword && open;
681683
const displaySearchInput = searchable && !disabled;
@@ -721,7 +723,7 @@ const InputPicker: PickerComponent<InputPickerProps> = React.forwardRef(
721723
onClean={handleClean}
722724
cleanable={cleanable && !disabled}
723725
hasValue={hasValue}
724-
active={active}
726+
active={open}
725727
disabled={disabled}
726728
placement={placement}
727729
inputValue={value}
@@ -733,6 +735,7 @@ const InputPicker: PickerComponent<InputPickerProps> = React.forwardRef(
733735
{displaySearchInput && (
734736
<InputSearch
735737
{...inputProps}
738+
tabIndex={tabIndex}
736739
readOnly={readOnly}
737740
onBlur={createChainedFunction(handleBlur, onBlur)}
738741
onFocus={createChainedFunction(handleFocus, onFocus)}

src/InputPicker/test/InputPickerSpec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,9 @@ describe('InputPicker', () => {
325325
assert.equal(instance.querySelector('.rs-picker-toggle-placeholder').innerText, 'Select');
326326
assert.notInclude(instance.className, 'rs-picker-has-value');
327327
});
328+
329+
it('Should set a tabindex for input', () => {
330+
const instance = getDOMNode(<InputPicker tabIndex={10} />);
331+
assert.equal(instance.querySelector('.rs-picker-search-input').getAttribute('tabindex'), '10');
332+
});
328333
});

src/Picker/utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ export function onMenuKeyDown(event: React.KeyboardEvent, events: EventsProps) {
147147
case KEY_CODE.ESC:
148148
case KEY_CODE.TAB:
149149
esc?.(event);
150-
event.preventDefault();
151150
break;
152151
// left arrow
153152
case KEY_CODE.LEFT:
@@ -409,7 +408,7 @@ export const useToggleKeyDownEvent = (props: ToggleKeyDownEventProps) => {
409408
}
410409
}
411410

412-
if (event.keyCode === KEY_CODE.ESC) {
411+
if (event.keyCode === KEY_CODE.ESC || event.keyCode === KEY_CODE.TAB) {
413412
handleClose();
414413
}
415414

src/TagPicker/test/TagPickerSpec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,4 +347,9 @@ describe('TagPicker', () => {
347347
assert.equal(instance.querySelector('.rs-picker-toggle-placeholder').innerText, 'Select');
348348
assert.notInclude(instance.className, 'rs-picker-has-value');
349349
});
350+
351+
it('Should set a tabindex for input', () => {
352+
const instance = getDOMNode(<TagPicker tabIndex={10} />);
353+
assert.equal(instance.querySelector('input[type="text"]').getAttribute('tabindex'), '10');
354+
});
350355
});

0 commit comments

Comments
 (0)