Skip to content

Commit c288e79

Browse files
feat: add hideClearButton prop to ComboBox (#593)
1 parent 840f540 commit c288e79

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

src/components/experimental/ComboBox/ComboBox.spec.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,26 @@ describe('ComboBox', () => {
3939
});
4040
});
4141

42+
it('hides clear button when hideClearButton is true', () => {
43+
render(
44+
<ComboBox label="Star Wars Character" items={mockItems} hideClearButton inputValue="Luke">
45+
{item => <ListBoxItem>{item.name}</ListBoxItem>}
46+
</ComboBox>
47+
);
48+
49+
expect(screen.queryByRole('button', { name: 'Clear field' })).not.toBeInTheDocument();
50+
});
51+
52+
it('shows clear button by default when input has value', () => {
53+
render(
54+
<ComboBox label="Star Wars Character" items={mockItems} inputValue="Luke">
55+
{item => <ListBoxItem>{item.name}</ListBoxItem>}
56+
</ComboBox>
57+
);
58+
59+
expect(screen.getByRole('button', { name: 'Clear field' })).toBeInTheDocument();
60+
});
61+
4262
it('calls onSelectionChange when an item is selected', async () => {
4363
const onSelectionChange = jest.fn();
4464
render(

src/components/experimental/ComboBox/ComboBox.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const defaultAriaStrings = {
3030
interface ComboBoxFieldProps extends Pick<FieldProps, 'description' | 'errorMessage' | 'leadingIcon'> {
3131
label: string;
3232
placeholder?: string;
33+
hideClearButton?: boolean;
3334
/**
3435
* If your project supports multiple languages,
3536
* it is recommended to pass translated labels to these properties
@@ -50,7 +51,7 @@ interface ComboBoxProps<T extends Record<string, unknown>>
5051
}
5152

5253
const ComboBoxInput = React.forwardRef<HTMLDivElement, ComboBoxFieldProps>(
53-
({ label, placeholder, leadingIcon, ariaStrings, inputRef: externalInputRef }, forwardedRef) => {
54+
({ label, placeholder, leadingIcon, ariaStrings, hideClearButton, inputRef: externalInputRef }, forwardedRef) => {
5455
const state = React.useContext(ComboBoxStateContext);
5556
const internalInputRef = React.useRef<HTMLInputElement>(null);
5657

@@ -67,7 +68,7 @@ const ComboBoxInput = React.forwardRef<HTMLDivElement, ComboBoxFieldProps>(
6768
<Label $flying={Boolean(placeholder || state?.inputValue?.length > 0)}>{label}</Label>
6869
<Input placeholder={placeholder} ref={combinedInputRef} />
6970
</InnerWrapper>
70-
{state?.inputValue?.length > 0 ? (
71+
{!hideClearButton && state?.inputValue?.length > 0 ? (
7172
<Button
7273
// Don't inherit default Button behavior from ComboBox.
7374
slot={null}
@@ -79,7 +80,7 @@ const ComboBoxInput = React.forwardRef<HTMLDivElement, ComboBoxFieldProps>(
7980
>
8081
<XCrossCircleIcon />
8182
</Button>
82-
) : (
83+
) : state?.inputValue?.length > 0 ? null : (
8384
<VisuallyHidden aria-live="polite">{ariaStrings.messageFieldIsCleared}</VisuallyHidden>
8485
)}
8586
</FakeInput>
@@ -96,6 +97,7 @@ function ComboBoxComponent<T extends Record<string, unknown>>(
9697
children,
9798
placeholder,
9899
leadingIcon,
100+
hideClearButton,
99101
ariaStrings = defaultAriaStrings,
100102
errorMessage,
101103
description,
@@ -130,6 +132,7 @@ function ComboBoxComponent<T extends Record<string, unknown>>(
130132
label={label}
131133
placeholder={placeholder}
132134
leadingIcon={leadingIcon}
135+
hideClearButton={hideClearButton}
133136
ariaStrings={ariaStrings}
134137
/>
135138
<Footer>{isInvalid ? <FieldError>{errorMessage}</FieldError> : description}</Footer>

0 commit comments

Comments
 (0)