-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
FinalFormAsyncSelect
, AsyncSelectField
, and `FinalFormAsyncAu…
…tocomplete` components (#2069) Thin wrappers to ease using `useAsyncOptionsProps()` with `FinalFormSelect` and `FinalFormAutocomplete`. **Example** Previously: ```tsx const asyncOptionsProps = useAsyncOptionsProps(async () => { // Load options here }); // ... <Field component={FinalFormAsyncAutocomplete} {...asyncOptionsProps} />; ``` Now: ```tsx <Field component={FinalFormAsyncAutocomplete} loadOptions={async () => { // Load options here }} /> ```
- Loading branch information
1 parent
3ee8c7a
commit 16ffa7b
Showing
8 changed files
with
105 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
"@comet/admin": minor | ||
--- | ||
|
||
Add `FinalFormAsyncSelect`, `AsyncSelectField`, and `FinalFormAsyncAutocomplete` components | ||
|
||
Thin wrappers to ease using `useAsyncOptionsProps()` with `FinalFormSelect` and `FinalFormAutocomplete`. | ||
|
||
**Example** | ||
|
||
Previously: | ||
|
||
```tsx | ||
const asyncOptionsProps = useAsyncOptionsProps(async () => { | ||
// Load options here | ||
}); | ||
|
||
// ... | ||
|
||
<Field component={FinalFormAsyncAutocomplete} {...asyncOptionsProps} />; | ||
``` | ||
|
||
Now: | ||
|
||
```tsx | ||
<Field | ||
component={FinalFormAsyncAutocomplete} | ||
loadOptions={async () => { | ||
// Load options here | ||
}} | ||
/> | ||
``` |
22 changes: 22 additions & 0 deletions
22
packages/admin/admin/src/form/FinalFormAsyncAutocomplete.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React from "react"; | ||
|
||
import { useAsyncOptionsProps } from "../hooks/useAsyncOptionsProps"; | ||
import FinalFormAutocomplete, { FinalFormAutocompleteProps } from "./Autocomplete"; | ||
|
||
export interface FinalFormAsyncAutocompleteProps< | ||
T extends Record<string, any>, | ||
Multiple extends boolean | undefined, | ||
DisableClearable extends boolean | undefined, | ||
FreeSolo extends boolean | undefined, | ||
> extends FinalFormAutocompleteProps<T, Multiple, DisableClearable, FreeSolo> { | ||
loadOptions: () => Promise<T[]>; | ||
} | ||
|
||
export function FinalFormAsyncAutocomplete< | ||
T extends Record<string, any>, | ||
Multiple extends boolean | undefined, | ||
DisableClearable extends boolean | undefined, | ||
FreeSolo extends boolean | undefined, | ||
>({ loadOptions, ...rest }: FinalFormAsyncAutocompleteProps<T, Multiple, DisableClearable, FreeSolo>) { | ||
return <FinalFormAutocomplete<T, Multiple, DisableClearable, FreeSolo> {...useAsyncOptionsProps(loadOptions)} {...rest} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { SelectProps } from "@mui/material"; | ||
import React from "react"; | ||
|
||
import { useAsyncOptionsProps } from "../hooks/useAsyncOptionsProps"; | ||
import { FinalFormSelect, FinalFormSelectProps } from "./FinalFormSelect"; | ||
|
||
export interface FinalFormAsyncSelectProps<T> extends FinalFormSelectProps<T>, Omit<SelectProps, "input"> { | ||
loadOptions: () => Promise<T[]>; | ||
} | ||
|
||
export function FinalFormAsyncSelect<T>({ loadOptions, ...rest }: FinalFormAsyncSelectProps<T>) { | ||
return <FinalFormSelect<T> {...useAsyncOptionsProps(loadOptions)} {...rest} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from "react"; | ||
|
||
import { Field, FieldProps } from "../Field"; | ||
import { FinalFormAsyncSelect } from "../FinalFormAsyncSelect"; | ||
|
||
export interface AsyncSelectFieldProps<Option> extends FieldProps<Option, HTMLSelectElement> { | ||
loadOptions: () => Promise<Option[]>; | ||
getOptionLabel?: (option: Option) => string; | ||
getOptionValue?: (option: Option) => string; | ||
clearable?: boolean; | ||
} | ||
|
||
export function AsyncSelectField<Option>(props: AsyncSelectFieldProps<Option>) { | ||
return <Field component={FinalFormAsyncSelect} {...props} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters