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: TagsInput MDS-1005 #2554

Merged
merged 3 commits into from
Feb 26, 2024
Merged
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
38 changes: 31 additions & 7 deletions workspaces/core/src/tagsInput/TagsInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,38 @@ const TagsInputRoot = forwardRef<HTMLSpanElement, TagsInputRootProps>(
ref
) => {
const [isFocused, setIsFocused] = useState(false);
const [isInvalid, setIsInvalid] = useState(false);

const states = {
isUppercase,
value: selected,
size: size,
disabled: disabled,
isError: isError,
isError: isError || isInvalid,
onClear: onClear,
};

const checkValidity = (event: React.FormEvent<HTMLInputElement>) => {
const target = event.target as HTMLInputElement;
const validity = target.validity;

if (!target.value.length) {
target.value = '';
setIsInvalid(validity.valueMissing);
return;
}

setIsInvalid(validity.typeMismatch ||
validity.patternMismatch ||
validity.tooLong ||
validity.tooShort ||
validity.rangeUnderflow ||
validity.rangeOverflow ||
validity.stepMismatch ||
validity.badInput ||
validity.customError);
};

return (
<TagsInputContext.Provider value={states}>
<Listbox horizontal={false}>
Expand All @@ -57,7 +80,7 @@ const TagsInputRoot = forwardRef<HTMLSpanElement, TagsInputRootProps>(
'focus:shadow-input-focus focus:outline-none',
'focus-visible::shadow-input-focus focus-visible::outline-none',
getWrapperStyle(size),
isError &&
(isError || isInvalid) &&
'shadow-input-err hover:shadow-input-err focus:shadow-input-err focus-visible:shadow-input-err',
disabled &&
'opacity-60 shadow-input focus:shadow-input hover:shadow-input cursor-not-allowed',
Expand All @@ -75,24 +98,25 @@ const TagsInputRoot = forwardRef<HTMLSpanElement, TagsInputRootProps>(
getTextSize(size)
)}
placeholder={placeholder}
error={isError}
error={isError || isInvalid}
disabled={disabled}
type={type}
onKeyDown={(e) => {
e.code === 'Enter' &&
(e.target as HTMLInputElement).value.length &&
e.code === 'Enter' && !isInvalid &&
!!(e.target as HTMLInputElement).value.length &&
onEnter &&
onEnter((e.target as HTMLInputElement).value);
e.code === 'Enter' &&
e.code === 'Enter' && !isInvalid &&
((e.target as HTMLInputElement).value = '');
e.code === 'Backspace' &&
(e.target as HTMLInputElement).value === '' &&
!(e.target as HTMLInputElement).value.length &&
selected.length !== 0 &&
onClear &&
onClear(selected.length - 1);
}}
onFocus={() => setIsFocused(true)}
onBlur={() => setIsFocused(false)}
onInput={(e) => checkValidity(e) }
/>
</span>
</Listbox>
Expand Down
Loading