Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react-components: Shown clear button only on autocomplete hover/focus #165

Merged
merged 4 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ exports[`<Autocomplete /> should render with default styles 1`] = `
border-color: var(--pv-color-gray-7);
}

.emotion-0:hover [aria-label="Clear"] {
visibility: visible;
}

.emotion-0[aria-placeholder] {
color: var(--pv-color-gray-9);
}
Expand All @@ -63,11 +67,19 @@ exports[`<Autocomplete /> should render with default styles 1`] = `
border-color: var(--pv-color-secondary-tint-3);
}

.emotion-0:focus-visible [aria-label="Clear"] {
visibility: visible;
}

.emotion-0:focus-within {
background-color: var(--pv-color-secondary-tint-5);
border-color: var(--pv-color-secondary-tint-3);
}

.emotion-0:focus-within [aria-label="Clear"] {
visibility: visible;
}

.emotion-1 {
margin: 0;
color: var(--pv-color-black);
Expand Down Expand Up @@ -175,12 +187,12 @@ exports[`<Autocomplete /> should render with default styles 1`] = `
>
<svg
aria-disabled="false"
aria-hidden="true"
aria-label="Open"
class="emotion-3"
fill="none"
height="24px"
role="button"
title="open"
title="Open"
viewBox="0 0 24 24"
width="24px"
xmlns="http://www.w3.org/2000/svg"
Expand Down Expand Up @@ -254,6 +266,10 @@ exports[`<Autocomplete /> should render with multiple selection enabled 1`] = `
border-color: var(--pv-color-gray-7);
}

.emotion-0:hover [aria-label="Clear"] {
visibility: visible;
}

.emotion-0[aria-placeholder] {
color: var(--pv-color-gray-9);
}
Expand All @@ -268,11 +284,19 @@ exports[`<Autocomplete /> should render with multiple selection enabled 1`] = `
border-color: var(--pv-color-secondary-tint-3);
}

.emotion-0:focus-visible [aria-label="Clear"] {
visibility: visible;
}

.emotion-0:focus-within {
background-color: var(--pv-color-secondary-tint-5);
border-color: var(--pv-color-secondary-tint-3);
}

.emotion-0:focus-within [aria-label="Clear"] {
visibility: visible;
}

.emotion-1 {
margin: 0;
color: var(--pv-color-black);
Expand Down Expand Up @@ -380,12 +404,12 @@ exports[`<Autocomplete /> should render with multiple selection enabled 1`] = `
>
<svg
aria-disabled="false"
aria-hidden="true"
aria-label="Open"
class="emotion-3"
fill="none"
height="24px"
role="button"
title="open"
title="Open"
viewBox="0 0 24 24"
width="24px"
xmlns="http://www.w3.org/2000/svg"
Expand Down Expand Up @@ -459,6 +483,10 @@ exports[`<Autocomplete /> sizes renders with size "large" 1`] = `
border-color: var(--pv-color-gray-7);
}

.emotion-0:hover [aria-label="Clear"] {
visibility: visible;
}

.emotion-0[aria-placeholder] {
color: var(--pv-color-gray-9);
}
Expand All @@ -473,11 +501,19 @@ exports[`<Autocomplete /> sizes renders with size "large" 1`] = `
border-color: var(--pv-color-secondary-tint-3);
}

.emotion-0:focus-visible [aria-label="Clear"] {
visibility: visible;
}

.emotion-0:focus-within {
background-color: var(--pv-color-secondary-tint-5);
border-color: var(--pv-color-secondary-tint-3);
}

.emotion-0:focus-within [aria-label="Clear"] {
visibility: visible;
}

.emotion-1 {
margin: 0;
color: var(--pv-color-black);
Expand Down Expand Up @@ -585,12 +621,12 @@ exports[`<Autocomplete /> sizes renders with size "large" 1`] = `
>
<svg
aria-disabled="false"
aria-hidden="true"
aria-label="Open"
class="emotion-3"
fill="none"
height="24px"
role="button"
title="open"
title="Open"
viewBox="0 0 24 24"
width="24px"
xmlns="http://www.w3.org/2000/svg"
Expand Down Expand Up @@ -664,6 +700,10 @@ exports[`<Autocomplete /> sizes renders with size "medium" 1`] = `
border-color: var(--pv-color-gray-7);
}

.emotion-0:hover [aria-label="Clear"] {
visibility: visible;
}

.emotion-0[aria-placeholder] {
color: var(--pv-color-gray-9);
}
Expand All @@ -678,11 +718,19 @@ exports[`<Autocomplete /> sizes renders with size "medium" 1`] = `
border-color: var(--pv-color-secondary-tint-3);
}

.emotion-0:focus-visible [aria-label="Clear"] {
visibility: visible;
}

.emotion-0:focus-within {
background-color: var(--pv-color-secondary-tint-5);
border-color: var(--pv-color-secondary-tint-3);
}

.emotion-0:focus-within [aria-label="Clear"] {
visibility: visible;
}

.emotion-1 {
margin: 0;
color: var(--pv-color-black);
Expand Down Expand Up @@ -790,12 +838,12 @@ exports[`<Autocomplete /> sizes renders with size "medium" 1`] = `
>
<svg
aria-disabled="false"
aria-hidden="true"
aria-label="Open"
class="emotion-3"
fill="none"
height="24px"
role="button"
title="open"
title="Open"
viewBox="0 0 24 24"
width="24px"
xmlns="http://www.w3.org/2000/svg"
Expand Down Expand Up @@ -869,6 +917,10 @@ exports[`<Autocomplete /> sizes renders with size "small" 1`] = `
border-color: var(--pv-color-gray-7);
}

.emotion-0:hover [aria-label="Clear"] {
visibility: visible;
}

.emotion-0[aria-placeholder] {
color: var(--pv-color-gray-9);
}
Expand All @@ -883,11 +935,19 @@ exports[`<Autocomplete /> sizes renders with size "small" 1`] = `
border-color: var(--pv-color-secondary-tint-3);
}

.emotion-0:focus-visible [aria-label="Clear"] {
visibility: visible;
}

.emotion-0:focus-within {
background-color: var(--pv-color-secondary-tint-5);
border-color: var(--pv-color-secondary-tint-3);
}

.emotion-0:focus-within [aria-label="Clear"] {
visibility: visible;
}

.emotion-1 {
margin: 0;
color: var(--pv-color-black);
Expand Down Expand Up @@ -995,12 +1055,12 @@ exports[`<Autocomplete /> sizes renders with size "small" 1`] = `
>
<svg
aria-disabled="false"
aria-hidden="true"
aria-label="Open"
class="emotion-3"
fill="none"
height="24px"
role="button"
title="open"
title="Open"
viewBox="0 0 24 24"
width="24px"
xmlns="http://www.w3.org/2000/svg"
Expand Down
17 changes: 10 additions & 7 deletions packages/react-components/src/Autocomplete/autocomplete.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
fireEvent,
act,
waitFor,
userEvent,
} from '../test-utils';
import { Autocomplete } from './index';

Expand Down Expand Up @@ -156,11 +157,11 @@ describe('<Autocomplete />', () => {

expect(screen.getByDisplayValue('clearable-option')).toBeInTheDocument();

await userEvent.hover(screen.getByRole('combobox'));

await act(async () => {
fireEvent.click(screen.getByRole('combobox'));
fireEvent.click(screen.getByTitle('Clear'));
});

fireEvent.click(screen.getByRole('button', { name: /clear/i }));
expect(screen.queryByDisplayValue('clearable-option')).not.toBeInTheDocument();
});

Expand Down Expand Up @@ -239,8 +240,10 @@ describe('<Autocomplete />', () => {

expect(screen.getByDisplayValue('option-1')).toBeInTheDocument();

await userEvent.hover(screen.getByRole('combobox'));

await act(async () => {
fireEvent.click(screen.getByRole('button', { name: /clear/i }));
fireEvent.click(screen.getByTitle('Clear'));
});

expect(screen.queryByDisplayValue('option-1')).not.toBeInTheDocument();
Expand Down Expand Up @@ -301,11 +304,11 @@ describe('<Autocomplete />', () => {
fireEvent.click(screen.getByText('option-1'));
fireEvent.click(screen.getByText('option-2'));

await userEvent.hover(screen.getByRole('combobox'));

await act(async () => {
fireEvent.click(screen.getByRole('combobox'));
fireEvent.click(screen.getByTitle('Clear'));
});

fireEvent.click(screen.getByRole('button', { name: /clear/i }));
expect(screen.queryByText('option-1')).not.toBeInTheDocument();
expect(screen.queryByText('option-2')).not.toBeInTheDocument();
});
Expand Down
51 changes: 32 additions & 19 deletions packages/react-components/src/Autocomplete/autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ TypographyOwnProps
'&:hover': {
backgroundColor: 'var(--pv-color-gray-3)',
borderColor: borderColorHover,
'[aria-label="Clear"]': {
visibility: 'visible',
},
},
'&[aria-placeholder]': {
color: colorPlaceholder,
Expand All @@ -220,10 +223,16 @@ TypographyOwnProps
'&:focus-visible': {
backgroundColor: backgroundColorFocus,
borderColor: borderColorFocus,
'[aria-label="Clear"]': {
visibility: 'visible',
},
},
'&:focus-within': {
backgroundColor: backgroundColorFocus,
borderColor: borderColorFocus,
'[aria-label="Clear"]': {
visibility: 'visible',
},
},
}),
});
Expand All @@ -239,16 +248,19 @@ const AutocompleteActions = styled('div')({
margin: '0px var(--pv-size-base)',
});

const AutocompleteRemoveIcon = styled(CloseSmallIcon)({
const AutocompleteClearButton = styled('button')({
color: 'var(--pv-color-gray-10)',
cursor: 'pointer',
'&[aria-disabled="true"]': {
color: 'inherit',
pointerEvents: 'none',
},
visibility: 'hidden',
display: 'inline-flex',
appearance: 'none',
outline: '0px',
padding: '0px',
borderWidth: '0px',
background: 'transparent',
});

const AutocompleteArrowIcon = styled(ArrowDropDownIcon)<{ open: boolean }>({
const AutocompleteOpenButton = styled(ArrowDropDownIcon)<{ open: boolean }>({
color: 'var(--pv-color-gray-10)',
'&[aria-disabled="true"]': {
color: 'inherit',
Expand Down Expand Up @@ -445,9 +457,6 @@ export const Autocomplete = <
onChange,
...otherInputProps
} = getInputProps();
const {
onClick,
} = getClearProps();

const rootProps = getRootProps();
const popoverProps = getPopoverProps();
Expand Down Expand Up @@ -608,19 +617,23 @@ export const Autocomplete = <
)}
<AutocompleteActions>
{!isValueEmpty && !readOnly ? (
<AutocompleteRemoveIcon
role="button"
title="clear"
aria-disabled={disabled}
// @ts-ignore
onClick={onClick}
/>
<AutocompleteClearButton
type="button"
disabled={disabled}
title="Clear"
aria-label="Clear"
{...getClearProps()}
>
<CloseSmallIcon
aria-hidden
/>
</AutocompleteClearButton>
) : null}
<AutocompleteArrowIcon
<AutocompleteOpenButton
role="button"
title="open"
title="Open"
aria-label="Open"
aria-disabled={disabled}
aria-hidden
open={popoverProps.open}
/>
</AutocompleteActions>
Expand Down
Loading