Skip to content

Commit 2b1a33e

Browse files
committed
chore: removes field from form state
1 parent f5191dc commit 2b1a33e

File tree

23 files changed

+111
-120
lines changed

23 files changed

+111
-120
lines changed

src/admin/components/forms/Form/buildStateFromSchema.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import {
99
} from '../../../../fields/config/types';
1010
import { Fields, Field, Data } from './types';
1111

12-
const buildValidationPromise = async (fieldState: Field, options: ValidateOptions<unknown, unknown, FieldSchema>) => {
12+
const buildValidationPromise = async (fieldState: Field, options: ValidateOptions<unknown, unknown>) => {
1313
const validatedFieldState = fieldState;
1414

1515
let validationResult: boolean | string = true;
1616

17-
if (fieldAffectsData(options.field) && typeof options.field.validate === 'function') {
18-
validationResult = await options.field.validate(fieldState.value, options);
17+
if (typeof fieldState.validate === 'function') {
18+
validationResult = await fieldState.validate(fieldState.value, options);
1919
}
2020

2121
if (typeof validationResult === 'string') {
@@ -51,7 +51,6 @@ const buildStateFromSchema = async (args: Args): Promise<Fields> => {
5151
const value = typeof data?.[field.name] !== 'undefined' ? data[field.name] : field.defaultValue;
5252

5353
const fieldState = {
54-
field,
5554
value,
5655
initialValue: value,
5756
valid: true,
@@ -61,7 +60,6 @@ const buildStateFromSchema = async (args: Args): Promise<Fields> => {
6160
};
6261

6362
validationPromises.push(buildValidationPromise(fieldState, {
64-
field,
6563
data: fullData,
6664
user,
6765
siblingData,

src/admin/components/forms/Form/index.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import reduceFieldsToValues from './reduceFieldsToValues';
1616
import getSiblingDataFunc from './getSiblingData';
1717
import getDataByPathFunc from './getDataByPath';
1818
import wait from '../../../../utilities/wait';
19-
import { Field, FieldAffectingData } from '../../../../fields/config/types';
19+
import { Field } from '../../../../fields/config/types';
2020
import buildInitialState from './buildInitialState';
2121
import errorMessages from './errorMessages';
2222
import { Context as FormContextType, Props, SubmitOptions } from './types';
@@ -86,7 +86,6 @@ const Form: React.FC<Props> = (props) => {
8686

8787
if (typeof field.validate === 'function') {
8888
validationResult = await field.validate(field.value, {
89-
field: (field.field as unknown as FieldAffectingData),
9089
data,
9190
siblingData: contextRef.current.getSiblingData(path),
9291
user,

src/admin/components/forms/Form/types.ts

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Field as FieldConfig, Condition, Validate } from '../../../../fields/co
22

33
export type Field = {
44
value: unknown
5-
field: Field
65
initialValue: unknown
76
errorMessage?: string
87
valid: boolean

src/admin/components/forms/field-types/Array/Array.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ const ArrayFieldType: React.FC<Props> = (props) => {
6262
const path = pathFromProps || name;
6363

6464
const memoizedValidate = useCallback((value, options) => {
65-
return validate(value, options);
66-
}, [validate]);
65+
return validate(value, { ...options, minRows, maxRows, required });
66+
}, [maxRows, minRows, required, validate]);
6767

6868
const [disableFormData, setDisableFormData] = useState(false);
6969

src/admin/components/forms/field-types/Blocks/Blocks.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ const Blocks: React.FC<Props> = (props) => {
6363
const { dispatchFields } = formContext;
6464

6565
const memoizedValidate = useCallback((value, options) => {
66-
return validate(value, options);
67-
}, [validate]);
66+
return validate(value, { ...options, minRows, maxRows, required });
67+
}, [maxRows, minRows, required, validate]);
6868

6969
const [disableFormData, setDisableFormData] = useState(false);
7070

src/admin/components/forms/field-types/Checkbox/index.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const Checkbox: React.FC<Props> = (props) => {
1919
label,
2020
onChange,
2121
disableFormData,
22+
required,
2223
admin: {
2324
readOnly,
2425
style,
@@ -32,8 +33,8 @@ const Checkbox: React.FC<Props> = (props) => {
3233
const path = pathFromProps || name;
3334

3435
const memoizedValidate = useCallback((value, options) => {
35-
return validate(value, options);
36-
}, [validate]);
36+
return validate(value, { ...options, required });
37+
}, [validate, required]);
3738

3839
const {
3940
value,

src/admin/components/forms/field-types/Code/Code.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ const Code: React.FC<Props> = (props) => {
4242
const path = pathFromProps || name;
4343

4444
const memoizedValidate = useCallback((value, options) => {
45-
return validate(value, options);
46-
}, [validate]);
45+
return validate(value, { ...options, required });
46+
}, [validate, required]);
4747

4848
const {
4949
value,

src/admin/components/forms/field-types/DateTime/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const DateTime: React.FC<Props> = (props) => {
3535
const path = pathFromProps || name;
3636

3737
const memoizedValidate = useCallback((value, options) => {
38-
return validate(value, options);
39-
}, [validate]);
38+
return validate(value, { ...options, required });
39+
}, [validate, required]);
4040

4141
const {
4242
value,

src/admin/components/forms/field-types/Email/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ const Email: React.FC<Props> = (props) => {
3131
const path = pathFromProps || name;
3232

3333
const memoizedValidate = useCallback((value, options) => {
34-
return validate(value, options);
35-
}, [validate]);
34+
return validate(value, { ...options, required });
35+
}, [validate, required]);
3636

3737
const fieldType = useField({
3838
path,

src/admin/components/forms/field-types/Number/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ const NumberField: React.FC<Props> = (props) => {
3333
const path = pathFromProps || name;
3434

3535
const memoizedValidate = useCallback((value, options) => {
36-
return validate(value, options);
37-
}, [validate]);
36+
return validate(value, { ...options, min, max, required });
37+
}, [validate, min, max, required]);
3838

3939
const {
4040
value,

src/admin/components/forms/field-types/Password/index.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ const Password: React.FC<Props> = (props) => {
2424
const path = pathFromProps || name;
2525

2626
const memoizedValidate = useCallback((value, options) => {
27-
debugger;
28-
return validate(value, options);
29-
}, [validate]);
27+
const validationResult = validate(value, { ...options, required });
28+
return validationResult;
29+
}, [validate, required]);
3030

3131
const {
3232
value,

src/admin/components/forms/field-types/Point/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ const PointField: React.FC<Props> = (props) => {
3333
const path = pathFromProps || name;
3434

3535
const memoizedValidate = useCallback((value, options) => {
36-
return validate(value, options);
37-
}, [validate]);
36+
return validate(value, { ...options, required });
37+
}, [validate, required]);
3838

3939
const {
4040
value = [null, null],

src/admin/components/forms/field-types/RadioGroup/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ const RadioGroup: React.FC<Props> = (props) => {
3636
const path = pathFromProps || name;
3737

3838
const memoizedValidate = useCallback((value, validationOptions) => {
39-
return validate(value, validationOptions);
40-
}, [validate]);
39+
return validate(value, { ...validationOptions, options, required });
40+
}, [validate, options, required]);
4141

4242
const {
4343
value,

src/admin/components/forms/field-types/Relationship/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ const Relationship: React.FC<Props> = (props) => {
6363
const [search, setSearch] = useState('');
6464

6565
const memoizedValidate = useCallback((value, validationOptions) => {
66-
return validate(value, validationOptions);
67-
}, [validate]);
66+
return validate(value, { ...validationOptions, required });
67+
}, [validate, required]);
6868

6969
const {
7070
value,

src/admin/components/forms/field-types/RichText/RichText.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import isHotkey from 'is-hotkey';
33
import { createEditor, Transforms, Node, Element as SlateElement, Text, BaseEditor } from 'slate';
44
import { ReactEditor, Editable, withReact, Slate } from 'slate-react';
55
import { HistoryEditor, withHistory } from 'slate-history';
6+
import { options } from 'joi';
67
import { richText } from '../../../../../fields/validations';
78
import useField from '../../useField';
89
import withCondition from '../../withCondition';
@@ -118,8 +119,8 @@ const RichText: React.FC<Props> = (props) => {
118119
}, [enabledLeaves, path, props]);
119120

120121
const memoizedValidate = useCallback((value, validationOptions) => {
121-
return validate(value, validationOptions);
122-
}, [validate]);
122+
return validate(value, { ...validationOptions, required });
123+
}, [validate, required]);
123124

124125
const fieldType = useField({
125126
path,
@@ -271,7 +272,6 @@ const RichText: React.FC<Props> = (props) => {
271272
ref={editorRef}
272273
>
273274
<Editable
274-
className={`${baseClass}__input`}
275275
renderElement={renderElement}
276276
renderLeaf={renderLeaf}
277277
placeholder={placeholder}

src/admin/components/forms/field-types/Select/index.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const Select: React.FC<Props> = (props) => {
2525
label,
2626
options: optionsFromProps,
2727
hasMany,
28+
required,
2829
admin: {
2930
readOnly,
3031
style,
@@ -44,8 +45,8 @@ const Select: React.FC<Props> = (props) => {
4445
}, [optionsFromProps]);
4546

4647
const memoizedValidate = useCallback((value, validationOptions) => {
47-
return validate(value, validationOptions);
48-
}, [validate]);
48+
return validate(value, { ...validationOptions, options, hasMany, required });
49+
}, [validate, required, hasMany, options]);
4950

5051
const {
5152
value,

src/admin/components/forms/field-types/Text/index.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const Text: React.FC<Props> = (props) => {
1212
required,
1313
validate = text,
1414
label,
15+
minLength,
16+
maxLength,
1517
admin: {
1618
placeholder,
1719
readOnly,
@@ -24,9 +26,10 @@ const Text: React.FC<Props> = (props) => {
2426
} = props;
2527

2628
const path = pathFromProps || name;
29+
2730
const memoizedValidate = useCallback((value, options) => {
28-
return validate(value, options);
29-
}, [validate]);
31+
return validate(value, { ...options, minLength, maxLength, required });
32+
}, [validate, minLength, maxLength, required]);
3033

3134
const field = useField<string>({
3235
path,

src/admin/components/forms/field-types/Textarea/index.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const Textarea: React.FC<Props> = (props) => {
1313
name,
1414
required,
1515
validate = textarea,
16+
maxLength,
17+
minLength,
1618
admin: {
1719
readOnly,
1820
style,
@@ -28,9 +30,9 @@ const Textarea: React.FC<Props> = (props) => {
2830

2931
const path = pathFromProps || name;
3032

31-
const memoizedValidate = useCallback((value, validationOptions) => {
32-
return validate(value, validationOptions);
33-
}, [validate]);
33+
const memoizedValidate = useCallback((value, options) => {
34+
return validate(value, { ...options, required, maxLength, minLength });
35+
}, [validate, required, maxLength, minLength]);
3436

3537
const {
3638
value,

src/admin/components/forms/field-types/Upload/index.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ const Upload: React.FC<Props> = (props) => {
3737

3838
const collection = collections.find((coll) => coll.slug === relationTo);
3939

40-
const memoizedValidate = useCallback((value, validationOptions) => {
41-
return validate(value, validationOptions);
42-
}, [validate]);
40+
const memoizedValidate = useCallback((value, options) => {
41+
return validate(value, { ...options, required });
42+
}, [validate, required]);
4343

4444
const field = useField({
4545
path,

src/admin/components/forms/useField/index.tsx

-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ const useField = <T extends unknown>(options: Options): FieldType<T> => {
6565

6666
const validateOptions = {
6767
id,
68-
field: field?.field,
6968
user,
7069
data: getData(),
7170
siblingData: getSiblingData(path),
@@ -88,7 +87,6 @@ const useField = <T extends unknown>(options: Options): FieldType<T> => {
8887
condition,
8988
disableFormData,
9089
dispatchFields,
91-
field,
9290
getData,
9391
getSiblingData,
9492
id,

src/fields/config/types.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,14 @@ export type Labels = {
5252
};
5353

5454
export type ValidateOptions<T, S, F> = {
55-
field: F
5655
data: Partial<T>
5756
siblingData: Partial<S>
5857
id?: string | number
5958
user?: Partial<User>
6059
operation?: Operation
61-
}
60+
} & F;
6261

63-
export type Validate<T = any, S = any, F = any, TT = any> = (value?: T, options?: ValidateOptions<F, S, TT>) => string | true | Promise<string | true>;
62+
export type Validate<T = any, S = any, F = any> = (value?: T, options?: ValidateOptions<F, S, F>) => string | true | Promise<string | true>;
6463

6564
export type OptionObject = {
6665
label: string

src/fields/validationPromise.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const validationPromise = async ({
4242
if (valueToValidate === undefined) valueToValidate = field.defaultValue;
4343

4444
const result = shouldValidate ? await field.validate(valueToValidate, {
45-
field,
45+
...field,
4646
data: merge(fullOriginalDoc, fullData),
4747
siblingData: merge(originalDoc, data),
4848
id,

0 commit comments

Comments
 (0)