Skip to content

Commit

Permalink
feat: handle clear button
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabhdaware committed Apr 10, 2024
1 parent 6c8cada commit d37fd80
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
17 changes: 17 additions & 0 deletions packages/blade/src/components/Input/BaseInput/useTaggedInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const useTaggedInput = ({
getTags: ({ size }: { size: NonNullable<BaseInputProps['size']> }) => React.ReactElement[];
handleTaggedInputKeydown: (e: FormInputOnKeyDownEvent) => void;
handleTaggedInputChange: FormInputOnEvent;
handleTagsClear: () => void;
} => {
const [activeTagIndex, setActiveTagIndex] = React.useState(-1);
const [inputValueUncontrolled, setInputValueUncontrolled] = React.useState(defaultValue ?? '');
Expand Down Expand Up @@ -79,9 +80,24 @@ const useTaggedInput = ({
);

const handleTaggedInputChange: FormInputOnEvent = ({ value }) => {
if (!isTaggedInput) {
return;
}
setInputValueUncontrolled(value ?? '');
};

const handleTagsClear = (): void => {
if (!isTaggedInput) {
return;
}

if (!isTagsControlled) {
setTagsUncontrolled([]);
}

onTagChange?.({ tags: [] });
};

const clearInput = (): void => {
const isControlledValue = Boolean(value);

Expand Down Expand Up @@ -143,6 +159,7 @@ const useTaggedInput = ({
getTags,
handleTaggedInputKeydown,
handleTaggedInputChange,
handleTagsClear,
};
};

Expand Down
2 changes: 2 additions & 0 deletions packages/blade/src/components/Input/TextArea/TextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ const _TextArea: React.ForwardRefRenderFunction<BladeElementRef, TextAreaProps>
getTags,
handleTaggedInputKeydown,
handleTaggedInputChange,
handleTagsClear,
} = useTaggedInput({
tags,
onTagChange,
Expand Down Expand Up @@ -169,6 +170,7 @@ const _TextArea: React.ForwardRefRenderFunction<BladeElementRef, TextAreaProps>
inputRef.current.focus();
}
}
handleTagsClear();
// if the input field is controlled just call the click handler and the value change shall be left upto the consumer
onClearButtonClick?.();
inputRef?.current?.focus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ export const TextInputWithControlledTags: StoryFn<typeof TextInputComponent> = (
{...args}
isTaggedInput={true}
tags={tags}
showClearButton={true}
showClearButton={false}
onTagChange={({ tags }) => {
setTags(tags);
}}
Expand Down
2 changes: 2 additions & 0 deletions packages/blade/src/components/Input/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ const _TextInput: React.ForwardRefRenderFunction<BladeElementRef, TextInputProps
getTags,
handleTaggedInputKeydown,
handleTaggedInputChange,
handleTagsClear,
} = useTaggedInput({
isTaggedInput,
tags,
Expand Down Expand Up @@ -222,6 +223,7 @@ const _TextInput: React.ForwardRefRenderFunction<BladeElementRef, TextInputProps
textInputRef.current.focus();
}
}
handleTagsClear();
// if the input field is controlled just call the click handler and the value change shall be left upto the consumer
onClearButtonClick?.();
textInputRef?.current?.focus();
Expand Down

0 comments on commit d37fd80

Please sign in to comment.