TagInput behaves as dropdown when limit is set to 1

This commit is contained in:
Sam Becker 2025-03-31 08:47:37 -05:00
parent 83b217d711
commit b943d3c102
2 changed files with 20 additions and 10 deletions

View File

@ -34,6 +34,8 @@ export default function TagInput({
limit?: number
limitValidationMessage?: string
}) {
const behaveAsDropdown = limit === 1;
const containerRef = useRef<HTMLInputElement>(null);
const inputRef = useRef<HTMLInputElement>(null);
const optionsRef = useRef<HTMLInputElement>(null);
@ -51,8 +53,10 @@ export default function TagInput({
, [value]);
const hasReachedLimit = useMemo(() =>
limit !== undefined && selectedOptions.length >= limit
, [limit, selectedOptions]);
limit !== undefined &&
selectedOptions.length >= limit &&
!behaveAsDropdown
, [limit, behaveAsDropdown, selectedOptions]);
const inputTextFormatted = parameterize(inputText);
const isInputTextUnique =
@ -100,21 +104,29 @@ export default function TagInput({
.filter(option => !selectedOptions.includes(option));
if (optionsToAdd.length > 0) {
onChange?.([
...selectedOptions,
...optionsToAdd,
].join(','));
if (behaveAsDropdown) {
// If behaving as dropdown, replace contents on add
onChange?.(optionsToAdd[0]);
} else {
onChange?.([
...selectedOptions,
...optionsToAdd,
].join(','));
}
}
setSelectedOptionIndex(undefined);
setInputText('');
if (limit !== undefined && limit - 1 >= selectedOptions.length) {
if (
behaveAsDropdown ||
(limit !== undefined && limit - 1 >= selectedOptions.length)
) {
hideMenu(true);
} else {
inputRef.current?.focus();
}
}, [limit, selectedOptions, onChange, hideMenu]);
}, [limit, behaveAsDropdown, selectedOptions, onChange, hideMenu]);
const removeOption = useCallback((option: string) => {
onChange?.(selectedOptions.filter(o =>

View File

@ -123,7 +123,6 @@ const FORM_METADATA = (
note: 'Intended for Fujifilm simulations and analog scans',
noteShort: 'Fujifilm simulations / analog scans',
tagOptions: filmOptions,
tagOptionsLimitValidationMessage: 'Photos can only have one film',
tagOptionsLimit: 1,
shouldNotOverwriteWithNullDataOnSync: true,
},
@ -131,7 +130,6 @@ const FORM_METADATA = (
label: 'recipe title',
tagOptions: recipeOptions,
tagOptionsLimit: 1,
tagOptionsLimitValidationMessage: 'Photos can only have one recipe',
spellCheck: false,
capitalize: false,
shouldHide: ({ make }) => make !== MAKE_FUJIFILM,