Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
feat(UIKIT-1108,Autocomplete): Popover скрыт, если пользователь ничег…
Browse files Browse the repository at this point in the history
…о не ввел, либо отсутствуют options (#1158)

Co-authored-by: Andrey Zaitsev <zaytsev_aa@astralnalog.ru>
  • Loading branch information
Cabagemage and Andrey Zaitsev authored Sep 23, 2024
1 parent 1274e15 commit 0b3defc
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 28 deletions.
36 changes: 23 additions & 13 deletions packages/components/src/Autocomplete/Autocomplete.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Meta, type StoryObj } from '@storybook/react';
import { useState } from 'react';
import { type SyntheticEvent, useState } from 'react';

import { Tooltip } from '../Tooltip';
import { Tag } from '../Tag';
Expand Down Expand Up @@ -219,18 +219,28 @@ export const Small = () => (
</Wrapper>
);

export const NoData = () => (
<Wrapper>
<Autocomplete<Option, true, false, false>
label="No data"
size="small"
multiple
options={[]}
getOptionLabel={(params) => params.title}
required
/>
</Wrapper>
);
export const NoData = () => {
const [inputValue, setInputValue] = useState('');

const handleInputValueChange = (_: SyntheticEvent, value: string) => {
setInputValue(value);
};

return (
<Wrapper>
<Autocomplete<Option, true, false, false>
label="No data"
size="small"
multiple
options={[]}
onInputChange={handleInputValueChange}
inputValue={inputValue}
getOptionLabel={(params) => params.title}
required
/>
</Wrapper>
);
};

export const CustomRenderOption = () => (
<Wrapper>
Expand Down
31 changes: 30 additions & 1 deletion packages/components/src/Autocomplete/Autocomplete.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,36 @@ import { TextField } from '../TextField';
import { Autocomplete } from './Autocomplete';

describe('Autocomplete', () => {
it('Popover не отображается пои фокусе на инпут', async () => {
it('Popover не отображается при клике на инпут, если inputValue пуст, а опций для выбора нет', async () => {
renderWithTheme(<Autocomplete options={[]} inputValue="" />);
await userEvents.click(screen.getByRole('combobox'));

const noDataPlaceholder = screen.queryByText('Нет данных');

expect(noDataPlaceholder).toBeNull();
});

it('Popover отображается при клике на инпут, если есть опции для выбора, но inputValue пуст', async () => {
type Option = { name: string; surname: string };

const options: Option[] = [{ name: 'Vasya', surname: 'Pupkin' }];

renderWithTheme(
<Autocomplete<Option, false, false, false>
options={options}
inputValue=""
getOptionLabel={(option) => option.surname}
/>,
);

await userEvents.click(screen.getByRole('combobox'));

const expectedOption = screen.getByText('Pupkin');

expect(expectedOption).toBeVisible();
});

it('Popover не отображается при фокусе на инпут', async () => {
renderWithTheme(<Autocomplete options={[]} />);
fireEvent.focus(screen.getByRole('combobox'));

Expand Down
35 changes: 21 additions & 14 deletions packages/components/src/Autocomplete/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,13 @@ const AutocompleteInner = <
>,
ref?: ForwardedRef<unknown>,
) => {
const isEmpty = checkIsInputEmpty(restProps.value);
const { inputValue, options, loading } = restProps;
const isInputValueNotEmpty =
inputValue !== undefined && inputValue.length >= 1;
const isOptionsAvailable = options.length > 0;
const isPopperVisible = isInputValueNotEmpty || isOptionsAvailable || loading;

const isEmpty = checkIsInputEmpty(restProps.value);
const disableClearable = isEmpty || Boolean(externalDisableClearable);

const renderDefaultTags = useCallback(
Expand Down Expand Up @@ -220,19 +225,21 @@ const AutocompleteInner = <
multiple={multiple}
getOptionLabel={getOptionLabel}
disableCloseOnSelect={multiple}
PopperComponent={({ children, ...rest }) => (
<MuiPopper {...rest}>
{isLoadedDataError ? (
<PopperWrapper>
<Typography variant="body1" color="grey" colorIntensity="600">
{loadedDataError}
</Typography>
</PopperWrapper>
) : (
children
)}
</MuiPopper>
)}
PopperComponent={({ children, ...rest }) => {
return isPopperVisible ? (
<MuiPopper {...rest}>
{isLoadedDataError ? (
<PopperWrapper>
<Typography variant="body1" color="grey" colorIntensity="600">
{loadedDataError}
</Typography>
</PopperWrapper>
) : (
children
)}
</MuiPopper>
) : null;
}}
renderTags={renderTags ?? renderDefaultTags}
renderInput={renderInput}
renderOption={renderOption}
Expand Down

0 comments on commit 0b3defc

Please sign in to comment.