-
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 field components to simplify the creation of forms with final-form
- Loading branch information
1 parent
3271c05
commit 3e15b81
Showing
17 changed files
with
317 additions
and
22 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,60 @@ | ||
--- | ||
"@comet/admin-color-picker": minor | ||
"@comet/admin-date-time": minor | ||
"@comet/admin": minor | ||
--- | ||
|
||
Add field components to simplify the creation of forms with final-form. | ||
|
||
- TextField | ||
- TextAreaField | ||
- SearchField | ||
- SelectField | ||
- CheckboxField | ||
- SwitchField | ||
- ColorField | ||
- DateField | ||
- DateRangeField | ||
- TimeField | ||
- TimeRangeField | ||
- DateTimeField | ||
|
||
**Example with TextField** | ||
|
||
```tsx | ||
// You can now do: | ||
<TextField name="text" label="Text" /> | ||
``` | ||
|
||
```tsx | ||
// Instead of: | ||
<Field name="text" label="Text" component={FinalFormInput} /> | ||
``` | ||
|
||
**Example with SelectField** | ||
|
||
```tsx | ||
// You can now do: | ||
<SelectField name="select" label="Select"> | ||
{options.map((option) => ( | ||
<MenuItem key={option.value} value={option.value}> | ||
{option.label} | ||
</MenuItem> | ||
))} | ||
</SelectField> | ||
``` | ||
|
||
```tsx | ||
// Instead of: | ||
<Field name="select" label="Select"> | ||
{(props) => ( | ||
<FinalFormSelect {...props}> | ||
{options.map((option) => ( | ||
<MenuItem key={option.value} value={option.value}> | ||
{option.label} | ||
</MenuItem> | ||
))} | ||
</FinalFormSelect> | ||
)} | ||
</Field> | ||
``` |
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,10 @@ | ||
import { Field, FieldProps } from "@comet/admin"; | ||
import * as React from "react"; | ||
|
||
import { FinalFormColorPicker } from "./FinalFormColorPicker"; | ||
|
||
export type ColorFieldProps = FieldProps<string, HTMLInputElement>; | ||
|
||
export const ColorField = ({ ...restProps }: ColorFieldProps): React.ReactElement => { | ||
return <Field component={FinalFormColorPicker} {...restProps} />; | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { ColorField, ColorFieldProps } from "./ColorField"; | ||
export { | ||
ColorPicker, | ||
ColorPickerColorPreviewProps, | ||
|
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,10 @@ | ||
import { Field, FieldProps } from "@comet/admin"; | ||
import * as React from "react"; | ||
|
||
import { FinalFormDatePicker } from "../FinalFormDatePicker"; | ||
|
||
export type DateFieldProps = FieldProps<Date, HTMLInputElement>; | ||
|
||
export const DateField = ({ ...restProps }: DateFieldProps): React.ReactElement => { | ||
return <Field component={FinalFormDatePicker} {...restProps} />; | ||
}; |
11 changes: 11 additions & 0 deletions
11
packages/admin/admin-date-time/src/fields/DateRangeField.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,11 @@ | ||
import { Field, FieldProps } from "@comet/admin"; | ||
import * as React from "react"; | ||
|
||
import { DateRange } from "../DateRangePicker"; | ||
import { FinalFormDateRangePicker } from "../FinalFormDateRangePicker"; | ||
|
||
export type DateRangeFieldProps = FieldProps<DateRange, HTMLInputElement>; | ||
|
||
export const DateRangeField = ({ ...restProps }: DateRangeFieldProps): React.ReactElement => { | ||
return <Field component={FinalFormDateRangePicker} {...restProps} />; | ||
}; |
10 changes: 10 additions & 0 deletions
10
packages/admin/admin-date-time/src/fields/DateTimeField.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,10 @@ | ||
import { Field, FieldProps } from "@comet/admin"; | ||
import * as React from "react"; | ||
|
||
import { FinalFormDateTimePicker } from "../FinalFormDateTimePicker"; | ||
|
||
export type DateTimeFieldProps = FieldProps<Date, HTMLInputElement>; | ||
|
||
export const DateTimeField = ({ ...restProps }: DateTimeFieldProps): React.ReactElement => { | ||
return <Field component={FinalFormDateTimePicker} {...restProps} />; | ||
}; |
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,10 @@ | ||
import { Field, FieldProps } from "@comet/admin"; | ||
import * as React from "react"; | ||
|
||
import { FinalFormTimePicker } from "../FinalFormTimePicker"; | ||
|
||
export type TimeFieldProps = FieldProps<string, HTMLInputElement>; | ||
|
||
export const TimeField = ({ ...restProps }: TimeFieldProps): React.ReactElement => { | ||
return <Field component={FinalFormTimePicker} {...restProps} />; | ||
}; |
11 changes: 11 additions & 0 deletions
11
packages/admin/admin-date-time/src/fields/TimeRangeField.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,11 @@ | ||
import { Field, FieldProps } from "@comet/admin"; | ||
import * as React from "react"; | ||
|
||
import { FinalFormTimeRangePicker } from "../FinalFormTimeRangePicker"; | ||
import { TimeRange } from "../TimeRangePicker"; | ||
|
||
export type TimeRangeFieldProps = FieldProps<TimeRange, HTMLInputElement>; | ||
|
||
export const TimeRangeField = ({ ...restProps }: TimeRangeFieldProps): React.ReactElement => { | ||
return <Field component={FinalFormTimeRangePicker} {...restProps} />; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { FormControlLabel, FormControlLabelProps } from "@mui/material"; | ||
import * as React from "react"; | ||
|
||
import { FinalFormCheckbox, FinalFormCheckboxProps } from "../Checkbox"; | ||
import { Field, FieldProps } from "../Field"; | ||
|
||
export interface CheckboxFieldProps extends FieldProps<string, HTMLInputElement> { | ||
fieldLabel?: string; | ||
componentsProps?: { | ||
formControlLabel?: FormControlLabelProps; | ||
finalFormCheckbox?: FinalFormCheckboxProps; | ||
}; | ||
} | ||
|
||
export const CheckboxField = ({ fieldLabel, label, componentsProps = {}, ...restProps }: CheckboxFieldProps): React.ReactElement => { | ||
const { formControlLabel: formControlLabelProps, finalFormCheckbox: finalFormCheckboxProps } = componentsProps; | ||
return ( | ||
<Field type="checkbox" label={fieldLabel} {...restProps}> | ||
{(props) => ( | ||
<FormControlLabel label={label} control={<FinalFormCheckbox {...props} {...finalFormCheckboxProps} />} {...formControlLabelProps} /> | ||
)} | ||
</Field> | ||
); | ||
}; |
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,10 @@ | ||
import * as React from "react"; | ||
|
||
import { Field, FieldProps } from "../Field"; | ||
import { FinalFormSearchTextField } from "../FinalFormSearchTextField"; | ||
|
||
export type SearchFieldProps = FieldProps<string, HTMLInputElement>; | ||
|
||
export const SearchField = ({ ...restProps }: SearchFieldProps): React.ReactElement => { | ||
return <Field component={FinalFormSearchTextField} {...restProps} />; | ||
}; |
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,31 @@ | ||
import React from "react"; | ||
|
||
import { Field, FieldProps } from "../Field"; | ||
import { FinalFormSelect, FinalFormSelectProps } from "../FinalFormSelect"; | ||
|
||
type SelectFieldPropsToExtendFrom<Value extends string | number> = FieldProps<Value, HTMLSelectElement>; | ||
|
||
// Remove `children` from the interface. Omit cannot be used here because `FieldProps` contains an index signature. | ||
type SelectFieldPropsToExtendFromWithoutChildren<Value extends string | number> = { | ||
[K in keyof SelectFieldPropsToExtendFrom<Value> as K extends "children" ? never : K]: SelectFieldPropsToExtendFrom<Value>[K]; | ||
}; | ||
|
||
export interface SelectFieldProps<Value extends string | number> extends SelectFieldPropsToExtendFromWithoutChildren<Value> { | ||
children: ReturnType<Required<SelectFieldPropsToExtendFrom<Value>>["children"]>; | ||
componentsProps?: { | ||
finalFormSelect?: FinalFormSelectProps<Value>; | ||
}; | ||
} | ||
|
||
export function SelectField<Value extends string | number>({ componentsProps = {}, children, ...restProps }: SelectFieldProps<Value>) { | ||
const { finalFormSelect: finalFormSelectProps } = componentsProps; | ||
return ( | ||
<Field {...restProps}> | ||
{(props) => ( | ||
<FinalFormSelect<Value> {...props} {...finalFormSelectProps}> | ||
{children} | ||
</FinalFormSelect> | ||
)} | ||
</Field> | ||
); | ||
} |
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,24 @@ | ||
import { FormControlLabel, FormControlLabelProps } from "@mui/material"; | ||
import * as React from "react"; | ||
|
||
import { Field, FieldProps } from "../Field"; | ||
import { FinalFormSwitch, FinalFormSwitchProps } from "../Switch"; | ||
|
||
export interface SwitchFieldProps extends FieldProps<string, HTMLInputElement> { | ||
fieldLabel?: string; | ||
componentsProps?: { | ||
formControlLabel?: FormControlLabelProps; | ||
finalFormSwitch?: FinalFormSwitchProps; | ||
}; | ||
} | ||
|
||
export const SwitchField = ({ fieldLabel, label, componentsProps = {}, ...restProps }: SwitchFieldProps): React.ReactElement => { | ||
const { formControlLabel: formControlLabelProps, finalFormSwitch: finalFormSwitchProps } = componentsProps; | ||
return ( | ||
<Field type="checkbox" label={fieldLabel} {...restProps}> | ||
{(props) => ( | ||
<FormControlLabel label={label} control={<FinalFormSwitch {...props} {...finalFormSwitchProps} />} {...formControlLabelProps} /> | ||
)} | ||
</Field> | ||
); | ||
}; |
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,10 @@ | ||
import * as React from "react"; | ||
|
||
import { Field, FieldProps } from "../Field"; | ||
import { FinalFormInput } from "../FinalFormInput"; | ||
|
||
export type TextAreaFieldProps = FieldProps<string, HTMLTextAreaElement>; | ||
|
||
export const TextAreaField = ({ ...restProps }: TextAreaFieldProps): React.ReactElement => { | ||
return <Field type="textarea" multiline rows={3} component={FinalFormInput} {...restProps} />; | ||
}; |
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,10 @@ | ||
import * as React from "react"; | ||
|
||
import { Field, FieldProps } from "../Field"; | ||
import { FinalFormInput } from "../FinalFormInput"; | ||
|
||
export type TextFieldProps = FieldProps<string, HTMLInputElement>; | ||
|
||
export const TextField = ({ ...restProps }: TextFieldProps): React.ReactElement => { | ||
return <Field component={FinalFormInput} {...restProps} />; | ||
}; |
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