* Constructs a new `FieldGroupApi` instance with the given form options.
(
opts: FieldGroupOptions<
TFormData,
TFieldGroupData,
TFields,
TOnMount,
TOnChange,
TOnChangeAsync,
TOnBlur,
TOnBlurAsync,
TOnSubmit,
TOnSubmitAsync,
TOnDynamic,
TOnDynamicAsync,
TOnServer,
TSubmitMeta
>,
)
| 236 | * Constructs a new `FieldGroupApi` instance with the given form options. |
| 237 | */ |
| 238 | constructor( |
| 239 | opts: FieldGroupOptions< |
| 240 | TFormData, |
| 241 | TFieldGroupData, |
| 242 | TFields, |
| 243 | TOnMount, |
| 244 | TOnChange, |
| 245 | TOnChangeAsync, |
| 246 | TOnBlur, |
| 247 | TOnBlurAsync, |
| 248 | TOnSubmit, |
| 249 | TOnSubmitAsync, |
| 250 | TOnDynamic, |
| 251 | TOnDynamicAsync, |
| 252 | TOnServer, |
| 253 | TSubmitMeta |
| 254 | >, |
| 255 | ) { |
| 256 | if (opts.form instanceof FieldGroupApi) { |
| 257 | const group = opts.form |
| 258 | this.form = group.form as never |
| 259 | |
| 260 | // the DeepKey is already namespaced, so we need to ensure that we reference |
| 261 | // the form and not the group |
| 262 | if (typeof opts.fields === 'string') { |
| 263 | this.fieldsMap = group.getFormFieldName(opts.fields) as TFields |
| 264 | } else { |
| 265 | // TypeScript has a tough time with generics being a union for some reason |
| 266 | const fields = { |
| 267 | ...(opts.fields as FieldsMap<TFormData, TFieldGroupData>), |
| 268 | } |
| 269 | for (const key in fields) { |
| 270 | fields[key] = group.getFormFieldName(fields[key]) as never |
| 271 | } |
| 272 | this.fieldsMap = fields as never |
| 273 | } |
| 274 | } else { |
| 275 | this.form = opts.form |
| 276 | this.fieldsMap = opts.fields |
| 277 | } |
| 278 | |
| 279 | this.store = new Derived({ |
| 280 | deps: [this.form.store], |
| 281 | fn: ({ currDepVals }) => { |
| 282 | const currFormStore = currDepVals[0] |
| 283 | let values: TFieldGroupData |
| 284 | if (typeof this.fieldsMap === 'string') { |
| 285 | // all values live at that name, so we can directly fetch it |
| 286 | values = getBy(currFormStore.values, this.fieldsMap) |
| 287 | } else { |
| 288 | // we need to fetch the values from all places where they were mapped from |
| 289 | values = {} as never |
| 290 | const fields: Record<keyof TFieldGroupData, string> = this |
| 291 | .fieldsMap as never |
| 292 | for (const key in fields) { |
| 293 | values[key] = getBy(currFormStore.values, fields[key]) |
| 294 | } |
| 295 | } |