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

feat: add AutocompleteField #255

Merged
merged 3 commits into from
Feb 27, 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
21 changes: 21 additions & 0 deletions src/Autocomplete/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Story } from '@storybook/react';
import React from 'react';

import { toFormInputOption } from '../Select';

import { AutocompleteProps, Autocomplete } from './index';

export default {
title: 'Autocomplete',
};

export const WithOptions: Story<AutocompleteProps> = function WithOptions(
args,
) {
return (
<Autocomplete
options={['food', 'foo', 'foobar', 'bar'].map(toFormInputOption)}
{...args}
/>
);
};
49 changes: 49 additions & 0 deletions src/Fields/AutocompleteField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from 'react';
import {
useController,
FieldValues,
UseControllerProps,
FieldPath,
} from 'react-hook-form';

import { Autocomplete, AutocompleteProps } from '../Autocomplete';

import { useFieldContext } from './FieldProvider';
import { FieldWrapper, FieldWrapperProps } from './FieldWrapper';

type AutocompleteFieldProps<
TFieldValues extends FieldValues,
TName extends FieldPath<TFieldValues>,
> = UseControllerProps<TFieldValues, TName> &
Pick<FieldWrapperProps<TFieldValues, TName>, 'formItem'> & {
component?: AutocompleteProps;
};

/**
* This component appears unstyled in Storybook because it is not properly prefixing its class names.
* However, if this library is used alongside globally included antd styles, it works somewhat fine.
* When we rip out antd 3 from our projects, we may want to remove the class prefix in this library to fix this.
*/
export function AutocompleteField<
TFieldValues extends FieldValues = FieldValues,
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
>({
formItem,
component,
...controller
}: AutocompleteFieldProps<TFieldValues, TName>) {
const { field } = useController<TFieldValues, TName>(controller);

const { disabled } = useFieldContext();

return (
<FieldWrapper controller={controller} formItem={formItem}>
<Autocomplete
{...field}
value={field.value ?? undefined}
disabled={disabled}
{...component}
/>
</FieldWrapper>
);
}
10 changes: 10 additions & 0 deletions src/Fields/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { TextAreaRef } from '../Input';
import { toFormInputOption } from '../Select';
import { Space } from '../Space';

import { AutocompleteField } from './AutocompleteField';
import { CheckboxField } from './CheckboxField';
import { FieldProvider, FieldProviderProps } from './FieldProvider';
import { InputField } from './InputField';
Expand All @@ -23,6 +24,7 @@ export default {
};

type FormType = {
autocomplete: string;
checkbox: boolean;
input: string;
input_number: number;
Expand Down Expand Up @@ -124,6 +126,14 @@ function AllFields() {
const formMethods = useFormContext<FormType>();
return (
<Form>
<AutocompleteField
name="autocomplete"
control={formMethods.control}
formItem={{ label: 'Autocomplete' }}
component={{
options: ['foo', 'bar'].map(toFormInputOption),
}}
/>
<CheckboxField
name="checkbox"
control={formMethods.control}
Expand Down
Loading