|
|
@@ -55,6 +55,7 @@ export interface TagInputProps extends Omit<React.HTMLProps<TagInputDerivedEleme |
|
|
|
* Separators for splitting the input value into multiple tags. |
|
|
|
*/ |
|
|
|
separator?: TagInputSeparator[], |
|
|
|
editOnRemove?: boolean, |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@@ -78,6 +79,10 @@ export const TagInput = React.forwardRef<TagInputDerivedElement, TagInputProps>( |
|
|
|
defaultValue, |
|
|
|
disabled, |
|
|
|
id: idProp, |
|
|
|
onFocus, |
|
|
|
onBlur, |
|
|
|
editOnRemove = false, |
|
|
|
placeholder, |
|
|
|
...etcProps |
|
|
|
}, |
|
|
|
forwardedRef, |
|
|
@@ -119,7 +124,19 @@ export const TagInput = React.forwardRef<TagInputDerivedElement, TagInputProps>( |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
const handleBlur = () => { |
|
|
|
const handleFocus: React.FocusEventHandler<HTMLTextAreaElement> = (e) => { |
|
|
|
if (!clientSide) { |
|
|
|
onFocus?.(e); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
const handleBlur: React.FocusEventHandler<HTMLTextAreaElement> = (e) => { |
|
|
|
if (!clientSide) { |
|
|
|
onBlur?.(e); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
const handleRemoveTag = () => { |
|
|
|
if (!(typeof ref === 'object' && ref)) { |
|
|
|
return; |
|
|
|
} |
|
|
@@ -128,10 +145,59 @@ export const TagInput = React.forwardRef<TagInputDerivedElement, TagInputProps>( |
|
|
|
return; |
|
|
|
} |
|
|
|
setTimeout(() => { |
|
|
|
delegateTriggerEvent('blur', input); |
|
|
|
const sibling = input.nextElementSibling as HTMLDivElement; |
|
|
|
const tagsInput = sibling.children[sibling.children.length - 1] as HTMLInputElement; |
|
|
|
tagsInput.focus(); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
const handleInputBlur: React.FocusEventHandler<HTMLInputElement> = (e) => { |
|
|
|
if (!(typeof ref === 'object' && ref)) { |
|
|
|
return; |
|
|
|
} |
|
|
|
const { current: input } = ref; |
|
|
|
if (!input) { |
|
|
|
return; |
|
|
|
} |
|
|
|
onBlur?.({ |
|
|
|
...e, |
|
|
|
target: input, |
|
|
|
currentTarget: input, |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
const handleFocusCapture: React.FocusEventHandler<HTMLDivElement> = (e) => { |
|
|
|
const { currentTarget } = e; |
|
|
|
if (!clientSide) { |
|
|
|
return; |
|
|
|
} |
|
|
|
const { activeElement } = window.document; |
|
|
|
if (!activeElement) { |
|
|
|
return; |
|
|
|
} |
|
|
|
const tagInputWrapper = currentTarget.children[1] as HTMLDivElement; |
|
|
|
const tagInput = ( |
|
|
|
tagInputWrapper.children[tagInputWrapper.children.length - 1] as HTMLInputElement |
|
|
|
); |
|
|
|
if (activeElement !== tagInput) { |
|
|
|
return; |
|
|
|
} |
|
|
|
if (!(typeof ref === 'object' && ref)) { |
|
|
|
return; |
|
|
|
} |
|
|
|
const { current: input } = ref; |
|
|
|
if (!input) { |
|
|
|
return; |
|
|
|
} |
|
|
|
if (activeElement.tagName === 'INPUT') { |
|
|
|
onFocus?.({ |
|
|
|
...e, |
|
|
|
target: input, |
|
|
|
currentTarget: input, |
|
|
|
}); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
return ( |
|
|
|
<div |
|
|
|
className={clsx( |
|
|
@@ -155,18 +221,23 @@ export const TagInput = React.forwardRef<TagInputDerivedElement, TagInputProps>( |
|
|
|
variant === 'alternate' && 'tag-input-alternate', |
|
|
|
className, |
|
|
|
)} |
|
|
|
onFocusCapture={handleFocusCapture} |
|
|
|
> |
|
|
|
<textarea |
|
|
|
{...etcProps} |
|
|
|
placeholder={placeholder} |
|
|
|
disabled={disabled} |
|
|
|
ref={ref} |
|
|
|
id={id} |
|
|
|
aria-labelledby={labelId} |
|
|
|
data-testid="input" |
|
|
|
defaultValue={defaultValue} |
|
|
|
onFocus={handleFocus} |
|
|
|
onBlur={handleBlur} |
|
|
|
style={{ |
|
|
|
height: clientSide ? undefined : 0, |
|
|
|
}} |
|
|
|
tabIndex={clientSide ? -1 : undefined} |
|
|
|
className={clsx( |
|
|
|
'bg-negative rounded-inherit peer block', |
|
|
|
'focus:outline-0', |
|
|
@@ -219,9 +290,12 @@ export const TagInput = React.forwardRef<TagInputDerivedElement, TagInputProps>( |
|
|
|
input: 'peer bg-transparent', |
|
|
|
tag: 'text-xs p-2 select-none', |
|
|
|
}} |
|
|
|
isEditOnRemove={editOnRemove} |
|
|
|
placeHolder={placeholder} |
|
|
|
disabled={disabled} |
|
|
|
onBlur={handleInputBlur} |
|
|
|
onChange={handleTagsInputChange} |
|
|
|
onBlur={handleBlur} |
|
|
|
onRemoved={handleRemoveTag} |
|
|
|
separators={separator.map((s) => TAG_INPUT_SEPARATOR_MAP[s])} |
|
|
|
/> |
|
|
|
)} |
|
|
@@ -322,4 +396,5 @@ TagInput.defaultProps = { |
|
|
|
block: false, |
|
|
|
hiddenLabel: false, |
|
|
|
enhanced: false, |
|
|
|
editOnRemove: false, |
|
|
|
}; |