diff --git a/apps/web-remix/app/components/form/fields/form.field.tsx b/apps/web-remix/app/components/form/fields/form.field.tsx index a395ad81..6de96d3b 100644 --- a/apps/web-remix/app/components/form/fields/form.field.tsx +++ b/apps/web-remix/app/components/form/fields/form.field.tsx @@ -44,3 +44,25 @@ export const useCurrentFormState = () => { return { values, subscribe, fieldErrors, getValues: () => values }; }, [values, subscribe, fieldErrors]); }; + +export function useSubscribeToField( + defaultWhen: Record> | undefined, + onUpdate: (value: string) => void, +) { + const { subscribe } = useCurrentFormState(); + + useEffect(() => { + if (!defaultWhen) return; + Object.keys(defaultWhen).forEach((key) => { + //eslint-disable-next-line + //@ts-ignore + subscribe.value(key, (data) => { + const value = defaultWhen[key][data]; + + if (value) { + onUpdate(value); + } + }); + }); + }, [defaultWhen]); +} diff --git a/apps/web-remix/app/components/form/fields/text.field.tsx b/apps/web-remix/app/components/form/fields/text.field.tsx index 4107ce3d..c32f4cd0 100644 --- a/apps/web-remix/app/components/form/fields/text.field.tsx +++ b/apps/web-remix/app/components/form/fields/text.field.tsx @@ -4,7 +4,10 @@ import { RefreshCcw } from 'lucide-react'; import { useFieldContext } from '~/components/form/fields/field.context'; import { FieldLabel } from '~/components/form/fields/field.label'; import type { ValidationBehaviorOptions } from '~/components/form/fields/form.field'; -import { useControlField } from '~/components/form/fields/form.field'; +import { + useControlField, + useSubscribeToField, +} from '~/components/form/fields/form.field'; import type { TextInputProps } from '~/components/form/inputs/text.input'; import { TextInput } from '~/components/form/inputs/text.input'; import { IconButton } from '~/components/iconButton'; @@ -59,8 +62,12 @@ export function ResettableTextInputField({ label, defaultValue, size, + defaultWhen, ...props -}: Partial & { label: string }) { +}: Partial & { + label: string; + defaultWhen?: Record>; +}) { const inputRef = useRef(null); const { name } = useFieldContext({ validationBehavior: { @@ -72,11 +79,13 @@ export function ResettableTextInputField({ const [value, setValue] = useControlField(name); + useSubscribeToField(defaultWhen, setValue); + useEffect(() => { - if (typeof defaultValue === 'string') { - updateAndValidate(defaultValue); + if (typeof defaultValue === 'string' && !value) { + setValue(defaultValue); } - }, [defaultValue]); + }, []); const onReset = () => { if (typeof defaultValue === 'string') { diff --git a/apps/web-remix/app/components/form/schema/SchemaFields.tsx b/apps/web-remix/app/components/form/schema/SchemaFields.tsx index c8d61189..fa53d189 100644 --- a/apps/web-remix/app/components/form/schema/SchemaFields.tsx +++ b/apps/web-remix/app/components/form/schema/SchemaFields.tsx @@ -108,7 +108,6 @@ export function StringField({ //eslint-disable-next-line //@ts-ignore let defaultValue = field.default; - if ('defaultWhen' in field && field.defaultWhen) { const formValues = getValues(); const defaultKey = Object.keys(field.defaultWhen)[0]; @@ -127,6 +126,7 @@ export function StringField({ label={field.title} size={size} readOnly={rest.disabled} + defaultWhen={field.defaultWhen} /> diff --git a/apps/web-remix/app/components/form/schema/SchemaParser.tsx b/apps/web-remix/app/components/form/schema/SchemaParser.tsx index 26f86af1..0ff4eeb0 100644 --- a/apps/web-remix/app/components/form/schema/SchemaParser.tsx +++ b/apps/web-remix/app/components/form/schema/SchemaParser.tsx @@ -228,6 +228,7 @@ export type JSONSchemaField = maxLength?: number; presentAs: 'password'; displayWhen?: DisplayWhen; + defaultWhen?: Record>; errorMessages?: Record; } | { @@ -236,6 +237,7 @@ export type JSONSchemaField = description: string; presentAs: 'wysiwyg'; displayWhen?: DisplayWhen; + defaultWhen?: Record>; errorMessages?: Record; } | { @@ -250,6 +252,7 @@ export type JSONSchemaField = default?: string; readonly?: boolean; displayWhen?: DisplayWhen; + defaultWhen?: Record>; errorMessages?: Record; } | { @@ -263,6 +266,7 @@ export type JSONSchemaField = default?: string; readonly?: boolean; displayWhen?: DisplayWhen; + defaultWhen?: Record>; errorMessages?: Record; } | { @@ -273,6 +277,7 @@ export type JSONSchemaField = url: string; default?: string; displayWhen?: DisplayWhen; + defaultWhen?: Record>; errorMessages?: Record; } | { @@ -285,6 +290,7 @@ export type JSONSchemaField = schema: JSONSchemaField; readonly?: boolean; displayWhen?: DisplayWhen; + defaultWhen?: Record>; errorMessages?: Record; } | { @@ -297,6 +303,7 @@ export type JSONSchemaField = default?: number; readonly?: boolean; displayWhen?: DisplayWhen; + defaultWhen?: Record>; errorMessages?: Record; } | { @@ -307,6 +314,7 @@ export type JSONSchemaField = minItems: number; default?: unknown[]; displayWhen?: DisplayWhen; + defaultWhen?: Record>; errorMessages?: Record; } | { @@ -315,6 +323,7 @@ export type JSONSchemaField = description: string; default?: boolean; displayWhen?: DisplayWhen; + defaultWhen?: Record>; errorMessages?: Record; };