feat: implement form components including CheckboxField, SelectField, TextField, and SubmitButton with validation
parent
d841581679
commit
942648e9e9
@ -0,0 +1,43 @@
|
||||
import cn from '@/utils/classnames'
|
||||
import { useFieldContext } from '..'
|
||||
import Checkbox from '../../checkbox'
|
||||
|
||||
type CheckboxFieldProps = {
|
||||
label: string;
|
||||
labelClassName?: string;
|
||||
}
|
||||
|
||||
const CheckboxField = ({
|
||||
label,
|
||||
labelClassName,
|
||||
}: CheckboxFieldProps) => {
|
||||
const field = useFieldContext<boolean>()
|
||||
|
||||
return (
|
||||
<div className='flex gap-2'>
|
||||
<div className='flex h-6 shrink-0 items-center'>
|
||||
<Checkbox
|
||||
id={field.name}
|
||||
checked={field.state.value}
|
||||
onCheck={() => {
|
||||
field.handleChange(!field.state.value)
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<label
|
||||
htmlFor={field.name}
|
||||
className={cn(
|
||||
'system-sm-medium grow cursor-pointer pt-1 text-text-secondary',
|
||||
labelClassName,
|
||||
)}
|
||||
onClick={() => {
|
||||
field.handleChange(!field.state.value)
|
||||
}}
|
||||
>
|
||||
{label}
|
||||
</label>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default CheckboxField
|
||||
@ -0,0 +1,44 @@
|
||||
import cn from '@/utils/classnames'
|
||||
import Tooltip from '../../tooltip'
|
||||
|
||||
type LabelProps = {
|
||||
htmlFor: string
|
||||
label: string
|
||||
isRequired?: boolean
|
||||
showOptional?: boolean
|
||||
tooltip?: string
|
||||
className?: string
|
||||
labelClassName?: string
|
||||
}
|
||||
|
||||
const Label = ({
|
||||
htmlFor,
|
||||
label,
|
||||
isRequired,
|
||||
showOptional,
|
||||
tooltip,
|
||||
labelClassName,
|
||||
}: LabelProps) => {
|
||||
return (
|
||||
<div className='flex h-6 items-center'>
|
||||
<label
|
||||
htmlFor={htmlFor}
|
||||
className={cn('system-sm-medium text-text-secondary', labelClassName)}
|
||||
>
|
||||
{label}
|
||||
</label>
|
||||
{showOptional && <div className='system-xs-regular ml-1 text-text-tertiary'>(Optional)</div>}
|
||||
{isRequired && <div className='system-xs-regular ml-1 text-text-destructive-secondary'>*</div>}
|
||||
{tooltip && (
|
||||
<Tooltip
|
||||
popupContent={
|
||||
<div className='w-[200px]'>{tooltip}</div>
|
||||
}
|
||||
triggerClassName='ml-0.5 w-4 h-4'
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default Label
|
||||
@ -0,0 +1,42 @@
|
||||
import cn from '@/utils/classnames'
|
||||
import { useFieldContext } from '..'
|
||||
import PureSelect from '../../select/pure'
|
||||
import Label from './label'
|
||||
|
||||
type SelectOption = {
|
||||
value: string
|
||||
label: string
|
||||
}
|
||||
|
||||
type SelectFieldProps = {
|
||||
label: string
|
||||
options: SelectOption[]
|
||||
className?: string
|
||||
labelClassName?: string
|
||||
}
|
||||
|
||||
const SelectField = ({
|
||||
label,
|
||||
options,
|
||||
className,
|
||||
labelClassName,
|
||||
}: SelectFieldProps) => {
|
||||
const field = useFieldContext<string>()
|
||||
|
||||
return (
|
||||
<div className={cn('flex flex-col gap-y-0.5', className)}>
|
||||
<Label
|
||||
htmlFor={field.name}
|
||||
className={labelClassName}
|
||||
label={label}
|
||||
/>
|
||||
<PureSelect
|
||||
value={field.state.value}
|
||||
options={options}
|
||||
onChange={value => field.handleChange(value)}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default SelectField
|
||||
@ -0,0 +1,25 @@
|
||||
import { useStore } from '@tanstack/react-form'
|
||||
import { useFormContext } from '..'
|
||||
import Button, { type ButtonProps } from '../../button'
|
||||
|
||||
type SubmitButtonProps = Omit<ButtonProps, 'disabled' | 'loading' | 'onClick'>
|
||||
|
||||
const SubmitButton = ({ ...buttonProps }: SubmitButtonProps) => {
|
||||
const form = useFormContext()
|
||||
|
||||
const [isSubmitting, canSubmit] = useStore(form.store, state => [
|
||||
state.isSubmitting,
|
||||
state.canSubmit,
|
||||
])
|
||||
|
||||
return (
|
||||
<Button
|
||||
disabled={isSubmitting || !canSubmit}
|
||||
loading={isSubmitting}
|
||||
onClick={() => form.handleSubmit()}
|
||||
{...buttonProps}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export default SubmitButton
|
||||
@ -0,0 +1,37 @@
|
||||
import React from 'react'
|
||||
import { useFieldContext } from '..'
|
||||
import Input from '../../input'
|
||||
import Label from './label'
|
||||
import cn from '@/utils/classnames'
|
||||
|
||||
type TextFieldProps = {
|
||||
label: string
|
||||
className?: string
|
||||
labelClassName?: string
|
||||
}
|
||||
|
||||
const TextField = ({
|
||||
label,
|
||||
className,
|
||||
labelClassName,
|
||||
}: TextFieldProps) => {
|
||||
const field = useFieldContext<string>()
|
||||
|
||||
return (
|
||||
<div className={cn('flex flex-col gap-y-0.5', className)}>
|
||||
<Label
|
||||
htmlFor={field.name}
|
||||
label={label}
|
||||
labelClassName={labelClassName}
|
||||
/>
|
||||
<Input
|
||||
id={field.name}
|
||||
value={field.state.value}
|
||||
onChange={e => field.handleChange(e.target.value)}
|
||||
onBlur={field.handleBlur}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default TextField
|
||||
@ -0,0 +1,35 @@
|
||||
import { withForm } from '../..'
|
||||
import { demoFormOpts } from './shared-options'
|
||||
import { ContactMethods } from './types'
|
||||
|
||||
const ContactFields = withForm({
|
||||
...demoFormOpts,
|
||||
render: ({ form }) => {
|
||||
return (
|
||||
<div className='my-2'>
|
||||
<h3 className='title-lg-bold text-text-primary'>Contacts</h3>
|
||||
<div className='flex flex-col gap-4'>
|
||||
<form.AppField
|
||||
name='contact.email'
|
||||
children={field => <field.TextField label='Email' />}
|
||||
/>
|
||||
<form.AppField
|
||||
name='contact.phone'
|
||||
children={field => <field.TextField label='Phone' />}
|
||||
/>
|
||||
<form.AppField
|
||||
name='contact.preferredContactMethod'
|
||||
children={field => (
|
||||
<field.SelectField
|
||||
label='Preferred Contact Method'
|
||||
options={ContactMethods}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
},
|
||||
})
|
||||
|
||||
export default ContactFields
|
||||
@ -0,0 +1,68 @@
|
||||
import { useStore } from '@tanstack/react-form'
|
||||
import { useAppForm } from '../..'
|
||||
import ContactFields from './contact-fields'
|
||||
import { demoFormOpts } from './shared-options'
|
||||
import { UserSchema } from './types'
|
||||
|
||||
const DemoForm = () => {
|
||||
const form = useAppForm({
|
||||
...demoFormOpts,
|
||||
validators: {
|
||||
onSubmit: ({ value }) => {
|
||||
// Validate the entire form
|
||||
const result = UserSchema.safeParse(value)
|
||||
if (!result.success) {
|
||||
const issues = result.error.issues
|
||||
console.log('Validation errors:', issues)
|
||||
return issues[0].message
|
||||
}
|
||||
return undefined
|
||||
},
|
||||
},
|
||||
onSubmit: ({ value }) => {
|
||||
console.log('Form submitted:', value)
|
||||
},
|
||||
})
|
||||
|
||||
const name = useStore(form.store, state => state.values.name)
|
||||
|
||||
return (
|
||||
<form
|
||||
className='flex w-[400px] flex-col gap-4'
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
form.handleSubmit()
|
||||
}}
|
||||
>
|
||||
<form.AppField
|
||||
name='name'
|
||||
children={field => (
|
||||
<field.TextField label='Name' />
|
||||
)}
|
||||
/>
|
||||
<form.AppField
|
||||
name='surname'
|
||||
children={field => (
|
||||
<field.TextField label='Surname' />
|
||||
)}
|
||||
/>
|
||||
<form.AppField
|
||||
name='isAcceptingTerms'
|
||||
children={field => (
|
||||
<field.CheckboxField label='I accept the terms and conditions.' />
|
||||
)}
|
||||
/>
|
||||
{
|
||||
!!name && (
|
||||
<ContactFields form={form} />
|
||||
)
|
||||
}
|
||||
<form.AppForm>
|
||||
<form.SubmitButton>Submit</form.SubmitButton>
|
||||
</form.AppForm>
|
||||
</form>
|
||||
)
|
||||
}
|
||||
|
||||
export default DemoForm
|
||||
@ -0,0 +1,14 @@
|
||||
import { formOptions } from '@tanstack/react-form'
|
||||
|
||||
export const demoFormOpts = formOptions({
|
||||
defaultValues: {
|
||||
name: '',
|
||||
surname: '',
|
||||
isAcceptingTerms: false,
|
||||
contact: {
|
||||
email: '',
|
||||
phone: '',
|
||||
preferredContactMethod: 'email',
|
||||
},
|
||||
},
|
||||
})
|
||||
@ -0,0 +1,34 @@
|
||||
import { z } from 'zod'
|
||||
|
||||
const ContactMethod = z.union([
|
||||
z.literal('email'),
|
||||
z.literal('phone'),
|
||||
z.literal('whatsapp'),
|
||||
z.literal('sms'),
|
||||
])
|
||||
|
||||
export const ContactMethods = ContactMethod.options.map(({ value }) => ({
|
||||
value,
|
||||
label: value.charAt(0).toUpperCase() + value.slice(1),
|
||||
}))
|
||||
|
||||
export const UserSchema = z.object({
|
||||
name: z
|
||||
.string()
|
||||
.regex(/^[A-Z]/, 'Name must start with a capital letter')
|
||||
.min(3, 'Name must be at least 3 characters long'),
|
||||
surname: z
|
||||
.string()
|
||||
.min(3, 'Surname must be at least 3 characters long')
|
||||
.regex(/^[A-Z]/, 'Surname must start with a capital letter'),
|
||||
isAcceptingTerms: z.boolean().refine(val => val, {
|
||||
message: 'You must accept the terms and conditions',
|
||||
}),
|
||||
contact: z.object({
|
||||
email: z.string().email('Invalid email address'),
|
||||
phone: z.string().optional(),
|
||||
preferredContactMethod: ContactMethod,
|
||||
}),
|
||||
})
|
||||
|
||||
export type User = z.infer<typeof UserSchema>
|
||||
@ -0,0 +1,21 @@
|
||||
import { createFormHook, createFormHookContexts } from '@tanstack/react-form'
|
||||
import TextField from './components/text-field'
|
||||
import CheckboxField from './components/checkbox-field'
|
||||
import SelectField from './components/select-filed'
|
||||
import SubmitButton from './components/submit-button'
|
||||
|
||||
export const { fieldContext, useFieldContext, formContext, useFormContext }
|
||||
= createFormHookContexts()
|
||||
|
||||
export const { useAppForm, withForm } = createFormHook({
|
||||
fieldComponents: {
|
||||
TextField,
|
||||
CheckboxField,
|
||||
SelectField,
|
||||
},
|
||||
formComponents: {
|
||||
SubmitButton,
|
||||
},
|
||||
fieldContext,
|
||||
formContext,
|
||||
})
|
||||
Loading…
Reference in New Issue