Skip to content

Commit

Permalink
Fix the Chips input component for mobile devices
Browse files Browse the repository at this point in the history
  • Loading branch information
YZarytskyi committed Mar 5, 2024
1 parent a2a2f11 commit 3b138ad
Showing 1 changed file with 19 additions and 7 deletions.
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

0 comments on commit 3b138ad

Please sign in to comment.