Skip to content

Commit

Permalink
Admin Generator (Future): Add FinalFormRangeInput to form generator (
Browse files Browse the repository at this point in the history
…#2612)

`FinalFormRangeInput` fields can now be generated in forms. The new
field type is `numberRange`. Data of the field is saved in this format:
`{ min: number, max: number }`

The names `min` and `max` are currently hardcoded, as they **have to**
be named this way. `FinalFormRangeInput` passes the values named like
this.
  • Loading branch information
andrearutrecht authored Oct 10, 2024
1 parent bc19fb1 commit 6f82b6e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions demo/admin/src/products/future/ProductForm.cometGen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const ProductForm: FormConfig<GQLProduct> = {
values: [{ value: "Cap", label: "great Cap" }, "Shirt", "Tie"],
},
{ type: "asyncSelect", name: "category", rootQuery: "productCategories" },
{ type: "numberRange", name: "priceRange", minValue: 25, maxValue: 500, disableSlider: true, startAdornment: "€" },
{
type: "optionalNestedFields",
name: "dimensions",
Expand Down
4 changes: 4 additions & 0 deletions demo/admin/src/products/future/generated/ProductForm.gql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export const productFormFragment = gql`
id
title
}
priceRange {
min
max
}
dimensions {
width
height
Expand Down
13 changes: 13 additions & 0 deletions demo/admin/src/products/future/generated/ProductForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
FinalForm,
FinalFormCheckbox,
FinalFormInput,
FinalFormRangeInput,
FinalFormSubmitEvent,
FinalFormSwitch,
Loading,
Expand Down Expand Up @@ -284,6 +285,18 @@ export function ProductForm({ id }: FormProps): React.ReactElement {
}}
getOptionLabel={(option) => option.title}
/>

<Field
variant="horizontal"
fullWidth
name="priceRange"
component={FinalFormRangeInput}
label={<FormattedMessage id="product.priceRange" defaultMessage="Price Range" />}
min={25}
max={500}
disableSlider
startAdornment={<InputAdornment position="start"></InputAdornment>}
/>
<Field
fullWidth
name="dimensionsEnabled"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ export function generateForm(
FinalForm,
FinalFormCheckbox,
FinalFormInput,
FinalFormRangeInput,
FinalFormSelect,
FinalFormSubmitEvent,
Loading,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,32 @@ export function generateFormField({
},
},
];
} else if (config.type === "numberRange") {
code = `
<Field
${required ? "required" : ""}
${config.readOnly ? readOnlyPropsWithLock : ""}
variant="horizontal"
fullWidth
name="${nameWithPrefix}"
component={FinalFormRangeInput}
label={${fieldLabel}}
min={${config.minValue}}
max={${config.maxValue}}
${config.disableSlider ? "disableSlider" : ""}
${config.startAdornment ? `startAdornment={<InputAdornment position="start">${config.startAdornment}</InputAdornment>}` : ""}
${config.endAdornment ? `endAdornment={<InputAdornment position="end">${config.endAdornment}</InputAdornment>}` : ""}
${
config.helperText
? `helperText={<FormattedMessage id=` +
`"${formattedMessageRootId}.${name}.helperText" ` +
`defaultMessage="${config.helperText}" />}`
: ""
}
${validateCode}
/>`;

formFragmentField = `${name} { min max }`;
} else if (config.type == "boolean") {
code = `<Field name="${nameWithPrefix}" label="" type="checkbox" variant="horizontal" fullWidth ${validateCode}>
{(props) => (
Expand Down
8 changes: 8 additions & 0 deletions packages/admin/cms-admin/src/generator/future/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ type MultiFileFormFieldConfig = { type: "fileUpload"; multiple: true; maxFiles?:
export type FormFieldConfig<T> = (
| { type: "text"; multiline?: boolean }
| { type: "number" }
| {
type: "numberRange";
minValue: number;
maxValue: number;
disableSlider?: boolean;
startAdornment?: string;
endAdornment?: string;
}
| { type: "boolean" }
| { type: "date" }
// TODO | { type: "dateTime" }
Expand Down

0 comments on commit 6f82b6e

Please sign in to comment.