@firecms/collection_editor 3.0.0-beta.2-pre.3 → 3.0.0-beta.2-pre.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.es.js +1852 -1950
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +2 -2
- package/dist/index.umd.js.map +1 -1
- package/dist/ui/RootCollectionSuggestions.d.ts +3 -1
- package/dist/ui/collection_editor/PropertyEditView.d.ts +1 -1
- package/dist/ui/collection_editor/SwitchControl.d.ts +1 -1
- package/dist/useCollectionEditorPlugin.d.ts +6 -3
- package/package.json +7 -6
- package/src/ConfigControllerProvider.tsx +1 -1
- package/src/ui/RootCollectionSuggestions.tsx +15 -8
- package/src/ui/collection_editor/CollectionDetailsForm.tsx +15 -13
- package/src/ui/collection_editor/CollectionEditorDialog.tsx +9 -13
- package/src/ui/collection_editor/CollectionEditorWelcomeView.tsx +1 -1
- package/src/ui/collection_editor/CollectionPropertiesEditorForm.tsx +2 -2
- package/src/ui/collection_editor/EnumForm.tsx +1 -1
- package/src/ui/collection_editor/GetCodeDialog.tsx +1 -1
- package/src/ui/collection_editor/PropertyEditView.tsx +3 -2
- package/src/ui/collection_editor/SubcollectionsEditTab.tsx +1 -1
- package/src/ui/collection_editor/SwitchControl.tsx +1 -1
- package/src/ui/collection_editor/import/CollectionEditorImportMapping.tsx +2 -2
- package/src/ui/collection_editor/properties/BlockPropertyField.tsx +1 -1
- package/src/ui/collection_editor/properties/BooleanPropertyField.tsx +1 -1
- package/src/ui/collection_editor/properties/CommonPropertyFields.tsx +1 -1
- package/src/ui/collection_editor/properties/DateTimePropertyField.tsx +1 -1
- package/src/ui/collection_editor/properties/EnumPropertyField.tsx +1 -1
- package/src/ui/collection_editor/properties/MapPropertyField.tsx +1 -1
- package/src/ui/collection_editor/properties/NumberPropertyField.tsx +1 -1
- package/src/ui/collection_editor/properties/ReferencePropertyField.tsx +1 -1
- package/src/ui/collection_editor/properties/RepeatPropertyField.tsx +1 -1
- package/src/ui/collection_editor/properties/StoragePropertyField.tsx +1 -1
- package/src/ui/collection_editor/properties/StringPropertyField.tsx +1 -1
- package/src/ui/collection_editor/properties/UrlPropertyField.tsx +1 -1
- package/src/ui/collection_editor/properties/advanced/AdvancedPropertyValidation.tsx +1 -1
- package/src/ui/collection_editor/properties/validation/ArrayPropertyValidation.tsx +1 -1
- package/src/ui/collection_editor/properties/validation/GeneralPropertyValidation.tsx +1 -1
- package/src/ui/collection_editor/properties/validation/NumberPropertyValidation.tsx +1 -1
- package/src/ui/collection_editor/properties/validation/StringPropertyValidation.tsx +2 -2
- package/src/useCollectionEditorPlugin.tsx +50 -31
- package/dist/form/Field.d.ts +0 -53
- package/dist/form/Formex.d.ts +0 -4
- package/dist/form/index.d.ts +0 -5
- package/dist/form/types.d.ts +0 -25
- package/dist/form/useCreateFormex.d.ts +0 -9
- package/dist/form/utils.d.ts +0 -44
- package/src/form/Field.tsx +0 -162
- package/src/form/Formex.tsx +0 -8
- package/src/form/README.md +0 -165
- package/src/form/index.ts +0 -5
- package/src/form/types.ts +0 -27
- package/src/form/useCreateFormex.tsx +0 -137
- package/src/form/utils.ts +0 -169
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import React, { FormEvent, useEffect, useState } from "react";
|
|
2
|
-
import { setIn } from "./utils";
|
|
3
|
-
import { FormexController, FormexResetProps } from "./types";
|
|
4
|
-
|
|
5
|
-
export function useCreateFormex<T extends object>({ initialValues, initialErrors, validation, validateOnChange = false, onSubmit, validateOnInitialRender = false }: {
|
|
6
|
-
initialValues: T,
|
|
7
|
-
initialErrors?: Record<string, string>,
|
|
8
|
-
validateOnChange?: boolean,
|
|
9
|
-
validateOnInitialRender?: boolean,
|
|
10
|
-
validation?: (values: T) => Record<string, string>,
|
|
11
|
-
onSubmit?: (values: T, controller: FormexController<T>) => void | Promise<void>
|
|
12
|
-
}): FormexController<T> {
|
|
13
|
-
|
|
14
|
-
const valuesRef = React.useRef<T>(initialValues);
|
|
15
|
-
|
|
16
|
-
const [values, setValuesInner] = useState<T>(initialValues);
|
|
17
|
-
const [touchedState, setTouchedState] = useState<Record<string, boolean>>({});
|
|
18
|
-
const [errors, setErrors] = useState<Record<string, string>>(initialErrors ?? {});
|
|
19
|
-
const [dirty, setDirty] = useState(false);
|
|
20
|
-
const [submitCount, setSubmitCount] = useState(0);
|
|
21
|
-
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
22
|
-
|
|
23
|
-
useEffect(() => {
|
|
24
|
-
if (validateOnInitialRender) {
|
|
25
|
-
validate();
|
|
26
|
-
}
|
|
27
|
-
}, []);
|
|
28
|
-
|
|
29
|
-
const setValues = (newValues: T) => {
|
|
30
|
-
valuesRef.current = newValues;
|
|
31
|
-
setValuesInner(newValues);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const validate = () => {
|
|
35
|
-
const values = valuesRef.current;
|
|
36
|
-
const validationErrors = validation?.(values);
|
|
37
|
-
setErrors(validationErrors ?? {});
|
|
38
|
-
return validationErrors;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const setFieldValue = (key: string, value: any, shouldValidate?: boolean) => {
|
|
42
|
-
const newValues = setIn(valuesRef.current, key, value);
|
|
43
|
-
valuesRef.current = newValues;
|
|
44
|
-
setValues(newValues);
|
|
45
|
-
if (shouldValidate) {
|
|
46
|
-
validate();
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const setFieldError = (key: string, error: string | undefined) => {
|
|
51
|
-
console.log("setFieldError", {key, error, errors })
|
|
52
|
-
const newErrors = { ...errors };
|
|
53
|
-
if (error) {
|
|
54
|
-
newErrors[key] = error;
|
|
55
|
-
} else {
|
|
56
|
-
delete newErrors[key];
|
|
57
|
-
}
|
|
58
|
-
setErrors(newErrors);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const setFieldTouched = (key: string, touched: boolean, shouldValidate?: boolean | undefined) => {
|
|
62
|
-
const newTouched = { ...touchedState };
|
|
63
|
-
newTouched[key] = touched;
|
|
64
|
-
setTouchedState(newTouched);
|
|
65
|
-
if (shouldValidate) {
|
|
66
|
-
validate();
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const handleChange = (event: React.SyntheticEvent) => {
|
|
71
|
-
const target = event.target as HTMLInputElement;
|
|
72
|
-
const value = target.type === "checkbox" ? target.checked : target.value;
|
|
73
|
-
const name = target.name;
|
|
74
|
-
setFieldValue(name, value, validateOnChange);
|
|
75
|
-
setFieldTouched(name, true);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const handleBlur = (event: React.FocusEvent) => {
|
|
79
|
-
console.log("handleBlur")
|
|
80
|
-
const target = event.target as HTMLInputElement;
|
|
81
|
-
const name = target.name;
|
|
82
|
-
setFieldTouched(name, true);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const submit = async (e?: FormEvent<HTMLFormElement>) => {
|
|
86
|
-
e?.preventDefault();
|
|
87
|
-
e?.stopPropagation();
|
|
88
|
-
setIsSubmitting(true);
|
|
89
|
-
setSubmitCount(submitCount + 1);
|
|
90
|
-
const validationErrors = validation?.(valuesRef.current);
|
|
91
|
-
if (validationErrors && Object.keys(validationErrors).length > 0) {
|
|
92
|
-
setErrors(validationErrors);
|
|
93
|
-
} else {
|
|
94
|
-
setErrors({});
|
|
95
|
-
await onSubmit?.(valuesRef.current, controllerRef.current);
|
|
96
|
-
}
|
|
97
|
-
setIsSubmitting(false);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const resetForm = (props?: FormexResetProps<T>) => {
|
|
101
|
-
const {
|
|
102
|
-
values: valuesProp,
|
|
103
|
-
errors: errorsProp,
|
|
104
|
-
touched: touchedProp
|
|
105
|
-
} = props ?? {};
|
|
106
|
-
valuesRef.current = valuesProp ?? initialValues;
|
|
107
|
-
setValues(valuesProp ?? initialValues);
|
|
108
|
-
setErrors(errorsProp ?? {});
|
|
109
|
-
setTouchedState(touchedProp ?? {});
|
|
110
|
-
setDirty(false);
|
|
111
|
-
setSubmitCount(0);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const controller: FormexController<T> = {
|
|
115
|
-
values,
|
|
116
|
-
handleChange,
|
|
117
|
-
isSubmitting,
|
|
118
|
-
setValues,
|
|
119
|
-
setFieldValue,
|
|
120
|
-
errors,
|
|
121
|
-
setFieldError,
|
|
122
|
-
touched: touchedState,
|
|
123
|
-
setFieldTouched,
|
|
124
|
-
dirty,
|
|
125
|
-
setDirty,
|
|
126
|
-
submitForm: submit,
|
|
127
|
-
submitCount,
|
|
128
|
-
setSubmitCount,
|
|
129
|
-
handleBlur,
|
|
130
|
-
validate,
|
|
131
|
-
resetForm
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
const controllerRef = React.useRef<FormexController<T>>(controller);
|
|
135
|
-
controllerRef.current = controller;
|
|
136
|
-
return controller
|
|
137
|
-
}
|
package/src/form/utils.ts
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
|
|
3
|
-
/** @private is the value an empty array? */
|
|
4
|
-
export const isEmptyArray = (value?: any) =>
|
|
5
|
-
Array.isArray(value) && value.length === 0;
|
|
6
|
-
|
|
7
|
-
/** @private is the given object a Function? */
|
|
8
|
-
export const isFunction = (obj: any): obj is Function =>
|
|
9
|
-
typeof obj === "function";
|
|
10
|
-
|
|
11
|
-
/** @private is the given object an Object? */
|
|
12
|
-
export const isObject = (obj: any): obj is Object =>
|
|
13
|
-
obj !== null && typeof obj === "object";
|
|
14
|
-
|
|
15
|
-
/** @private is the given object an integer? */
|
|
16
|
-
export const isInteger = (obj: any): boolean =>
|
|
17
|
-
String(Math.floor(Number(obj))) === obj;
|
|
18
|
-
|
|
19
|
-
/** @private is the given object a string? */
|
|
20
|
-
export const isString = (obj: any): obj is string =>
|
|
21
|
-
Object.prototype.toString.call(obj) === "[object String]";
|
|
22
|
-
|
|
23
|
-
/** @private is the given object a NaN? */
|
|
24
|
-
// eslint-disable-next-line no-self-compare
|
|
25
|
-
export const isNaN = (obj: any): boolean => obj !== obj;
|
|
26
|
-
|
|
27
|
-
/** @private Does a React component have exactly 0 children? */
|
|
28
|
-
export const isEmptyChildren = (children: any): boolean =>
|
|
29
|
-
React.Children.count(children) === 0;
|
|
30
|
-
|
|
31
|
-
/** @private is the given object/value a promise? */
|
|
32
|
-
export const isPromise = (value: any): value is PromiseLike<any> =>
|
|
33
|
-
isObject(value) && isFunction(value.then);
|
|
34
|
-
|
|
35
|
-
/** @private is the given object/value a type of synthetic event? */
|
|
36
|
-
export const isInputEvent = (value: any): value is React.SyntheticEvent<any> =>
|
|
37
|
-
value && isObject(value) && isObject(value.target);
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Same as document.activeElement but wraps in a try-catch block. In IE it is
|
|
41
|
-
* not safe to call document.activeElement if there is nothing focused.
|
|
42
|
-
*
|
|
43
|
-
* The activeElement will be null only if the document or document body is not
|
|
44
|
-
* yet defined.
|
|
45
|
-
*
|
|
46
|
-
* @param {?Document} doc Defaults to current document.
|
|
47
|
-
* @return {Element | null}
|
|
48
|
-
* @see https://github.com/facebook/fbjs/blob/master/packages/fbjs/src/core/dom/getActiveElement.js
|
|
49
|
-
*/
|
|
50
|
-
export function getActiveElement(doc?: Document): Element | null {
|
|
51
|
-
doc = doc || (typeof document !== "undefined" ? document : undefined);
|
|
52
|
-
if (typeof doc === "undefined") {
|
|
53
|
-
return null;
|
|
54
|
-
}
|
|
55
|
-
try {
|
|
56
|
-
return doc.activeElement || doc.body;
|
|
57
|
-
} catch (e) {
|
|
58
|
-
return doc.body;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Deeply get a value from an object via its path.
|
|
64
|
-
*/
|
|
65
|
-
export function getIn(
|
|
66
|
-
obj: any,
|
|
67
|
-
key: string | string[],
|
|
68
|
-
def?: any,
|
|
69
|
-
p = 0
|
|
70
|
-
) {
|
|
71
|
-
const path = toPath(key);
|
|
72
|
-
while (obj && p < path.length) {
|
|
73
|
-
obj = obj[path[p++]];
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// check if path is not in the end
|
|
77
|
-
if (p !== path.length && !obj) {
|
|
78
|
-
return def;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return obj === undefined ? def : obj;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export function setIn(obj: any, path: string, value: any): any {
|
|
85
|
-
const res: any = clone(obj); // this keeps inheritance when obj is a class
|
|
86
|
-
let resVal: any = res;
|
|
87
|
-
let i = 0;
|
|
88
|
-
const pathArray = toPath(path);
|
|
89
|
-
|
|
90
|
-
for (; i < pathArray.length - 1; i++) {
|
|
91
|
-
const currentPath: string = pathArray[i];
|
|
92
|
-
const currentObj: any = getIn(obj, pathArray.slice(0, i + 1));
|
|
93
|
-
|
|
94
|
-
if (currentObj && (isObject(currentObj) || Array.isArray(currentObj))) {
|
|
95
|
-
resVal = resVal[currentPath] = clone(currentObj);
|
|
96
|
-
} else {
|
|
97
|
-
const nextPath: string = pathArray[i + 1];
|
|
98
|
-
resVal = resVal[currentPath] =
|
|
99
|
-
isInteger(nextPath) && Number(nextPath) >= 0 ? [] : {};
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// Return original object if new value is the same as current
|
|
104
|
-
if ((i === 0 ? obj : resVal)[pathArray[i]] === value) {
|
|
105
|
-
return obj;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (value === undefined) {
|
|
109
|
-
delete resVal[pathArray[i]];
|
|
110
|
-
} else {
|
|
111
|
-
resVal[pathArray[i]] = value;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// If the path array has a single element, the loop did not run.
|
|
115
|
-
// Deleting on `resVal` had no effect in this scenario, so we delete on the result instead.
|
|
116
|
-
if (i === 0 && value === undefined) {
|
|
117
|
-
delete res[pathArray[i]];
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return res;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Recursively a set the same value for all keys and arrays nested object, cloning
|
|
125
|
-
* @param object
|
|
126
|
-
* @param value
|
|
127
|
-
* @param visited
|
|
128
|
-
* @param response
|
|
129
|
-
*/
|
|
130
|
-
export function setNestedObjectValues<T>(
|
|
131
|
-
object: any,
|
|
132
|
-
value: any,
|
|
133
|
-
visited: any = new WeakMap(),
|
|
134
|
-
response: any = {}
|
|
135
|
-
): T {
|
|
136
|
-
for (const k of Object.keys(object)) {
|
|
137
|
-
const val = object[k];
|
|
138
|
-
if (isObject(val)) {
|
|
139
|
-
if (!visited.get(val)) {
|
|
140
|
-
visited.set(val, true);
|
|
141
|
-
// In order to keep array values consistent for both dot path and
|
|
142
|
-
// bracket syntax, we need to check if this is an array so that
|
|
143
|
-
// this will output { friends: [true] } and not { friends: { "0": true } }
|
|
144
|
-
response[k] = Array.isArray(val) ? [] : {};
|
|
145
|
-
setNestedObjectValues(val, value, visited, response[k]);
|
|
146
|
-
}
|
|
147
|
-
} else {
|
|
148
|
-
response[k] = value;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return response;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
function clone(value: any) {
|
|
156
|
-
if (Array.isArray(value)) {
|
|
157
|
-
return [...value];
|
|
158
|
-
} else if (typeof value === "object" && value !== null) {
|
|
159
|
-
return { ...value };
|
|
160
|
-
} else {
|
|
161
|
-
return value; // This is for primitive types which do not need cloning.
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
function toPath(value: string | string[]) {
|
|
166
|
-
if (Array.isArray(value)) return value; // Already in path array form.
|
|
167
|
-
// Replace brackets with dots, remove leading/trailing dots, then split by dot.
|
|
168
|
-
return value.replace(/\[(\d+)]/g, ".$1").replace(/^\./, "").replace(/\.$/, "").split(".");
|
|
169
|
-
}
|