@react-fabric/form 1.0.0-beta.0
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/LICENSE +21 -0
- package/README.md +10 -0
- package/dist/cjs/builder/FileField.js +131 -0
- package/dist/cjs/builder/FormField.js +119 -0
- package/dist/cjs/form/Controller.js +64 -0
- package/dist/cjs/form/Form.js +128 -0
- package/dist/cjs/hooks/useFileUploader.js +204 -0
- package/dist/cjs/hooks/useFormBuilder.js +87 -0
- package/dist/cjs/i18n/ar.json.js +37 -0
- package/dist/cjs/i18n/en.json.js +38 -0
- package/dist/cjs/i18n/index.js +33 -0
- package/dist/cjs/index.js +56 -0
- package/dist/cjs/input/ArrayInput.js +189 -0
- package/dist/cjs/input/ArrayInput.module.css +1 -0
- package/dist/cjs/input/AvatarInput.js +141 -0
- package/dist/cjs/input/Checkbox.js +83 -0
- package/dist/cjs/input/ColorInput.js +100 -0
- package/dist/cjs/input/ColorPicker.js +122 -0
- package/dist/cjs/input/ColorPicker.module.css +1 -0
- package/dist/cjs/input/Field.js +31 -0
- package/dist/cjs/input/File.js +165 -0
- package/dist/cjs/input/File.module.css +1 -0
- package/dist/cjs/input/Hidden.js +54 -0
- package/dist/cjs/input/Input.js +79 -0
- package/dist/cjs/input/Number.js +80 -0
- package/dist/cjs/input/Password.js +104 -0
- package/dist/cjs/input/Radio.js +70 -0
- package/dist/cjs/input/RadioGroup.js +31 -0
- package/dist/cjs/input/Range.js +169 -0
- package/dist/cjs/input/Search.js +109 -0
- package/dist/cjs/input/Slider.js +172 -0
- package/dist/cjs/input/Switch.js +79 -0
- package/dist/cjs/input/Textarea.js +87 -0
- package/dist/cjs/internal/ErrorIcon.js +32 -0
- package/dist/cjs/internal/Field.module.css +1 -0
- package/dist/cjs/internal/FieldWrapper.js +78 -0
- package/dist/cjs/internal/InputWrapper.js +113 -0
- package/dist/cjs/ranger/index.js +352 -0
- package/dist/cjs/ranger/utils.js +49 -0
- package/dist/cjs/select/AutoComplete.js +268 -0
- package/dist/cjs/select/DualList.js +238 -0
- package/dist/cjs/select/List.js +241 -0
- package/dist/cjs/select/Options.js +76 -0
- package/dist/cjs/select/Select.js +283 -0
- package/dist/cjs/select/helpers.js +41 -0
- package/dist/cjs/select/useSelect.js +337 -0
- package/dist/cjs/utils/eventHandlers.js +50 -0
- package/dist/cjs/utils/getColor.js +40 -0
- package/dist/esm/builder/FileField.js +131 -0
- package/dist/esm/builder/FormField.js +119 -0
- package/dist/esm/form/Controller.js +64 -0
- package/dist/esm/form/Form.js +128 -0
- package/dist/esm/hooks/useFileUploader.js +204 -0
- package/dist/esm/hooks/useFormBuilder.js +87 -0
- package/dist/esm/i18n/ar.json.js +31 -0
- package/dist/esm/i18n/en.json.js +32 -0
- package/dist/esm/i18n/index.js +33 -0
- package/dist/esm/index.js +28 -0
- package/dist/esm/input/ArrayInput.js +189 -0
- package/dist/esm/input/ArrayInput.module.css +1 -0
- package/dist/esm/input/AvatarInput.js +141 -0
- package/dist/esm/input/Checkbox.js +83 -0
- package/dist/esm/input/ColorInput.js +100 -0
- package/dist/esm/input/ColorPicker.js +122 -0
- package/dist/esm/input/ColorPicker.module.css +1 -0
- package/dist/esm/input/Field.js +31 -0
- package/dist/esm/input/File.js +165 -0
- package/dist/esm/input/File.module.css +1 -0
- package/dist/esm/input/Hidden.js +54 -0
- package/dist/esm/input/Input.js +79 -0
- package/dist/esm/input/Number.js +80 -0
- package/dist/esm/input/Password.js +104 -0
- package/dist/esm/input/Radio.js +70 -0
- package/dist/esm/input/RadioGroup.js +31 -0
- package/dist/esm/input/Range.js +169 -0
- package/dist/esm/input/Search.js +109 -0
- package/dist/esm/input/Slider.js +172 -0
- package/dist/esm/input/Switch.js +79 -0
- package/dist/esm/input/Textarea.js +87 -0
- package/dist/esm/internal/ErrorIcon.js +32 -0
- package/dist/esm/internal/Field.module.css +1 -0
- package/dist/esm/internal/FieldWrapper.js +78 -0
- package/dist/esm/internal/InputWrapper.js +113 -0
- package/dist/esm/ranger/index.js +352 -0
- package/dist/esm/ranger/utils.js +47 -0
- package/dist/esm/select/AutoComplete.js +268 -0
- package/dist/esm/select/DualList.js +238 -0
- package/dist/esm/select/List.js +241 -0
- package/dist/esm/select/Options.js +76 -0
- package/dist/esm/select/Select.js +283 -0
- package/dist/esm/select/helpers.js +40 -0
- package/dist/esm/select/useSelect.js +337 -0
- package/dist/esm/utils/eventHandlers.js +50 -0
- package/dist/esm/utils/getColor.js +39 -0
- package/dist/types/builder/FileField.d.ts +8 -0
- package/dist/types/builder/FormField.d.ts +9 -0
- package/dist/types/form/Controller.d.ts +20 -0
- package/dist/types/form/Form.d.ts +33 -0
- package/dist/types/hooks/useFileUploader.d.ts +23 -0
- package/dist/types/hooks/useFormBuilder.d.ts +16 -0
- package/dist/types/i18n/index.d.ts +1 -0
- package/dist/types/index.d.ts +27 -0
- package/dist/types/input/ArrayInput.d.ts +64 -0
- package/dist/types/input/AvatarInput.d.ts +31 -0
- package/dist/types/input/Checkbox.d.ts +46 -0
- package/dist/types/input/ColorInput.d.ts +10 -0
- package/dist/types/input/ColorPicker.d.ts +24 -0
- package/dist/types/input/Field.d.ts +41 -0
- package/dist/types/input/File.d.ts +17 -0
- package/dist/types/input/Hidden.d.ts +16 -0
- package/dist/types/input/Input.d.ts +9 -0
- package/dist/types/input/Number.d.ts +17 -0
- package/dist/types/input/Password.d.ts +13 -0
- package/dist/types/input/Radio.d.ts +42 -0
- package/dist/types/input/RadioGroup.d.ts +30 -0
- package/dist/types/input/Range.d.ts +59 -0
- package/dist/types/input/Search.d.ts +17 -0
- package/dist/types/input/Slider.d.ts +63 -0
- package/dist/types/input/Switch.d.ts +42 -0
- package/dist/types/input/Textarea.d.ts +17 -0
- package/dist/types/internal/ErrorIcon.d.ts +1 -0
- package/dist/types/internal/FieldWrapper.d.ts +1 -0
- package/dist/types/internal/InputWrapper.d.ts +1 -0
- package/dist/types/ranger/index.d.ts +70 -0
- package/dist/types/ranger/utils.d.ts +25 -0
- package/dist/types/select/AutoComplete.d.ts +17 -0
- package/dist/types/select/DualList.d.ts +38 -0
- package/dist/types/select/List.d.ts +4 -0
- package/dist/types/select/Options.d.ts +14 -0
- package/dist/types/select/Select.d.ts +2 -0
- package/dist/types/select/helpers.d.ts +2 -0
- package/dist/types/select/useSelect.d.ts +50 -0
- package/dist/types/types/index.d.ts +169 -0
- package/dist/types/types/schema.d.ts +59 -0
- package/dist/types/utils/eventHandlers.d.ts +11 -0
- package/dist/types/utils/getColor.d.ts +4 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/package.json +64 -0
- package/src/builder/FileField.tsx +122 -0
- package/src/builder/FormField.tsx +161 -0
- package/src/form/Controller.tsx +103 -0
- package/src/form/Form.tsx +160 -0
- package/src/hooks/useFileUploader.ts +234 -0
- package/src/hooks/useFormBuilder.tsx +128 -0
- package/src/i18n/ar.json +23 -0
- package/src/i18n/en.json +24 -0
- package/src/i18n/index.ts +28 -0
- package/src/index.ts +54 -0
- package/src/input/ArrayInput.module.css +41 -0
- package/src/input/ArrayInput.tsx +318 -0
- package/src/input/AvatarInput.tsx +150 -0
- package/src/input/Checkbox.tsx +165 -0
- package/src/input/ColorInput.tsx +157 -0
- package/src/input/ColorPicker.module.css +51 -0
- package/src/input/ColorPicker.tsx +210 -0
- package/src/input/Field.tsx +96 -0
- package/src/input/File.module.css +42 -0
- package/src/input/File.tsx +224 -0
- package/src/input/Hidden.tsx +65 -0
- package/src/input/Input.tsx +114 -0
- package/src/input/Number.tsx +126 -0
- package/src/input/Password.tsx +150 -0
- package/src/input/Radio.tsx +146 -0
- package/src/input/RadioGroup.tsx +75 -0
- package/src/input/Range.tsx +276 -0
- package/src/input/Search.tsx +160 -0
- package/src/input/Slider.tsx +282 -0
- package/src/input/Switch.tsx +168 -0
- package/src/input/Textarea.tsx +127 -0
- package/src/internal/ErrorIcon.tsx +61 -0
- package/src/internal/Field.module.css +139 -0
- package/src/internal/FieldWrapper.tsx +84 -0
- package/src/internal/InputWrapper.tsx +187 -0
- package/src/ranger/index.tsx +514 -0
- package/src/ranger/utils.ts +48 -0
- package/src/select/AutoComplete.tsx +354 -0
- package/src/select/DualList.tsx +286 -0
- package/src/select/List.tsx +288 -0
- package/src/select/Options.tsx +121 -0
- package/src/select/Select.tsx +350 -0
- package/src/select/helpers.ts +36 -0
- package/src/select/useSelect.tsx +413 -0
- package/src/types/index.ts +203 -0
- package/src/types/schema.ts +100 -0
- package/src/utils/eventHandlers.ts +72 -0
- package/src/utils/getColor.ts +47 -0
- package/src/utils/index.ts +25 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2024 Adarsh Pastakia
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
+
* subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
+
* portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
Children,
|
|
26
|
+
cloneElement,
|
|
27
|
+
isValidElement,
|
|
28
|
+
type ReactElement,
|
|
29
|
+
} from "react";
|
|
30
|
+
import { Controller as HFController } from "react-hook-form";
|
|
31
|
+
|
|
32
|
+
export interface ControllerProps {
|
|
33
|
+
/**
|
|
34
|
+
* field name
|
|
35
|
+
*/
|
|
36
|
+
name?: string;
|
|
37
|
+
children:
|
|
38
|
+
| ReactElement
|
|
39
|
+
| ((props: {
|
|
40
|
+
name: string;
|
|
41
|
+
error?: string;
|
|
42
|
+
checked: boolean;
|
|
43
|
+
value: AnyObject;
|
|
44
|
+
invalid?: boolean;
|
|
45
|
+
readOnly?: boolean;
|
|
46
|
+
disabled?: boolean;
|
|
47
|
+
onBlur: () => void;
|
|
48
|
+
onChange: (value: AnyObject) => void;
|
|
49
|
+
inputRef?: React.Ref<HTMLInputElement>;
|
|
50
|
+
}) => ReactElement);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const Controller = ({ name, children, ...rest }: ControllerProps) => {
|
|
54
|
+
if (!name) throw Error("Form Controller missing `name` prop");
|
|
55
|
+
return (
|
|
56
|
+
<HFController
|
|
57
|
+
name={name}
|
|
58
|
+
render={({
|
|
59
|
+
field: { ref, onChange, ...field },
|
|
60
|
+
fieldState: { error },
|
|
61
|
+
formState: { isSubmitting },
|
|
62
|
+
}) =>
|
|
63
|
+
isValidElement<AnyObject>(children)
|
|
64
|
+
? cloneElement(Children.only(children), {
|
|
65
|
+
...field,
|
|
66
|
+
...rest,
|
|
67
|
+
ref: (el: AnyObject) => {
|
|
68
|
+
el && ref(el);
|
|
69
|
+
try {
|
|
70
|
+
if (children.props.ref && "current" in children.props.ref)
|
|
71
|
+
children.props.ref.current = el;
|
|
72
|
+
if (children.props.ref?.prototype) children.props.ref(el);
|
|
73
|
+
} catch (_) {
|
|
74
|
+
//
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
checked: !!field.value,
|
|
78
|
+
readOnly: isSubmitting || children.props.readOnly,
|
|
79
|
+
invalid: !!error?.message || children.props.invalid,
|
|
80
|
+
error: error?.message ?? children.props.error,
|
|
81
|
+
onChange: (v: AnyObject) => {
|
|
82
|
+
onChange(v);
|
|
83
|
+
children.props.onChange?.(v);
|
|
84
|
+
},
|
|
85
|
+
})
|
|
86
|
+
: children({
|
|
87
|
+
...field,
|
|
88
|
+
...rest,
|
|
89
|
+
inputRef: (el: AnyObject) => {
|
|
90
|
+
el && ref(el);
|
|
91
|
+
},
|
|
92
|
+
readOnly: isSubmitting,
|
|
93
|
+
invalid: !!error?.message,
|
|
94
|
+
checked: !!field.value,
|
|
95
|
+
error: error?.message,
|
|
96
|
+
onChange: (v: AnyObject) => {
|
|
97
|
+
onChange(v);
|
|
98
|
+
},
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
/>
|
|
102
|
+
);
|
|
103
|
+
};
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2024 Adarsh Pastakia
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
+
* subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
+
* portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { yupResolver } from "@hookform/resolvers/yup";
|
|
25
|
+
import { useDebounce } from "@react-fabric/core";
|
|
26
|
+
import { type yup } from "@react-fabric/utilities";
|
|
27
|
+
import {
|
|
28
|
+
useCallback,
|
|
29
|
+
useEffect,
|
|
30
|
+
useImperativeHandle,
|
|
31
|
+
useRef,
|
|
32
|
+
useTransition,
|
|
33
|
+
type PropsWithChildren,
|
|
34
|
+
type Ref,
|
|
35
|
+
} from "react";
|
|
36
|
+
import {
|
|
37
|
+
FormProvider,
|
|
38
|
+
useForm,
|
|
39
|
+
type DefaultValues,
|
|
40
|
+
type FormState,
|
|
41
|
+
} from "react-hook-form";
|
|
42
|
+
|
|
43
|
+
export interface FormRef<K extends KeyValue> {
|
|
44
|
+
state: FormState<K>;
|
|
45
|
+
reset: () => void;
|
|
46
|
+
clear: () => void;
|
|
47
|
+
submit: () => void;
|
|
48
|
+
validate: () => Promise<boolean>;
|
|
49
|
+
getValues: () => K;
|
|
50
|
+
setValue: (key: keyof K, value: AnyObject) => void;
|
|
51
|
+
setValues: (values: K) => void;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface FormProps<K extends KeyValue = KeyValue> {
|
|
55
|
+
formRef?: Ref<FormRef<K>>;
|
|
56
|
+
/**
|
|
57
|
+
* form data schema
|
|
58
|
+
*/
|
|
59
|
+
schema?: yup.ObjectSchema<K>;
|
|
60
|
+
/**
|
|
61
|
+
* default data values
|
|
62
|
+
*/
|
|
63
|
+
defaultValues?: K;
|
|
64
|
+
/**
|
|
65
|
+
* change callback
|
|
66
|
+
*/
|
|
67
|
+
onChange?: (values: K) => void;
|
|
68
|
+
/**
|
|
69
|
+
* submit callback after successful validation
|
|
70
|
+
*/
|
|
71
|
+
onSubmit?: (values: K) => void;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const DEFAULT_SUBMIT = () => undefined;
|
|
75
|
+
|
|
76
|
+
export const Form = <K extends KeyValue>({
|
|
77
|
+
formRef,
|
|
78
|
+
schema,
|
|
79
|
+
children,
|
|
80
|
+
defaultValues,
|
|
81
|
+
onSubmit = DEFAULT_SUBMIT,
|
|
82
|
+
onChange,
|
|
83
|
+
...rest
|
|
84
|
+
}: PropsWithChildren<FormProps<K>>) => {
|
|
85
|
+
const ref = useRef<HTMLFormElement>(null);
|
|
86
|
+
const form = useForm<K>({
|
|
87
|
+
shouldFocusError: true,
|
|
88
|
+
resolver: schema && (yupResolver(schema) as AnyObject),
|
|
89
|
+
defaultValues: defaultValues as DefaultValues<K>,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const [, startTransition] = useTransition();
|
|
93
|
+
const changeHandler = useDebounce(onChange, [onChange]);
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
const subscription = form.watch((value) => {
|
|
96
|
+
startTransition(() => changeHandler?.(value as K));
|
|
97
|
+
});
|
|
98
|
+
return () => subscription.unsubscribe();
|
|
99
|
+
}, [form.watch, changeHandler]);
|
|
100
|
+
|
|
101
|
+
const handleReset = useCallback(
|
|
102
|
+
(reset: AnyObject = {}) => {
|
|
103
|
+
form.reset(reset);
|
|
104
|
+
form.clearErrors();
|
|
105
|
+
ref.current
|
|
106
|
+
?.querySelector<HTMLElement>("[auto-focus], input, textarea")
|
|
107
|
+
?.focus();
|
|
108
|
+
},
|
|
109
|
+
[defaultValues],
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
useImperativeHandle(
|
|
113
|
+
formRef,
|
|
114
|
+
() => ({
|
|
115
|
+
state: form.formState,
|
|
116
|
+
reset: () => handleReset(defaultValues),
|
|
117
|
+
clear: () => handleReset(),
|
|
118
|
+
submit: () => form.handleSubmit(onSubmit),
|
|
119
|
+
validate: async () => await form.trigger(),
|
|
120
|
+
getValues: () => form.getValues(),
|
|
121
|
+
setValues: (v) => form.reset(v),
|
|
122
|
+
setValue: (k, v) =>
|
|
123
|
+
form.setValue(k as AnyObject, v, {
|
|
124
|
+
shouldDirty: false,
|
|
125
|
+
shouldTouch: false,
|
|
126
|
+
}),
|
|
127
|
+
}),
|
|
128
|
+
[onSubmit, form, form.formState, defaultValues],
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
const onInvalid = useCallback((_: AnyObject, e: AnyObject) => {
|
|
132
|
+
const el = e.target;
|
|
133
|
+
setTimeout(
|
|
134
|
+
() =>
|
|
135
|
+
el
|
|
136
|
+
.querySelector(
|
|
137
|
+
"input[aria-invalid='true'],textarea[aria-invalid='true']",
|
|
138
|
+
)
|
|
139
|
+
?.focus(),
|
|
140
|
+
10,
|
|
141
|
+
);
|
|
142
|
+
}, []);
|
|
143
|
+
|
|
144
|
+
return (
|
|
145
|
+
<FormProvider {...form}>
|
|
146
|
+
<form
|
|
147
|
+
ref={ref}
|
|
148
|
+
onReset={() => handleReset(defaultValues)}
|
|
149
|
+
onSubmit={form.handleSubmit(onSubmit, onInvalid) as AnyObject}
|
|
150
|
+
data-loading={form.formState.isSubmitting}
|
|
151
|
+
className="contents"
|
|
152
|
+
autoComplete="off"
|
|
153
|
+
{...rest}
|
|
154
|
+
>
|
|
155
|
+
{children}
|
|
156
|
+
<input type="submit" className="absolute invisible" />
|
|
157
|
+
</form>
|
|
158
|
+
</FormProvider>
|
|
159
|
+
);
|
|
160
|
+
};
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2024 Adarsh Pastakia
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
+
* subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
+
* portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { isArray, isObject, isString, uuid } from "@react-fabric/utilities";
|
|
25
|
+
import { type AxiosRequestConfig } from "axios";
|
|
26
|
+
import { useCallback, useReducer } from "react";
|
|
27
|
+
|
|
28
|
+
export type UploadHandler = (
|
|
29
|
+
data: FormData,
|
|
30
|
+
config: AxiosRequestConfig,
|
|
31
|
+
) => Promise<string>;
|
|
32
|
+
|
|
33
|
+
interface FileInfo {
|
|
34
|
+
key: string;
|
|
35
|
+
filename: string;
|
|
36
|
+
size: number;
|
|
37
|
+
mime: string;
|
|
38
|
+
uploaded?: true;
|
|
39
|
+
file?: File;
|
|
40
|
+
path?: string;
|
|
41
|
+
error?: string;
|
|
42
|
+
progress?: number;
|
|
43
|
+
abort?: AbortController["abort"];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface UploaderState {
|
|
47
|
+
files: Map<string, FileInfo>;
|
|
48
|
+
list: FileInfo[];
|
|
49
|
+
count: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
type UploaderActions =
|
|
53
|
+
| { type: "start"; key: string; file: FileInfo }
|
|
54
|
+
| { type: "stop"; key: string; path: string }
|
|
55
|
+
| { type: "error"; key: string; error: string }
|
|
56
|
+
| { type: "progress"; key: string; progress: number }
|
|
57
|
+
| { type: "remove"; key: string }
|
|
58
|
+
| { type: "abort"; key: string };
|
|
59
|
+
|
|
60
|
+
export const useFileUploader = (
|
|
61
|
+
uploader: UploadHandler,
|
|
62
|
+
initialList?: string | FileInfo | FileInfo[],
|
|
63
|
+
multiple = true,
|
|
64
|
+
) => {
|
|
65
|
+
const [state, dispatch] = useReducer(
|
|
66
|
+
(state: UploaderState, action: UploaderActions) => {
|
|
67
|
+
if (action.type === "start") {
|
|
68
|
+
if (!multiple) state.files.clear();
|
|
69
|
+
state.files.set(action.key, action.file);
|
|
70
|
+
state.count++;
|
|
71
|
+
}
|
|
72
|
+
if (action.type === "stop") {
|
|
73
|
+
const fileObject = state.files.get(action.key);
|
|
74
|
+
if (fileObject) {
|
|
75
|
+
const { progress, abort, ...rest } = fileObject;
|
|
76
|
+
state.files.set(action.key, {
|
|
77
|
+
...rest,
|
|
78
|
+
path: action.path,
|
|
79
|
+
uploaded: true,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
state.count--;
|
|
83
|
+
}
|
|
84
|
+
if (action.type === "error") {
|
|
85
|
+
const fileObject = state.files.get(action.key);
|
|
86
|
+
if (fileObject) {
|
|
87
|
+
const { file, progress, abort, ...rest } = fileObject;
|
|
88
|
+
state.files.set(action.key, { ...rest, error: action.error });
|
|
89
|
+
}
|
|
90
|
+
state.count--;
|
|
91
|
+
}
|
|
92
|
+
if (action.type === "progress") {
|
|
93
|
+
const fileObject = state.files.get(action.key);
|
|
94
|
+
if (fileObject) {
|
|
95
|
+
fileObject.progress = action.progress;
|
|
96
|
+
state.files.set(action.key, fileObject);
|
|
97
|
+
}
|
|
98
|
+
state.count--;
|
|
99
|
+
}
|
|
100
|
+
if (action.type === "abort") {
|
|
101
|
+
state.files.delete(action.key);
|
|
102
|
+
}
|
|
103
|
+
if (action.type === "remove") {
|
|
104
|
+
state.files.delete(action.key);
|
|
105
|
+
}
|
|
106
|
+
state.list = Array.from(state.files.values())
|
|
107
|
+
.map((file) => {
|
|
108
|
+
return (
|
|
109
|
+
!file.progress &&
|
|
110
|
+
!file.error && {
|
|
111
|
+
key: file.key,
|
|
112
|
+
path: file.path,
|
|
113
|
+
mime: file.mime,
|
|
114
|
+
size: file.size,
|
|
115
|
+
uploaded: file.uploaded,
|
|
116
|
+
filename: file.filename,
|
|
117
|
+
}
|
|
118
|
+
);
|
|
119
|
+
})
|
|
120
|
+
.filter(Boolean) as FileInfo[];
|
|
121
|
+
return { ...state };
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
files: new Map(),
|
|
125
|
+
count: 0,
|
|
126
|
+
} as UploaderState,
|
|
127
|
+
(state: UploaderState) => {
|
|
128
|
+
if (initialList) {
|
|
129
|
+
if (isString(initialList)) {
|
|
130
|
+
const key = uuid();
|
|
131
|
+
state.files.set(key, {
|
|
132
|
+
key,
|
|
133
|
+
path: initialList,
|
|
134
|
+
filename: "",
|
|
135
|
+
size: 0,
|
|
136
|
+
mime: "",
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
if (isObject(initialList)) {
|
|
140
|
+
const key = uuid();
|
|
141
|
+
state.files.set(key, { ...(initialList as AnyObject), key });
|
|
142
|
+
}
|
|
143
|
+
if (isArray(initialList)) {
|
|
144
|
+
initialList.forEach((file) => {
|
|
145
|
+
const key = uuid();
|
|
146
|
+
state.files.set(key, { ...file, key });
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
state.list = Array.from(state.files.values());
|
|
151
|
+
return state;
|
|
152
|
+
},
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
const doUpload = useCallback(async (key: string, file: File) => {
|
|
156
|
+
const data = new FormData();
|
|
157
|
+
data.append("file", file);
|
|
158
|
+
const abort = new AbortController();
|
|
159
|
+
dispatch({
|
|
160
|
+
type: "start",
|
|
161
|
+
key,
|
|
162
|
+
file: {
|
|
163
|
+
key,
|
|
164
|
+
file,
|
|
165
|
+
progress: 0.001,
|
|
166
|
+
mime: file.type,
|
|
167
|
+
filename: file.name,
|
|
168
|
+
size: file.size,
|
|
169
|
+
abort: () => abort.abort(),
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
await uploader(data, {
|
|
173
|
+
signal: abort.signal,
|
|
174
|
+
onUploadProgress(progressEvent) {
|
|
175
|
+
dispatch({
|
|
176
|
+
type: "progress",
|
|
177
|
+
key,
|
|
178
|
+
progress: progressEvent.progress ?? 0,
|
|
179
|
+
});
|
|
180
|
+
},
|
|
181
|
+
})
|
|
182
|
+
.then((path) => {
|
|
183
|
+
dispatch({ type: "stop", key, path });
|
|
184
|
+
})
|
|
185
|
+
.catch((error) => {
|
|
186
|
+
if (error.code === "ERR_CANCELED" || error.name === "AbortError") {
|
|
187
|
+
return dispatch({ type: "abort", key });
|
|
188
|
+
}
|
|
189
|
+
dispatch({
|
|
190
|
+
type: "error",
|
|
191
|
+
key,
|
|
192
|
+
error: error?.message ?? "form:fileUploadFailed",
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
}, []);
|
|
196
|
+
|
|
197
|
+
const upload = useCallback(
|
|
198
|
+
(files: FileList | null) => {
|
|
199
|
+
if (!multiple) {
|
|
200
|
+
Array.from(state.files.values())[0]?.abort?.();
|
|
201
|
+
}
|
|
202
|
+
Array.from(files ?? [])
|
|
203
|
+
.slice(0, multiple ? undefined : 1)
|
|
204
|
+
.forEach((file) => {
|
|
205
|
+
void doUpload(uuid(), file);
|
|
206
|
+
});
|
|
207
|
+
},
|
|
208
|
+
[multiple],
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
const retry = useCallback(
|
|
212
|
+
(key: string) => {
|
|
213
|
+
const fileObject = state.files.get(key);
|
|
214
|
+
void (fileObject?.file && doUpload(key, fileObject.file));
|
|
215
|
+
},
|
|
216
|
+
[state],
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
const remove = useCallback(
|
|
220
|
+
(key: string) => {
|
|
221
|
+
dispatch({ type: "remove", key });
|
|
222
|
+
},
|
|
223
|
+
[state],
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
return {
|
|
227
|
+
upload,
|
|
228
|
+
remove,
|
|
229
|
+
retry,
|
|
230
|
+
pending: state.count,
|
|
231
|
+
list: state.list,
|
|
232
|
+
files: Array.from(state.files.values()),
|
|
233
|
+
};
|
|
234
|
+
};
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2024 Adarsh Pastakia
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
+
* subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
+
* portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { isEmpty, isNil, yup } from "@react-fabric/utilities";
|
|
25
|
+
import { Fragment, useMemo } from "react";
|
|
26
|
+
import { FormField } from "../builder/FormField";
|
|
27
|
+
import { type FormSchema } from "../types/schema";
|
|
28
|
+
import { type UploadHandler } from "./useFileUploader";
|
|
29
|
+
|
|
30
|
+
const makeYupField = (field: FormSchema) => {
|
|
31
|
+
let yupField: AnyObject = yup.string();
|
|
32
|
+
if (field.datatype === "schema") {
|
|
33
|
+
const child: yup.ObjectShape = Object.fromEntries(
|
|
34
|
+
field.schema.map((field) => [field.id, makeYupField(field)]),
|
|
35
|
+
);
|
|
36
|
+
yupField = yup.object(child);
|
|
37
|
+
}
|
|
38
|
+
if (field.datatype === "boolean") yupField = yup.boolean();
|
|
39
|
+
if (
|
|
40
|
+
field.datatype === "decimal" ||
|
|
41
|
+
field.datatype === "number" ||
|
|
42
|
+
field.datatype === "range"
|
|
43
|
+
) {
|
|
44
|
+
yupField = yup.number();
|
|
45
|
+
!isNil(field.min) && (yupField = yupField.min(field.min));
|
|
46
|
+
!isNil(field.max) && (yupField = yupField.max(field.max));
|
|
47
|
+
|
|
48
|
+
if (field.datatype === "range") yupField = yup.array(yupField);
|
|
49
|
+
}
|
|
50
|
+
if (field.datatype === "string") {
|
|
51
|
+
field.options && (yupField = yupField.oneOf(field.options));
|
|
52
|
+
field.regex && (yupField = yupField.matches(new RegExp(field.regex)));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (field.datatype === "file") {
|
|
56
|
+
yupField = yup
|
|
57
|
+
.object({
|
|
58
|
+
filename: yup.string().required(),
|
|
59
|
+
path: yup.string().required(),
|
|
60
|
+
})
|
|
61
|
+
.unknown(true);
|
|
62
|
+
if (field.required) {
|
|
63
|
+
yupField = yupField.test(
|
|
64
|
+
"is-empty",
|
|
65
|
+
{
|
|
66
|
+
key: "form:yup.empty",
|
|
67
|
+
values: { path: field.id, label: field.label },
|
|
68
|
+
},
|
|
69
|
+
(value: AnyObject) => !isEmpty(value),
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (field.required) {
|
|
75
|
+
yupField = yupField.required();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (field.multiple) {
|
|
79
|
+
yupField = yup.array(yupField);
|
|
80
|
+
field.required && (yupField = yupField.required().min(1));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return yupField.label(field.label);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export const useFormBuilder = (
|
|
87
|
+
schema: FormSchema[],
|
|
88
|
+
options: {
|
|
89
|
+
prefix?: string;
|
|
90
|
+
inline?: boolean;
|
|
91
|
+
fileUrl?: (path: string) => string;
|
|
92
|
+
uploadHandler?: UploadHandler;
|
|
93
|
+
} = {},
|
|
94
|
+
) => {
|
|
95
|
+
const filteredSchema = useMemo(
|
|
96
|
+
() => schema.filter?.((schm) => !!schm.id) ?? [],
|
|
97
|
+
[schema],
|
|
98
|
+
);
|
|
99
|
+
const schemaDef = yup.object(
|
|
100
|
+
Object.fromEntries(
|
|
101
|
+
filteredSchema.map((field) => [field.id, makeYupField(field)]),
|
|
102
|
+
),
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
const formDef = (
|
|
106
|
+
<Fragment>
|
|
107
|
+
{filteredSchema.map((field, idx) => (
|
|
108
|
+
<div className="mb-4" key={field.id}>
|
|
109
|
+
{field.datatype === "schema" ? null : (
|
|
110
|
+
<FormField
|
|
111
|
+
{...field}
|
|
112
|
+
autoFocus={idx === 0}
|
|
113
|
+
fileUrl={options.fileUrl}
|
|
114
|
+
uploadHandler={options.uploadHandler}
|
|
115
|
+
inline={options.inline}
|
|
116
|
+
prefix={options.prefix}
|
|
117
|
+
/>
|
|
118
|
+
)}
|
|
119
|
+
</div>
|
|
120
|
+
))}
|
|
121
|
+
</Fragment>
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
return {
|
|
125
|
+
formDef,
|
|
126
|
+
schemaDef,
|
|
127
|
+
};
|
|
128
|
+
};
|
package/src/i18n/ar.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"addArray": "اضافة عنصر",
|
|
3
|
+
"password": {
|
|
4
|
+
"show": "عرض كلمة المرور",
|
|
5
|
+
"hide": "اخفاء كلمة المرور"
|
|
6
|
+
},
|
|
7
|
+
"select": {
|
|
8
|
+
"emptyList": "لا توجد خيارات متاحة",
|
|
9
|
+
"notFound": "لم يتم العثور على خيارات ل ",
|
|
10
|
+
"createOption": "إنشاء خيار جديد ل "
|
|
11
|
+
},
|
|
12
|
+
"yup": {
|
|
13
|
+
"arrayMax": "{{label}} حدد ما يصل {{max}} غرض",
|
|
14
|
+
"arrayMin": "{{label}} اختر على الأقل {{min}} غرض",
|
|
15
|
+
"invalid": "{{label}} نوع بيانات غير صالح",
|
|
16
|
+
"invalidType": "{{label}} غير صالح",
|
|
17
|
+
"numberMax": "{{label}} الحد الأقصى {{max}}",
|
|
18
|
+
"numberMin": "{{label}} الحد الأدنى {{min}}",
|
|
19
|
+
"required": "{{label}} مطلوب",
|
|
20
|
+
"stringMax": "{{label}} أقصى {{max}} الحروف مسموحة",
|
|
21
|
+
"stringMin": "{{label}} يتطلب الحد الأدنى {{min}} الشخصيات"
|
|
22
|
+
}
|
|
23
|
+
}
|
package/src/i18n/en.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"addArray": "Add Item",
|
|
3
|
+
"password": {
|
|
4
|
+
"show": "Show password",
|
|
5
|
+
"hide": "Hide password"
|
|
6
|
+
},
|
|
7
|
+
"select": {
|
|
8
|
+
"emptyList": "No options available",
|
|
9
|
+
"notFound": "No options found for <0>{{query}}</0>",
|
|
10
|
+
"createOption": "Create new option for <0>{{query}}</0>"
|
|
11
|
+
},
|
|
12
|
+
"yup": {
|
|
13
|
+
"arrayMax": "{{label}} must contain upto {{max}} item",
|
|
14
|
+
"arrayMin": "{{label}} must contain atleast {{min}} item",
|
|
15
|
+
"invalid": "{{label}} invalid",
|
|
16
|
+
"empty": "{{label}} must not be empty",
|
|
17
|
+
"invalidType": "{{label}} invalid datatype",
|
|
18
|
+
"numberMax": "{{label}} maximum {{max}}",
|
|
19
|
+
"numberMin": "{{label}} minimum {{min}}",
|
|
20
|
+
"required": "{{label}} required",
|
|
21
|
+
"stringMax": "{{label}} maximum {{max}} characters allowed",
|
|
22
|
+
"stringMin": "{{label}} requires minimum {{min}} characters"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* React Fabric
|
|
3
|
+
* @version: 1.0.0
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
* The MIT License (MIT)
|
|
7
|
+
* Copyright (c) 2024 Adarsh Pastakia
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
+
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
+
* subject to the following conditions:
|
|
14
|
+
*
|
|
15
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
+
* portions of the Software.
|
|
17
|
+
*
|
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { addTranslationBundle } from "@react-fabric/core";
|
|
25
|
+
import ar from "./ar.json";
|
|
26
|
+
import en from "./en.json";
|
|
27
|
+
|
|
28
|
+
addTranslationBundle("form", { en, ar });
|