Skip to content

Commit

Permalink
[EN-4026] chore(inputs): try typing
Browse files Browse the repository at this point in the history
  • Loading branch information
emile-bex committed Aug 8, 2023
1 parent b3b9310 commit 7fb2b24
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 100 deletions.
139 changes: 62 additions & 77 deletions src/components/forms/FormSchema/FormSchema.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RadioTypes } from 'src/components/utils/Inputs/Radio/Radio.types';
import { FilterConstant } from 'src/constants';
import { AnyCantFix, AnyToFix } from 'src/utils/Types';
import { AnyToFix } from 'src/utils/Types';

const FormComponents = {
DATEPICKER: 'datepicker',
Expand Down Expand Up @@ -32,7 +32,9 @@ export type FieldValue =

type MultiFilterConstant<M extends boolean> = M extends true
? FilterConstant[]
: FilterConstant;
: M extends false
? FilterConstant
: FilterConstant | FilterConstant[];

interface FormComponentValues<M extends boolean>
extends Record<FormComponent, FieldValue> {
Expand Down Expand Up @@ -96,35 +98,30 @@ type InputComponent =
| SelectComponent
| SelectRequestComponent;

interface FormFieldCommonProperties {
id: string;
name: string;
}
// export type FormFieldName<S> = keyof S & string;

interface FormTypes {
test: string;
interface FormFieldCommonProperties<S> {
id: S;
name: S;
}

export type FormType = keyof FormTypes;
// export type GetValueType = (name: FormFieldName<S>) => S[FormFieldName<S>];

// export type GetValueType<K extends FormType> = (name: K) => FormTypes[K];
export type GetValueType = (name: string) => AnyToFix;

interface Rule<T extends InputComponent, M extends boolean> {
method: (
fieldValue: FormComponentValues<M>[T],
fieldValues: { [K in FormType]: FormTypes[K] }
) => boolean;
interface Rule<T extends InputComponent, S, M extends boolean> {
method: (fieldValue: FormComponentValues<M>[T], fieldValues: S) => boolean;
args?: AnyToFix[];
message: string;
}

interface FormFieldInputCommonProperties<
T extends InputComponent,
M extends boolean = false
> extends FormFieldCommonProperties {
S,
M extends boolean = boolean
> extends FormFieldCommonProperties<S> {
isRequired?: boolean;
rules?: Rule<T, M>[];
rules?: Rule<T, S, M>[];
title: string | JSX.Element | ((getValue: GetValueType) => string);
disabled?: boolean;
disable?: (getValue: GetValueType) => boolean;
Expand All @@ -134,8 +131,8 @@ interface FormFieldInputCommonProperties<
showLabel?: boolean;
}

export interface FormFieldTextInput
extends FormFieldInputCommonProperties<TextInputComponent> {
export interface FormFieldTextInput<S>
extends FormFieldInputCommonProperties<TextInputComponent, S> {
component: TextInputComponent;
type?: 'text' | 'email' | 'password';
rows?: number;
Expand All @@ -145,17 +142,17 @@ export interface FormFieldTextInput
max?: string;
}

export interface FormFieldCheckBox
extends FormFieldInputCommonProperties<CheckBoxComponent> {
export interface FormFieldCheckBox<S>
extends FormFieldInputCommonProperties<CheckBoxComponent, S> {
component: CheckBoxComponent;
}

export interface FormFieldSelect<K extends FilterConstant[] = AnyToFix>
extends FormFieldInputCommonProperties<SelectComponent> {
export interface FormFieldSelect<S>
extends FormFieldInputCommonProperties<SelectComponent, S> {
component: SelectComponent;
dynamicFilter?: (getValue: GetValueType) => string;
fieldsToReset?: string[];
options?: K;
options?: FilterConstant[];
loadOptions?: (
callback: (options: FilterConstant[] | RadioTypes[]) => void,
inputValue?: string,
Expand All @@ -164,93 +161,81 @@ export interface FormFieldSelect<K extends FilterConstant[] = AnyToFix>
errorMessage?: string;
limit?: number;
}
export interface FormFieldSelectRequestCommon<
K extends FilterConstant[],
M extends boolean
> extends FormFieldInputCommonProperties<SelectRequestComponent, M> {
export interface FormFieldSelectRequestCommon<S, M extends boolean>
extends FormFieldInputCommonProperties<SelectRequestComponent, S, M> {
component: SelectRequestComponent;
fieldsToReset?: string[];
options?: K;
options?: FilterConstant[];
loadOptions?: (
callback: (options: FilterConstant[] | RadioTypes[]) => void,
inputValue?: string,
getValue?: GetValueType
) => Promise<void> | void;
openMenuOnClick?: boolean;
isMulti?: boolean | ((getValue: (name: string) => AnyToFix) => boolean);
}

// TODO fix type depending on is Multi
interface FormFieldSelectRequestMulti<K extends FilterConstant[]>
extends FormFieldSelectRequestCommon<K, true> {
isMulti?: true;
interface FormFieldSelectRequestMulti<S>
extends FormFieldSelectRequestCommon<S, true> {
isMulti: true;
}

interface FormFieldSelectRequestSingle<K extends FilterConstant[]>
extends FormFieldSelectRequestCommon<K, false> {
isMulti?: false;
interface FormFieldSelectRequestSingle<S>
extends FormFieldSelectRequestCommon<S, false> {
isMulti: false;
}

export type FormFieldSelectRequest<K extends FilterConstant[] = AnyToFix> =
| FormFieldSelectRequestMulti<K>
| FormFieldSelectRequestSingle<K>
interface FormFieldSelectRequestMethod<S>
extends FormFieldSelectRequestCommon<S, boolean> {
isMulti: (getValue: (name: string) => AnyToFix) => boolean;
}

export type FormFieldInput =
| FormFieldTextInput
| FormFieldCheckBox
| FormFieldSelect
| FormFieldSelectRequest;
type FormFieldSelectRequestOmit<S> = Omit<
FormFieldSelectRequestCommon<S, false>,
'isMulti'
>;

export interface FormFieldText extends FormFieldCommonProperties {
id: string;
name: string;
export type FormFieldSelectRequest<S> =
| FormFieldSelectRequestMulti<S>
| FormFieldSelectRequestSingle<S>
| FormFieldSelectRequestOmit<S>
| FormFieldSelectRequestMethod<S>;

export type FormFieldInput<S> =
| FormFieldTextInput<S>
| FormFieldCheckBox<S>
| FormFieldSelect<S>
| FormFieldSelectRequest<S>;

export interface FormFieldText<S> extends FormFieldCommonProperties<S> {
title: string | ((getValue: GetValueType) => string);
component: TextComponent;
hide?: (getValue: GetValueType, fieldOptions?: AnyToFix) => boolean;
hidden?: boolean;
}

export interface FormFieldMultiple extends FormFieldCommonProperties {
id: string;
name: string;
export interface FormFieldMultiple<S> extends FormFieldCommonProperties<S> {
action: string;
component: MultipleComponent;
fields: FormFieldInput[];
fields: FormFieldInput<S>[];
hide?: (getValue: GetValueType) => boolean;
hidden?: boolean;
}

export interface FormFieldGroup extends FormFieldCommonProperties {
export interface FormFieldGroup<S> extends FormFieldCommonProperties<S> {
component: GroupComponent;
fields: (FormFieldInput | FormFieldText)[];
fields: (FormFieldInput<S> | FormFieldText<S>)[];
hide?: (getValue: GetValueType) => boolean;
hidden?: boolean;
}

export type FormField =
| FormFieldInput
| FormFieldText
| FormFieldMultiple
| FormFieldGroup;
export type FormField<S> =
| FormFieldInput<S>
| FormFieldText<S>
| FormFieldMultiple<S>
| FormFieldGroup<S>;

export interface FormSchema {
id: string;
fields: FormField[];
fields: FormField<string>[];
}

const test: FormFieldSelectRequest = {
component: undefined,
id: '',
name: '',
options: undefined,
title: undefined,
isMulti: true,
rules: [
{
method: (fieldValue) => {
return true;
},
message: 'oui',
},
],
};
10 changes: 5 additions & 5 deletions src/components/forms/FormWithValidation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ import {
} from './FormSchema';
import { StyledForm } from './Forms.styles';

interface FormWithValidationProps {
interface FormWithValidationProps<S> {
defaultValues?: AnyToFix; // to be typed
onCancel?: () => void;
onSubmit: (arg1: AnyToFix, arg2: AnyToFix) => void; // to be typed
onError?: (any) => void;
formSchema: FormSchema;
formSchema: FormSchema<S>;
submitText?: string;
cancelText?: string;
enterToSubmit?: boolean;
}

export const FormWithValidation = forwardRef<
{ resetForm: () => void },
FormWithValidationProps
FormWithValidationProps<S>
>(
(
{
Expand All @@ -49,7 +49,7 @@ export const FormWithValidation = forwardRef<
onCancel,
enterToSubmit = false,
onError,
},
}: FormWithValidationProps<S>,
ref
) => {
const { id: formId, fields } = formSchema;
Expand All @@ -65,7 +65,7 @@ export const FormWithValidation = forwardRef<
);

const { handleSubmit, control, reset, getValues, resetField, watch } =
useForm({
useForm<S>({
defaultValues,
shouldUnregister: true,
});
Expand Down
10 changes: 5 additions & 5 deletions src/components/forms/fields/GenericField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ import { CommonInputProps } from 'src/components/utils/Inputs/Inputs.types';

import { AnyToFix } from 'src/utils/Types';

interface GenericFieldProps {
field: FormFieldInput;
interface GenericFieldProps<S> {
field: FormFieldInput<S>;
formId: string;
getValue: GetValueType;
getValue: GetValueType<S>;
fieldOptions?: AnyToFix;
updateFieldOptions?: (newFieldOption?: { [K in string]: AnyToFix }) => void;
control: Control;
resetField: UseFormResetField<AnyToFix>;
watch: UseFormWatch<AnyToFix>;
}

export const GenericField = ({
export function GenericField<S>({
field,
formId,
getValue,
Expand All @@ -51,7 +51,7 @@ export const GenericField = ({
control,
resetField,
watch,
}: GenericFieldProps) => {
}: GenericFieldProps<S>) {
const rules = field.rules?.reduce((acc, curr, index) => {
return {
...acc,
Expand Down
4 changes: 2 additions & 2 deletions src/components/forms/schemas/formAddUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ export const formAddUser: FormSchema = {
id: 'userToLinkId',
name: 'userToLinkId',
component: 'select-async',
/* isMulti: (getValue) => {
isMulti: (getValue) => {
const role = getValue('role');
return role === USER_ROLES.COACH_EXTERNAL;
}, */
},
hide: (getValue) => {
const role = getValue('role') as UserRole;
const organizationId = (
Expand Down
1 change: 0 additions & 1 deletion src/components/forms/schemas/formChangePassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const formChangePassword: FormSchema = {
rules: [
{
method: (fieldValue) => passwordStrength(fieldValue).id >= 2,

message: 'Doit répondre aux critères ci-dessus',
},
],
Expand Down
3 changes: 1 addition & 2 deletions src/components/forms/schemas/formCompanyContact.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { isValidPhoneNumber } from 'react-phone-number-input/mobile';
import validator from 'validator';
import { FormSchema } from '../FormSchema';
import {
COMPANY_APPROACHES_FILTERS,
COMPANY_CONTACT_ZONES_FILTERS,
HEARD_ABOUT_FILTERS,
} from 'src/constants';
import validator from "validator";

export const formCompanyContact: FormSchema = {
id: 'form-company-contact',
Expand All @@ -17,7 +17,6 @@ export const formCompanyContact: FormSchema = {
title: 'Votre prénom*',
isRequired: true,
maxLength: 80,

},
{
id: 'lastName',
Expand Down
9 changes: 5 additions & 4 deletions src/components/forms/schemas/formResetPassword.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { passwordStrength } from 'check-password-strength';
import { FormSchema } from '../FormSchema';

interface Schema {
newPassword: string;
confirmPassword: string;
}
export const formResetPassword: FormSchema = {
id: 'form-reset-pwd',
fields: [
Expand All @@ -16,7 +20,6 @@ export const formResetPassword: FormSchema = {
method: (fieldValue) => {
return passwordStrength(fieldValue).id >= 2;
},

message: 'Doit répondre aux critères ci-dessus',
},
],
Expand All @@ -30,15 +33,13 @@ export const formResetPassword: FormSchema = {
isRequired: true,
rules: [
{
method: (fieldValue) =>
passwordStrength(fieldValue).id >= 2,
method: (fieldValue) => passwordStrength(fieldValue).id >= 2,
message: 'Doit répondre aux critères ci-dessus',
},
{
method: (fieldValue, fieldValues) => {
return fieldValues.newPassword === fieldValue;
},

message: 'Les deux mots de passe ne correspondent pas',
},
],
Expand Down
Loading

0 comments on commit 7fb2b24

Please sign in to comment.