From 30587a079b1e051ec094f6ff357467a80655eaf6 Mon Sep 17 00:00:00 2001 From: Joe Karow <58997957+JoeKarow@users.noreply.github.com> Date: Wed, 14 Feb 2024 16:39:28 -0500 Subject: [PATCH] dynamic fields --- .../modals/dataPortal/Attributes/fields.tsx | 95 ++++++++----------- 1 file changed, 39 insertions(+), 56 deletions(-) diff --git a/packages/ui/modals/dataPortal/Attributes/fields.tsx b/packages/ui/modals/dataPortal/Attributes/fields.tsx index 6ec229ca431..d340809b358 100644 --- a/packages/ui/modals/dataPortal/Attributes/fields.tsx +++ b/packages/ui/modals/dataPortal/Attributes/fields.tsx @@ -1,19 +1,12 @@ import { Group, Select as MantineSelect, Stack, Text } from '@mantine/core' import { useTranslation } from 'next-i18next' -import { type ComponentPropsWithoutRef, forwardRef, type MouseEventHandler, useEffect, useState } from 'react' +import { type ComponentPropsWithoutRef, forwardRef, useState } from 'react' import { type FieldPath, useFormContext } from 'react-hook-form' -import { - Radio, - type RadioGroupProps, - Select, - type SelectProps, - TextInput, - type TextInputProps, -} from 'react-hook-form-mantine' -import { type LiteralUnion, type TupleToUnion } from 'type-fest' +import { NumberInput, Radio, Select, TextInput } from 'react-hook-form-mantine' +import { type TupleToUnion } from 'type-fest' import { type ApiOutput } from '@weareinreach/api' -import { countries } from '@weareinreach/api/router/fieldOpt/query.countries.handler' +import { type FieldAttributes, FieldType } from '@weareinreach/db/zod_util/attributeSupplement' import { Button } from '~ui/components/core/Button' import { trpc as api } from '~ui/lib/trpcClient' @@ -42,65 +35,55 @@ const SuppText = () => { ) } -const dataSchemas = ['numMinMaxOrRange', 'numRange', 'numMin', 'numMax', 'number'] as const -type DataSchema = TupleToUnion - -const isDataSchema = (schema: string): schema is DataSchema => dataSchemas.includes(schema as DataSchema) - const SuppData = ({ schema }: SuppDataProps) => { const { control } = useFormContext() - if (!isDataSchema(schema)) { - console.error('Invalid schema', schema) - throw new Error('Invalid schema') - } - console.log('SuppData') - // useEffect(() => { - // if (!form.values.supplement?.data) { - // form.setFieldValue('supplement.data', {}) - // } - // // eslint-disable-next-line react-hooks/exhaustive-deps - // }, [form.values.supplement]) - const body = (() => { - switch (schema) { - case 'numMax': - case 'numMin': - case 'number': { - const label = schema === 'numMax' ? 'Max' : schema === 'numMin' ? 'Min' : 'Amount' - const key = schema === 'numMax' ? 'max' : schema === 'numMin' ? 'min' : 'number' - return + + const renderField = (schema: FieldAttributes) => { + const { type, name: dataKey, ...schemaProps } = schema + const baseProps = { + ...schemaProps, + name: `data.${dataKey}` as const, + control, + } + switch (type) { + case FieldType.text: { + return + } + case FieldType.select: { + const { options } = schema + return