Skip to content

Commit

Permalink
fix defaultWhen in string fields
Browse files Browse the repository at this point in the history
  • Loading branch information
breeg554 committed Jan 14, 2025
1 parent bf9f671 commit abba5d0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
22 changes: 22 additions & 0 deletions apps/web-remix/app/components/form/fields/form.field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,25 @@ export const useCurrentFormState = () => {
return { values, subscribe, fieldErrors, getValues: () => values };
}, [values, subscribe, fieldErrors]);
};

export function useSubscribeToField(
defaultWhen: Record<string, Record<string, string>> | 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]);
}
19 changes: 14 additions & 5 deletions apps/web-remix/app/components/form/fields/text.field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -59,8 +62,12 @@ export function ResettableTextInputField({
label,
defaultValue,
size,
defaultWhen,
...props
}: Partial<TextInputFieldProps> & { label: string }) {
}: Partial<TextInputFieldProps> & {
label: string;
defaultWhen?: Record<string, Record<string, string>>;
}) {
const inputRef = useRef<HTMLInputElement | null>(null);
const { name } = useFieldContext({
validationBehavior: {
Expand All @@ -72,11 +79,13 @@ export function ResettableTextInputField({

const [value, setValue] = useControlField<string | undefined>(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') {
Expand Down
2 changes: 1 addition & 1 deletion apps/web-remix/app/components/form/schema/SchemaFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -127,6 +126,7 @@ export function StringField({
label={field.title}
size={size}
readOnly={rest.disabled}
defaultWhen={field.defaultWhen}
/>

<FieldMessage size={size} error={error}>
Expand Down
9 changes: 9 additions & 0 deletions apps/web-remix/app/components/form/schema/SchemaParser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ export type JSONSchemaField =
maxLength?: number;
presentAs: 'password';
displayWhen?: DisplayWhen;
defaultWhen?: Record<string, Record<string, string>>;
errorMessages?: Record<string, string>;
}
| {
Expand All @@ -236,6 +237,7 @@ export type JSONSchemaField =
description: string;
presentAs: 'wysiwyg';
displayWhen?: DisplayWhen;
defaultWhen?: Record<string, Record<string, string>>;
errorMessages?: Record<string, string>;
}
| {
Expand All @@ -250,6 +252,7 @@ export type JSONSchemaField =
default?: string;
readonly?: boolean;
displayWhen?: DisplayWhen;
defaultWhen?: Record<string, Record<string, string>>;
errorMessages?: Record<string, string>;
}
| {
Expand All @@ -263,6 +266,7 @@ export type JSONSchemaField =
default?: string;
readonly?: boolean;
displayWhen?: DisplayWhen;
defaultWhen?: Record<string, Record<string, string>>;
errorMessages?: Record<string, string>;
}
| {
Expand All @@ -273,6 +277,7 @@ export type JSONSchemaField =
url: string;
default?: string;
displayWhen?: DisplayWhen;
defaultWhen?: Record<string, Record<string, string>>;
errorMessages?: Record<string, string>;
}
| {
Expand All @@ -285,6 +290,7 @@ export type JSONSchemaField =
schema: JSONSchemaField;
readonly?: boolean;
displayWhen?: DisplayWhen;
defaultWhen?: Record<string, Record<string, string>>;
errorMessages?: Record<string, string>;
}
| {
Expand All @@ -297,6 +303,7 @@ export type JSONSchemaField =
default?: number;
readonly?: boolean;
displayWhen?: DisplayWhen;
defaultWhen?: Record<string, Record<string, string>>;
errorMessages?: Record<string, string>;
}
| {
Expand All @@ -307,6 +314,7 @@ export type JSONSchemaField =
minItems: number;
default?: unknown[];
displayWhen?: DisplayWhen;
defaultWhen?: Record<string, Record<string, string>>;
errorMessages?: Record<string, string>;
}
| {
Expand All @@ -315,6 +323,7 @@ export type JSONSchemaField =
description: string;
default?: boolean;
displayWhen?: DisplayWhen;
defaultWhen?: Record<string, Record<string, string>>;
errorMessages?: Record<string, string>;
};

Expand Down

0 comments on commit abba5d0

Please sign in to comment.