|
| 1 | +import { type SubmissionResult } from '@conform-to/react'; |
| 2 | + |
| 3 | +type Action<State, Payload> = (state: Awaited<State>, payload: Payload) => State | Promise<State>; |
| 4 | + |
| 5 | +interface FormField { |
| 6 | + name: string; |
| 7 | + label?: string; |
| 8 | + errors?: string[]; |
| 9 | + required?: boolean; |
| 10 | + id?: string; |
| 11 | +} |
| 12 | + |
| 13 | +type RadioField = { |
| 14 | + type: 'radio-group'; |
| 15 | + options: Array<{ label: string; value: string }>; |
| 16 | + defaultValue?: string; |
| 17 | +} & FormField; |
| 18 | + |
| 19 | +type CheckboxField = { |
| 20 | + type: 'checkbox'; |
| 21 | + defaultValue?: string; |
| 22 | +} & FormField; |
| 23 | + |
| 24 | +type CheckboxGroupField = { |
| 25 | + type: 'checkbox-group'; |
| 26 | + options: Array<{ label: string; value: string }>; |
| 27 | + defaultValue?: string[]; |
| 28 | +} & FormField; |
| 29 | + |
| 30 | +type NumberInputField = { |
| 31 | + type: 'number'; |
| 32 | + defaultValue?: string; |
| 33 | + min?: number; |
| 34 | + max?: number; |
| 35 | + step?: number; |
| 36 | + incrementLabel?: string; |
| 37 | + decrementLabel?: string; |
| 38 | +} & FormField; |
| 39 | + |
| 40 | +type TextInputField = { |
| 41 | + type: 'text'; |
| 42 | + defaultValue?: string; |
| 43 | +} & FormField; |
| 44 | + |
| 45 | +type TextAreaField = { |
| 46 | + type: 'textarea'; |
| 47 | + defaultValue?: string; |
| 48 | +} & FormField; |
| 49 | + |
| 50 | +type DateField = { |
| 51 | + type: 'date'; |
| 52 | + defaultValue?: string; |
| 53 | + minDate?: string; |
| 54 | + maxDate?: string; |
| 55 | +} & FormField; |
| 56 | + |
| 57 | +type SwatchRadioFieldOption = |
| 58 | + | { |
| 59 | + type: 'color'; |
| 60 | + value: string; |
| 61 | + label: string; |
| 62 | + color: string; |
| 63 | + disabled?: boolean; |
| 64 | + } |
| 65 | + | { |
| 66 | + type: 'image'; |
| 67 | + value: string; |
| 68 | + label: string; |
| 69 | + image: { src: string; alt: string }; |
| 70 | + disabled?: boolean; |
| 71 | + }; |
| 72 | + |
| 73 | +type SwatchRadioField = { |
| 74 | + type: 'swatch-radio-group'; |
| 75 | + defaultValue?: string; |
| 76 | + options: SwatchRadioFieldOption[]; |
| 77 | +} & FormField; |
| 78 | + |
| 79 | +type CardRadioField = { |
| 80 | + type: 'card-radio-group'; |
| 81 | + defaultValue?: string; |
| 82 | + options: Array<{ |
| 83 | + value: string; |
| 84 | + label: string; |
| 85 | + image: { src: string; alt: string }; |
| 86 | + disabled?: boolean; |
| 87 | + }>; |
| 88 | +} & FormField; |
| 89 | + |
| 90 | +type ButtonRadioField = { |
| 91 | + type: 'button-radio-group'; |
| 92 | + defaultValue?: string; |
| 93 | + pattern?: string; |
| 94 | + options: Array<{ |
| 95 | + value: string; |
| 96 | + label: string; |
| 97 | + disabled?: boolean; |
| 98 | + }>; |
| 99 | +} & FormField; |
| 100 | + |
| 101 | +type SelectField = { |
| 102 | + type: 'select'; |
| 103 | + options: Array<{ label: string; value: string }>; |
| 104 | + defaultValue?: string; |
| 105 | +} & FormField; |
| 106 | + |
| 107 | +type PasswordField = { |
| 108 | + type: 'password'; |
| 109 | +} & FormField; |
| 110 | + |
| 111 | +type ConfirmPasswordField = { |
| 112 | + type: 'confirm-password'; |
| 113 | +} & FormField; |
| 114 | + |
| 115 | +type EmailInputField = { |
| 116 | + type: 'email'; |
| 117 | + defaultValue?: string; |
| 118 | +} & FormField; |
| 119 | + |
| 120 | +type HiddenInputField = { |
| 121 | + type: 'hidden'; |
| 122 | + defaultValue?: string; |
| 123 | +} & FormField; |
| 124 | + |
| 125 | +type Field = |
| 126 | + | RadioField |
| 127 | + | CheckboxField |
| 128 | + | CheckboxGroupField |
| 129 | + | NumberInputField |
| 130 | + | TextInputField |
| 131 | + | TextAreaField |
| 132 | + | DateField |
| 133 | + | SwatchRadioField |
| 134 | + | CardRadioField |
| 135 | + | ButtonRadioField |
| 136 | + | SelectField |
| 137 | + | PasswordField |
| 138 | + | ConfirmPasswordField |
| 139 | + | EmailInputField |
| 140 | + | HiddenInputField; |
| 141 | + |
| 142 | +type FieldGroup<F> = F[]; |
| 143 | + |
| 144 | +interface Address { |
| 145 | + [key: string]: unknown; |
| 146 | + id: string; |
| 147 | + firstName: string; |
| 148 | + lastName: string; |
| 149 | + company?: string | undefined; |
| 150 | + address1: string; |
| 151 | + address2?: string | undefined; |
| 152 | + city: string; |
| 153 | + stateOrProvince?: string | undefined; |
| 154 | + postalCode?: string | undefined; |
| 155 | + phone?: string | undefined; |
| 156 | + countryCode: string; |
| 157 | +} |
| 158 | + |
| 159 | +interface DefaultAddressConfiguration { |
| 160 | + id: string | null; |
| 161 | +} |
| 162 | + |
| 163 | +interface AddressListState<A extends Address, F extends Field> { |
| 164 | + addresses: A[]; |
| 165 | + defaultAddress?: DefaultAddressConfiguration; |
| 166 | + lastResult: SubmissionResult | null; |
| 167 | + fields: Array<F | FieldGroup<F>>; |
| 168 | +} |
| 169 | + |
| 170 | +export interface AddressListData<A extends Address, F extends Field> { |
| 171 | + addresses: A[]; |
| 172 | + fields: Array<F | FieldGroup<F>>; |
| 173 | + defaultAddress?: DefaultAddressConfiguration; |
| 174 | + addressAction: Action<AddressListState<A, F>, FormData>; |
| 175 | +} |
0 commit comments