Skip to content
Open
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 @@ -114,7 +114,7 @@ export const _ControlledAsyncAutoCompleteMultiple: StoryObj<typeof ControlledAsy
name: 'controlledAsyncAutocomplete',
FieldProps: { label: 'Async Select', helperText: 'Helper Text', fullWidth: false, required: true },
getOptionLabel: (val: Option) => val.label,
isOptionEqualToValue: (option, value) => option.label === value.label,
isOptionEqualToValue: (option: Option, value: Option) => option.label === value.label,
loadOptions,
limit: 5,
queryKey: 'example',
Expand Down Expand Up @@ -165,5 +165,8 @@ export const _ControlledAsyncAutoCompleteDefaultToOnlyOption: StoryObj<typeof Co
rules: { required: 'This is required.' },
defaultToOnlyOption: true,
disableClearable: true,
onChange: (value) => {
console.log(value);
},
},
};
23 changes: 13 additions & 10 deletions packages/controlled-form/src/lib/AsyncAutocomplete.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fireEvent, render, waitFor } from '@testing-library/react';
import { fireEvent, render, screen, waitFor, act } from '@testing-library/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import AvApi, { ApiConfig } from '@availity/api-axios';
// eslint-disable-next-line @nx/enforce-module-boundaries
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('ControlledAsyncAutocomplete', () => {
});

test('should loadOptions successfully', async () => {
const screen = render(
render(
<QueryClientProvider client={client}>
<TestForm UseFormOptions={{ values: { controlledAutocomplete: undefined } }} onSubmit={onSubmit}>
<ControlledAsyncAutocomplete
Expand All @@ -82,14 +82,17 @@ describe('ControlledAsyncAutocomplete', () => {
);

const dropdown = screen.getByRole('combobox');
fireEvent.click(dropdown);
fireEvent.keyDown(dropdown, { key: 'ArrowDown' });

await waitFor(() => expect(screen.getByText('Option 1')).toBeDefined());
act(() => {
fireEvent.click(dropdown);
fireEvent.keyDown(dropdown, { key: 'ArrowDown' });
});

await waitFor(() => expect(screen.getByText('Option 0')).toBeDefined());
});

test('should set the value and submit the form data', async () => {
const screen = render(
render(
<QueryClientProvider client={client}>
<TestForm UseFormOptions={{ values: { controlledAutocomplete: undefined } }} onSubmit={onSubmit}>
<ControlledAsyncAutocomplete
Expand Down Expand Up @@ -127,7 +130,7 @@ describe('ControlledAsyncAutocomplete', () => {
describe('when using rules', () => {
describe('when required', () => {
test('should indicate it is required when passing a string', async () => {
const screen = render(
render(
<QueryClientProvider client={client}>
<TestForm UseFormOptions={{ values: { controlledAutocomplete: undefined } }} onSubmit={onSubmit}>
<ControlledAsyncAutocomplete
Expand All @@ -147,7 +150,7 @@ describe('ControlledAsyncAutocomplete', () => {
});

test('should indicate it is required when passing an object with true', async () => {
const screen = render(
render(
<QueryClientProvider client={client}>
<TestForm UseFormOptions={{ values: { controlledAutocomplete: undefined } }} onSubmit={onSubmit}>
<ControlledAsyncAutocomplete
Expand All @@ -167,7 +170,7 @@ describe('ControlledAsyncAutocomplete', () => {
});

test('should not indicate it is required when passing an object with false', async () => {
const screen = render(
render(
<QueryClientProvider client={client}>
<TestForm UseFormOptions={{ values: { controlledAutocomplete: undefined } }} onSubmit={onSubmit}>
<ControlledAsyncAutocomplete
Expand All @@ -187,4 +190,4 @@ describe('ControlledAsyncAutocomplete', () => {
});
});
});
});
});
11 changes: 6 additions & 5 deletions packages/controlled-form/src/lib/AsyncAutocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ export const ControlledAsyncAutocomplete = <
loadOptions={async (offset, limit, inputValue) => {
const { options, hasMore, offset: returnedOffsetValue } = await rest.loadOptions(offset, limit, inputValue);

if (defaultToFirstOption && offset === 0) {
setValue(name, rest.multiple ? [options[0]] : options[0]);
}
const shouldAssignValue =
(defaultToFirstOption && offset === 0) || (defaultToOnlyOption && offset === 0 && options.length === 1);

if (defaultToOnlyOption && offset === 0 && options.length === 1) {
setValue(name, rest.multiple ? [options[0]] : options[0]);
if (shouldAssignValue) {
const newValue = rest.multiple ? [options[0]] : options[0];
setValue(name, newValue);
onChange(newValue);
}

return { options, hasMore, offset: returnedOffsetValue };
Expand Down
Loading