Skip to content

Commit

Permalink
[DSTSUP-21]: Adding onClear over Autocomplete component (#3645)
Browse files Browse the repository at this point in the history
  • Loading branch information
OsamaAbdellateef authored Jan 18, 2024
1 parent 139190e commit 7e3aa28
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-radios-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marigold/components": patch
---

Adding onClear prop over Autocomplete component
19 changes: 19 additions & 0 deletions packages/components/src/Autocomplete/Autocomplete.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,25 @@ test('supports autocompletion', async () => {
expect(input).toHaveValue('Spinach');
});

test('supports clear input value', async () => {
render(
<Autocomplete label="Label" data-testid="input-field">
<Autocomplete.Item id="spinach">Spinach</Autocomplete.Item>
<Autocomplete.Item id="carrots">Carrots</Autocomplete.Item>
<Autocomplete.Item id="broccoli">Broccoli</Autocomplete.Item>
<Autocomplete.Item id="garlic">Garlic</Autocomplete.Item>
</Autocomplete>
);

const input = screen.getByRole('combobox');
await user.type(input, 'sp');

const clearButton = screen.getByTestId('clear-button');
expect(clearButton).toBeInTheDocument();
await user.click(clearButton);
expect(input).toHaveValue('');
});

test('supports submit handler', async () => {
const spy = jest.fn();

Expand Down
11 changes: 8 additions & 3 deletions packages/components/src/Autocomplete/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ import { AutocompleteClearButton } from './ClearButton';
//----------------
interface SearchInputProps {
onSubmit?: (key: Key | null, value: string | null) => void;
onClear?: () => void;
ref?: Ref<HTMLInputElement> | undefined;
}
const SearchInput = ({ onSubmit, ref }: SearchInputProps) => {
const SearchInput = ({ onSubmit, onClear, ref }: SearchInputProps) => {
const state = React.useContext(ComboBoxStateContext);

return (
<Input
ref={ref}
icon={<SearchIcon />}
action={
state?.inputValue !== '' ? <AutocompleteClearButton /> : undefined
state?.inputValue !== '' ? (
<AutocompleteClearButton onClear={onClear} />
) : undefined
}
onKeyDown={e => {
if (e.key === 'Enter' || e.key === 'Escape') {
Expand Down Expand Up @@ -81,6 +84,7 @@ export interface AutocompleteProps
defaultValue?: RAC.ComboBoxProps<object>['defaultInputValue'];
value?: RAC.ComboBoxProps<object>['inputValue'];
onChange?: RAC.ComboBoxProps<object>['onInputChange'];
onClear?: () => void;
disabled?: RAC.ComboBoxProps<object>['isDisabled'];
required?: RAC.ComboBoxProps<object>['isRequired'];
error?: RAC.ComboBoxProps<object>['isInvalid'];
Expand Down Expand Up @@ -113,6 +117,7 @@ const _Autocomplete = forwardRef<HTMLInputElement, AutocompleteProps>(
defaultValue,
value,
onChange,
onClear,
onSubmit,
disabled,
error,
Expand All @@ -138,7 +143,7 @@ const _Autocomplete = forwardRef<HTMLInputElement, AutocompleteProps>(
return (
<>
<FieldBase as={ComboBox} {...props}>
<SearchInput onSubmit={onSubmit} ref={ref} />
<SearchInput onSubmit={onSubmit} onClear={onClear} ref={ref} />
<Popover>
<ListBox>{children}</ListBox>
</Popover>
Expand Down
12 changes: 10 additions & 2 deletions packages/components/src/Autocomplete/ClearButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,24 @@ import { Button } from '../Button';

export interface ClearButtonProps {
className?: string;
onClear?: () => void;
}

export const AutocompleteClearButton = ({ className }: ClearButtonProps) => {
export const AutocompleteClearButton = ({
className,
onClear,
}: ClearButtonProps) => {
let state = React.useContext(ComboBoxStateContext);

return (
<Button
// Don't inherit default Button behavior from ComboBox.
aria-label="Clear"
onPress={() => state?.setInputValue('')}
data-testid="clear-button"
onPress={() => {
state?.setInputValue('');
onClear?.();
}}
variant="icon"
className={cn(
'cursor-pointer appearance-none border-none p-0 pr-1',
Expand Down

0 comments on commit 7e3aa28

Please sign in to comment.