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

Fix #6086: Chips input component doesn't function properly on mobile devices #6088

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 19 additions & 7 deletions components/lib/chips/Chips.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export const Chips = React.memo(
return;
}

switch (event.code) {
switch (event.key) {
case 'Backspace':
if (inputValue.length === 0 && values.length > 0) {
removeItem(event, values.length - 1);
Expand All @@ -164,7 +164,6 @@ export const Chips = React.memo(
break;

case 'Enter':
case 'NumpadEnter':
if (inputValue && inputValue.trim().length && (!props.max || props.max > values.length)) {
addItem(event, inputValue, true);
}
Expand All @@ -189,11 +188,6 @@ export const Chips = React.memo(

if (isMaxedOut()) {
event.preventDefault();
} else if (props.separator === ',') {
// GitHub #3885 Android Opera gives strange code 229 for comma
if (event.key === props.separator || (DomHandler.isAndroid() && event.which === 229)) {
addItem(event, inputValue, true);
}
}

break;
Expand Down Expand Up @@ -223,6 +217,22 @@ export const Chips = React.memo(
preventDefault && event.preventDefault();
};

const onChange = (event) => {
const value = event.target.value?.trim();

if (value === props.separator) {
inputRef.current.value = '';

return;
}

if (props.separator && value.endsWith(props.separator)) {
const trimmedValue = value.slice(0, -1);

addItem(event, trimmedValue);
}
};

const onPaste = (event) => {
if (props.separator) {
let separator = props.separator.replace('\\n', '\n').replace('\\r', '\r').replace('\\t', '\t');
Expand Down Expand Up @@ -372,9 +382,11 @@ export const Chips = React.memo(
ref: inputRef,
placeholder: props.placeholder,
type: 'text',
enterKeyHint: 'enter',
name: props.name,
disabled: props.disabled || isMaxedOut(),
onKeyDown: (e) => onKeyDown(e),
onChange: (e) => onChange(e),
onPaste: (e) => onPaste(e),
onFocus: (e) => onFocus(e),
onBlur: (e) => onBlur(e),
Expand Down
2 changes: 1 addition & 1 deletion components/lib/chips/chips.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export interface ChipsProps extends Omit<React.DetailedHTMLProps<React.InputHTML
*/
ariaLabelledBy?: string | undefined;
/**
* Separator char to add an item when pressed in addition to the enter key. Currently only possible value is ','.
* Separator char to add an item when pressed in addition to the enter key.
*/
separator?: string | undefined;
/**
Expand Down
Loading