I have an address field setup as a complex field. Within that field I have two select fields. 1 for the state/region and the other for the country code.
The issue I'm having is I cannot figure out how to pass options down to these fields. Not sure how to get the code below to appear right. md is not behaving with me today. Any idea how to pass the options down to each select field within the address schema?
this is my form.tsx
`
const mapping = [
[z.string(), TextInput],
[z.boolean(), Checkbox],
[z.number(), NumberInput],
[PasswordSchema, PasswordField] as const,
[PasswordWConfirmSchema, PasswordWConfirmInput] as const,
[AddressSchema, AddressInput] as const,
[SelectSchema, SelectField]
] as const;
const MyForm = createTsForm(mapping);
const SignUpSchema = z.object({
firstName: z.string().describe("First Name"),
lastName: z.string().describe("Last Name"),
email: z.string().email("Enter a real email please.").describe("Email"), // renders TextField
password: PasswordWConfirmSchema,
address: AddressSchema,
})
export function Form() {
function onSubmit(data: z.infer<typeof SignUpSchema>) {
// gets typesafe data when form is submitted
alert(JSON.stringify(data));
}
return (
<MyForm
schema={SignUpSchema}
onSubmit={onSubmit}
renderAfter={() => <button type="submit">Submit</button>}
props={{
address: {
}
}}
/>
);
}
I figured out a way around this by just creating props for the address function and passing them to each select manually created within the AddressInput Field. but would think there would be a way to allow use of the SelectField Component.
I have an address field setup as a complex field. Within that field I have two select fields. 1 for the state/region and the other for the country code.
The issue I'm having is I cannot figure out how to pass options down to these fields. Not sure how to get the code below to appear right. md is not behaving with me today. Any idea how to pass the options down to each select field within the address schema?
this is my form.tsx
` const mapping = [ [z.string(), TextInput], [z.boolean(), Checkbox], [z.number(), NumberInput], [PasswordSchema, PasswordField] as const, [PasswordWConfirmSchema, PasswordWConfirmInput] as const, [AddressSchema, AddressInput] as const, [SelectSchema, SelectField] ] as const;
`
My AddressInput.tsx
` const AddressInput = () => { const { field: { value, onChange }, error, } = useTsController<z.infer>();
const line1 = value?.line1;
const line2 = value?.line2;
const locale = value?.locale;
const region = value?.region;
const postalCode = value?.postalCode;
const countryCode = value?.countryCode;
return (
Select
function SelectField({ options }: { options: string[] }) { const { field, error } = useTsController}
export default SelectField
export const SelectSchema = createUniqueFieldSchema(z.string(), "DropdownSelect")`
I figured out a way around this by just creating props for the address function and passing them to each select manually created within the AddressInput Field. but would think there would be a way to allow use of the SelectField Component.
I think creating a prop for the complex input type is a good solution if it's rendering child SelectField components