Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/form/fields/ForcedValueField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function ForcedValueField({
const descriptionId = `forced-value-${name}-description`;

useEffect(() => {
console.log('setting value', name, value);
setValue(name, value);
}, [name, value, setValue]);

Expand Down
15 changes: 14 additions & 1 deletion src/components/form/fields/HiddenField.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import { useFormContext } from 'react-hook-form';
import { useEffect } from 'react';
import { FormField } from '../../ui/form';
import { Field } from '@/src/flows/types';

export function HiddenField(props: Field) {
const { control } = useFormContext();
const { control, setValue } = useFormContext();

// If the field has a forced/computed value (const === default), set it
// If not it'll get the value from the form state
useEffect(() => {
if (
props.const !== undefined &&
props.const === props.default &&
props.name
) {
setValue(props.name, props.const);
}
}, [props.const, props.default, props.name, setValue]);

return (
<FormField
Expand Down
65 changes: 65 additions & 0 deletions src/components/form/fields/TimeField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as React from 'react';

import { useFormFields, useTransformer } from '@/src/context';
import { Components, JSFField, $TSFixMe } from '@/src/types/remoteFlows';
import { useFormContext } from 'react-hook-form';
import { FormField } from '../../ui/form';
import { TimeFieldDataProps } from '@/src/types/fields';

export type TimeFieldProps = JSFField & {
onChange?: (event: React.ChangeEvent<HTMLInputElement>) => void;
component?: Components['time'];
};

export function TimeField({
name,
description,
label,
type,
onChange,
component,
maxLength,
...rest
}: TimeFieldProps) {
const { components } = useFormFields();
const { control } = useFormContext();
const transformHtml = useTransformer();

return (
<FormField
control={control}
name={name}
render={({ field, fieldState }) => {
const Component = component || components?.time;

if (!Component) {
throw new Error(`Time component not found for field ${name}`);
}

const customTimeFieldProps: TimeFieldDataProps = {
name,
description,
label,
type,
onChange,
maxLength,
transformHtml,
...rest,
};
return (
<Component
field={{
...field,
onChange: (value: $TSFixMe) => {
field.onChange(value);
onChange?.(value);
},
}}
fieldState={fieldState}
fieldData={customTimeFieldProps}
/>
);
}}
/>
);
}
2 changes: 2 additions & 0 deletions src/components/form/fields/baseFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { WorkScheduleField } from './WorkScheduleField';
import { MultiSelectField } from './MultiSelectField';
import { MoneyField } from './MoneyField';
import { TelField } from './TelField';
import { TimeField } from './TimeField';
import { SupportedTypes } from './types';
import { $TSFixMe } from '@/src/types/remoteFlows';

Expand All @@ -34,5 +35,6 @@ export const baseFields: Record<
countries: CountryField,
hidden: HiddenField,
tel: TelField,
time: TimeField,
'work-schedule': WorkScheduleField,
};
42 changes: 42 additions & 0 deletions src/components/form/fields/default/TimeFieldDefault.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { FormDescription, FormMessage } from '@/src/components/ui/form';
import { FormControl, FormItem, FormLabel } from '@/src/components/ui/form';
import { Input } from '@/src/components/ui/input';
import { TimeFieldComponentProps } from '@/src/types/fields';

export function TimeFieldDefault({
field,
fieldState,
fieldData,
}: TimeFieldComponentProps) {
const { name, label, description } = fieldData;
return (
<FormItem
data-field={name}
className={`RemoteFlows__TimeField__Item__${name}`}
>
{label && (
<FormLabel className='RemoteFlows__TimeField__Label'>{label}</FormLabel>
)}
<FormControl>
<Input
{...field}
type='time'
value={field.value ?? ''}
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
field.onChange(event);
}}
className='RemoteFlows__TimeField__Input'
placeholder={label}
/>
</FormControl>
{description && (
<FormDescription className='RemoteFlows__TimeField__Description'>
{description}
</FormDescription>
)}
{fieldState.error && (
<FormMessage className='RemoteFlows__TimeField__Error' />
)}
</FormItem>
);
}
3 changes: 2 additions & 1 deletion src/components/form/fields/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type BaseTypes =
| 'countries'
| 'hidden'
| 'work-schedule'
| 'tel';
| 'tel'
| 'time';

export type SupportedTypes = BaseTypes | 'fieldset' | 'fieldset-flat';
3 changes: 3 additions & 0 deletions src/components/form/validationResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ export const useJsonSchemasValidationFormResolver = <
handleValidation: (data: T) => Promise<ValidationResult | null>,
): Resolver<T> => {
return async (data: T) => {
console.log('data', data);
const result = await handleValidation(data);

console.log('result', result);

// Handle null case - return no errors
if (!result) {
return {
Expand Down
2 changes: 2 additions & 0 deletions src/default-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { WorkScheduleFieldDefault } from '@/src/components/form/fields/default/W
import { ButtonDefault } from '@/src/components/form/fields/default/ButtonDefault';
import { PDFPreviewDefault } from '@/src/components/shared/pdf-preview/PDFPreviewDefault';
import { TelFieldDefault } from '@/src/components/form/fields/default/TelFieldDefault';
import { TimeFieldDefault } from '@/src/components/form/fields/default/TimeFieldDefault';

/**
* Default field components for RemoteFlows.
Expand Down Expand Up @@ -48,4 +49,5 @@ export const defaultComponents: Components = {
'work-schedule': WorkScheduleFieldDefault,
pdfViewer: PDFPreviewDefault,
tel: TelFieldDefault,
time: TimeFieldDefault,
};
8 changes: 8 additions & 0 deletions src/types/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,11 @@ export type RadioGroupComponentProps = Omit<
> & {
fieldData: RadioGroupDataProps;
};

export type TimeFieldDataProps = FieldDataProps & {
onChange?: (event: React.ChangeEvent<HTMLInputElement>) => void;
};

export type TimeFieldComponentProps = FieldComponentProps & {
fieldData: TimeFieldDataProps;
};
3 changes: 3 additions & 0 deletions src/types/remoteFlows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
TextFieldComponentProps,
WorkScheduleComponentProps,
TelFieldComponentProps,
TimeFieldComponentProps,
RadioGroupComponentProps,
} from '@/src/types/fields';

Expand Down Expand Up @@ -138,6 +139,7 @@ type FieldComponentTypes = Exclude<
| 'money'
| 'date'
| 'tel'
| 'time'
| 'radio'
>;

Expand All @@ -157,6 +159,7 @@ export type Components = {
'work-schedule'?: React.ComponentType<WorkScheduleComponentProps>;
pdfViewer?: React.ComponentType<PDFPreviewComponentProps>;
tel?: React.ComponentType<TelFieldComponentProps>;
time?: React.ComponentType<TimeFieldComponentProps>;
radio?: React.ComponentType<RadioGroupComponentProps>;
};

Expand Down
Loading