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

[Select] Create options onPaste event #162

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 41 additions & 2 deletions src/form/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ import { CloneElement, useId } from '../../utils';
import { SelectInput, SelectInputProps, SelectInputRef } from './SelectInput';
import { SelectMenu, SelectMenuProps } from './SelectMenu';
import { SelectOptionProps, SelectValue } from './SelectOption';
import { useWidth } from './utils/useWidth';
import { useFuzzy } from '@reaviz/react-use-fuzzy';
import { createOptions, getGroups } from './utils';
import { createOptions, getGroups, useWidth, keyNameToCode } from './utils';
import isEqual from 'react-fast-compare';

export interface SelectProps {
Expand Down Expand Up @@ -638,6 +637,45 @@ export const Select: FC<Partial<SelectProps>> = ({
[createable, menuDisabled, onInputBlur, toggleSelectedOption]
);

const onPasteHandler = useCallback(
(e: React.ClipboardEvent<HTMLInputElement>) => {
if (createable && multiple) {
SerhiiTsybulskyi marked this conversation as resolved.
Show resolved Hide resolved
const inputElement = e.target as HTMLInputElement;
const inputValue = inputElement.value;
const clipboardValue = e.clipboardData.getData('Text');
const value = `${inputValue}${clipboardValue}`.trim();
const separators = selectOnKeys?.map(key =>
String.fromCharCode(keyNameToCode[key])
);
const expression = `[${separators}]`;
const regex = new RegExp(expression, 'g');
const items = value.split(regex);
if (items.length > 1) {
const result = toggleSelectedMultiOption(
items.map(item => ({ value: item, children: item }))
);
if (result.newOptions?.length) {
onOptionsChange?.([...options, ...result.newOptions]);
}
setInternalValue(result.newValue);
resetInput();
onChange?.(result.newValue);
e.preventDefault();
}
}
},
[
createable,
multiple,
onChange,
onOptionsChange,
options,
resetInput,
selectOnKeys,
toggleSelectedMultiOption
]
);

const onMenuSelectedChange = useCallback(
(option: SelectValue) => {
toggleSelectedOption(option);
Expand Down Expand Up @@ -728,6 +766,7 @@ export const Select: FC<Partial<SelectProps>> = ({
onBlur={onInputBlured}
onFocus={onInputFocused}
onRefresh={onRefresh}
onPaste={onPasteHandler}
/>
</ConnectedOverlay>
);
Expand Down
7 changes: 7 additions & 0 deletions src/form/Select/SelectInput/SelectInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ export interface SelectInputProps {
*/
onInputChange: (event: React.ChangeEvent<HTMLInputElement>) => void;

/**
* The function to handle input paste event.
*/
onPaste: (event: React.ClipboardEvent<HTMLInputElement>) => void;

/**
* The function to handle refresh.
*/
Expand Down Expand Up @@ -253,6 +258,7 @@ export const SelectInput: FC<Partial<SelectInputProps>> = ({
onFocus,
onBlur,
onRefresh,
onPaste,
chip,
theme: customTheme
}) => {
Expand Down Expand Up @@ -521,6 +527,7 @@ export const SelectInput: FC<Partial<SelectInputProps>> = ({
onChange={onChange}
onFocus={onInputFocus}
onBlur={onBlur}
onPaste={onPaste}
placeholderIsMinWidth={false}
/>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/form/Select/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './grouping';
export * from './options';
export * from './useWidth';
export * from './keyboard';
102 changes: 102 additions & 0 deletions src/form/Select/utils/keyboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
export const keyNameToCode = {
Copy link
Contributor Author

@SerhiiTsybulskyi SerhiiTsybulskyi Apr 30, 2024

Choose a reason for hiding this comment

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

I've added this mapping to keep readability for

selectOnKeys={['Space', 'Comma']

I think this more friendly format instead array of numbers (key codes)

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is works for Keyboard events (manually filling) but didn't work on paste due to ClipboardEvent (another way to interact with input needs a little bit different handler)

Backspace: 8,
Tab: 9,
Enter: 13,
Shift: 16,
Ctrl: 17,
Alt: 18,
'Pause/Break': 19,
'Caps Lock': 20,
Esc: 27,
Space: 32,
'Page Up': 33,
'Page Down': 34,
End: 35,
Home: 36,
Left: 37,
Up: 38,
Right: 39,
Down: 40,
Insert: 45,
Delete: 46,
'0': 48,
'1': 49,
'2': 50,
'3': 51,
'4': 52,
'5': 53,
'6': 54,
'7': 55,
'8': 56,
'9': 57,
A: 65,
B: 66,
C: 67,
D: 68,
E: 69,
F: 70,
G: 71,
H: 72,
I: 73,
J: 74,
K: 75,
L: 76,
M: 77,
N: 78,
O: 79,
P: 80,
Q: 81,
R: 82,
S: 83,
T: 84,
U: 85,
V: 86,
W: 87,
X: 88,
Y: 89,
Z: 90,
Windows: 91,
'Right Click': 93,
'Numpad 0': 96,
'Numpad 1': 97,
'Numpad 2': 98,
'Numpad 3': 99,
'Numpad 4': 100,
'Numpad 5': 101,
'Numpad 6': 102,
'Numpad 7': 103,
'Numpad 8': 104,
'Numpad 9': 105,
'Numpad *': 106,
'Numpad +': 107,
'Numpad -': 109,
'Numpad .': 110,
'Numpad /': 111,
F1: 112,
F2: 113,
F3: 114,
F4: 115,
F5: 116,
F6: 117,
F7: 118,
F8: 119,
F9: 120,
F10: 121,
F11: 122,
F12: 123,
'Num Lock': 144,
'Scroll Lock': 145,
'My Computer': 182,
'My Calculator': 183,
';': 186,
'=': 187,
',': 188,
'-': 189,
'.': 190,
'/': 191,
'`': 192,
'[': 219,
'\\': 220,
']': 221,
"'": 222
};
Loading