-
If you have simple form, it is easy to write
When using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I'm not sure what your X and Y are supposed to represent, but let's pretend it's a restaurant. What I would generally do is create a "Shape" which a Custom Field understands, let's say your shape is: I'm using TypeScript, though it will work fine without it. TS allows me to explain the shapes better interface StoreHours = {
normal: StoreHoursMap,
holiday: StoreHoursMap
}
type StoreHoursMap = Record<number, Hours>;
interface Hours {
times: {
from: string,
to: string,
}
}; Now you have a number of different types. These can be broken up in a million different ways depending on how reusable you want the fields to be. For example: interface TimeFieldProps {
name: string,
// etc
}
const TimeField = (props: TimeFieldProps) => {
<Field {...props} />
}
interface HoursFieldProps {
name: string,
}
const HoursField = (props: HoursFieldProps) => {
return <>
<TimeField name={`${props.name}.from`} />
<TimeField name={`${props.name}.to`} />
</>
}
interface WeeklyHoursFieldProps {
name: string;
}
const WeeklyHoursField = (props: WeeklyHoursFieldProps) => {
return <>
<HoursField name={`${props.name}.0`} label="Sunday" />
<HoursField name={`${props.name}.1`} label="Monday" />
<HoursField name={`${props.name}.2`} label="Tuesday" />
<HoursField name={`${props.name}.3`} label="Wednesday" />
<HoursField name={`${props.name}.4`} label="Thursday" />
<HoursField name={`${props.name}.5`} label="Friday" />
<HoursField name={`${props.name}.6`} label="Saturday" />
</>
}
interface StoreHoursFieldProps {
name: string;
}
const StoreHoursField = (props: StoreHoursFieldProps) => {
return <>
<WeeklyHoursField name={`${props.name}.normal`} label="Normal Hours" />
<WeeklyHoursField name={`${props.name}.holiday`} label="Holiday Hours" />
</>
}
const MyForm = () => {
return <Formik {...formikProps}>
<StoreHoursField name="openingHours" />
<StoreHoursField name="dinnerHours" />
<StoreHoursField name="lunchHours" />
<StoreHoursField name="brunchHours" />
</Formik>
} Seems like a lot of work for one field, until you realize you've done the work already when you need a TimeField for Reservations, for example. For accessibility you'll want to add fieldsets and make sure you're using |
Beta Was this translation helpful? Give feedback.
I'm not sure what your X and Y are supposed to represent, but let's pretend it's a restaurant. What I would generally do is create a "Shape" which a Custom Field understands, let's say your shape is:
I'm using TypeScript, though it will work fine without it. TS allows me to explain the shapes better
Now you have a number of different types. These can be broken up in a million different ways depending on how reusable you want the fields to be. For example: