Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to define custom keys to create values in select #145

Merged
merged 3 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/form/Select/MultiSelect.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export const Createable = () => {
multiple
closeOnSelect={false}
createable
valuesSplitterKeyCodes={['Enter', 'Space', 'Comma']}
placeholder="Add some categories or pick existing one..."
value={value}
onChange={v => setValue(v)}
Expand Down
35 changes: 27 additions & 8 deletions src/form/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ export interface SelectProps {
*/
createable?: boolean;

/**
* The list of KeyCodes for creating select values.
SerhiiTsybulskyi marked this conversation as resolved.
Show resolved Hide resolved
*/
valuesSplitterKeyCodes?: string[];
SerhiiTsybulskyi marked this conversation as resolved.
Show resolved Hide resolved

/**
* The options of the select.
*/
Expand Down Expand Up @@ -206,6 +211,7 @@ export const Select: FC<Partial<SelectProps>> = ({
placeholder,
disabled,
createable,
valuesSplitterKeyCodes,
loading,
multiple,
error,
Expand Down Expand Up @@ -492,10 +498,15 @@ export const Select: FC<Partial<SelectProps>> = ({
]
);

const onEnterKeyUp = useCallback(
const onAddSelection = useCallback(
(event: React.KeyboardEvent<HTMLInputElement>) => {
const inputElement = event.target as HTMLInputElement;
const inputValue = inputElement.value.trim();
let inputValue = inputElement.value.trim();
// Remove the last character if it is a valuesSplitterKeyCode
inputValue =
inputValue.charAt(inputValue.length - 1) === event.key
? inputValue.slice(0, -1)
: inputValue;

if (index === -1 && createable && !inputValue) {
return;
Expand Down Expand Up @@ -533,7 +544,7 @@ export const Select: FC<Partial<SelectProps>> = ({
}

if (index > -1 || (createable && inputValue)) {
onEnterKeyUp(event);
onAddSelection(event);
}

if (multiple) {
Expand All @@ -542,26 +553,33 @@ export const Select: FC<Partial<SelectProps>> = ({
setOpen(false);
}
},
[index, onEnterKeyUp, setOpen, multiple, createable]
[index, onAddSelection, setOpen, multiple, createable]
);

const onInputKeyedUp = useCallback(
(event: React.KeyboardEvent<HTMLInputElement>) => {
const key = event.key;
const key = event.code;

if (key === 'ArrowUp') {
onArrowUpKeyUp(event);
} else if (key === 'ArrowDown') {
onArrowDownKeyUp(event);
} else if (key === 'Escape') {
resetSelect();
} else if (key === 'Enter') {
onEnterKeyUp(event);
} else if (valuesSplitterKeyCodes?.includes(key)) {
onAddSelection(event);
}

onInputKeyUp?.(event);
},
[onArrowDownKeyUp, onArrowUpKeyUp, onEnterKeyUp, onInputKeyUp, resetSelect]
[
valuesSplitterKeyCodes,
onInputKeyUp,
onArrowUpKeyUp,
onArrowDownKeyUp,
resetSelect,
onAddSelection
]
);

const onInputKeyedDown = useCallback(
Expand Down Expand Up @@ -693,6 +711,7 @@ export const Select: FC<Partial<SelectProps>> = ({

Select.defaultProps = {
clearable: true,
valuesSplitterKeyCodes: ['Enter'],
filterable: true,
menuPlacement: 'bottom-start',
closeOnSelect: true,
Expand Down
Loading