@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
package/src/index.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
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 "./i18n";
|
|
25
|
+
|
|
26
|
+
export { Controller } from "./form/Controller";
|
|
27
|
+
export { Form, type FormRef } from "./form/Form";
|
|
28
|
+
|
|
29
|
+
export { useFileUploader } from "./hooks/useFileUploader";
|
|
30
|
+
export { useFormBuilder } from "./hooks/useFormBuilder";
|
|
31
|
+
|
|
32
|
+
export { type FormSchema } from "./types/schema";
|
|
33
|
+
|
|
34
|
+
export { ArrayInput } from "./input/ArrayInput";
|
|
35
|
+
export { Checkbox } from "./input/Checkbox";
|
|
36
|
+
export { ColorInput } from "./input/ColorInput";
|
|
37
|
+
export { ColorPicker } from "./input/ColorPicker";
|
|
38
|
+
export { Field } from "./input/Field";
|
|
39
|
+
export { FileInput } from "./input/File";
|
|
40
|
+
export { HiddenInput } from "./input/Hidden";
|
|
41
|
+
export { Input } from "./input/Input";
|
|
42
|
+
export { Number } from "./input/Number";
|
|
43
|
+
export { Password } from "./input/Password";
|
|
44
|
+
export { Radio } from "./input/Radio";
|
|
45
|
+
export { RadioGroup } from "./input/RadioGroup";
|
|
46
|
+
export { Range } from "./input/Range";
|
|
47
|
+
export { Search } from "./input/Search";
|
|
48
|
+
export { Slider } from "./input/Slider";
|
|
49
|
+
export { Switch } from "./input/Switch";
|
|
50
|
+
export { Textarea } from "./input/Textarea";
|
|
51
|
+
export { AutoComplete } from "./select/AutoComplete";
|
|
52
|
+
export { DualList } from "./select/DualList";
|
|
53
|
+
export { List } from "./select/List";
|
|
54
|
+
export { Select } from "./select/Select";
|
|
@@ -0,0 +1,41 @@
|
|
|
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
|
+
@layer var {
|
|
25
|
+
.arrayInput {
|
|
26
|
+
& [data-ref="fieldWrapper"] {
|
|
27
|
+
flex: 1 1 0%;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
& .addButton :global(.buttonInner) {
|
|
31
|
+
color: theme(colors.tint.600);
|
|
32
|
+
outline: 1px dashed theme(colors.tint.600);
|
|
33
|
+
|
|
34
|
+
&[data-invalid="true"] {
|
|
35
|
+
color: theme(colors.danger.600);
|
|
36
|
+
background-color: theme(colors.danger.100);
|
|
37
|
+
outline: 1px dashed theme(colors.danger.600);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,318 @@
|
|
|
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 { DndContext, type DragEndEvent } from "@dnd-kit/core";
|
|
25
|
+
import { SortableContext, useSortable } from "@dnd-kit/sortable";
|
|
26
|
+
import { Button, CoreIcons, Icon, Tooltip } from "@react-fabric/core";
|
|
27
|
+
import { isString } from "@react-fabric/utilities";
|
|
28
|
+
import classNames from "classnames";
|
|
29
|
+
import {
|
|
30
|
+
Children,
|
|
31
|
+
cloneElement,
|
|
32
|
+
useCallback,
|
|
33
|
+
useImperativeHandle,
|
|
34
|
+
useMemo,
|
|
35
|
+
type PropsWithChildren,
|
|
36
|
+
type ReactNode,
|
|
37
|
+
type RefObject,
|
|
38
|
+
} from "react";
|
|
39
|
+
import { useFieldArray, useFormContext } from "react-hook-form";
|
|
40
|
+
import { useTranslation } from "react-i18next";
|
|
41
|
+
import { FieldWrapper } from "../internal/FieldWrapper";
|
|
42
|
+
import classes from "./ArrayInput.module.css";
|
|
43
|
+
|
|
44
|
+
import { restrictToVerticalAxis } from "@dnd-kit/modifiers";
|
|
45
|
+
import { CSS } from "@dnd-kit/utilities";
|
|
46
|
+
export interface ArrayInputProps {
|
|
47
|
+
/**
|
|
48
|
+
* input label
|
|
49
|
+
*/
|
|
50
|
+
label?: string;
|
|
51
|
+
/**
|
|
52
|
+
* append to label end
|
|
53
|
+
*/
|
|
54
|
+
appendLabel?: JSX.Element | string | number | boolean;
|
|
55
|
+
/**
|
|
56
|
+
* info label at bottom
|
|
57
|
+
*/
|
|
58
|
+
info?: string;
|
|
59
|
+
/**
|
|
60
|
+
* required
|
|
61
|
+
*/
|
|
62
|
+
required?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* disabled input
|
|
65
|
+
*/
|
|
66
|
+
disabled?: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* read-only input
|
|
69
|
+
*/
|
|
70
|
+
readOnly?: boolean;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* field name
|
|
74
|
+
*/
|
|
75
|
+
name: string;
|
|
76
|
+
/**
|
|
77
|
+
* minimum required length
|
|
78
|
+
*/
|
|
79
|
+
minItems?: number;
|
|
80
|
+
/**
|
|
81
|
+
* field name to focus on add
|
|
82
|
+
*/
|
|
83
|
+
focusName?: string;
|
|
84
|
+
/**
|
|
85
|
+
* enable drag-n-drop sorting
|
|
86
|
+
*/
|
|
87
|
+
enableSorting?: boolean;
|
|
88
|
+
children:
|
|
89
|
+
| React.ReactNode
|
|
90
|
+
| ((props: { index: number; name: string }) => ReactNode);
|
|
91
|
+
/**
|
|
92
|
+
* add item button label
|
|
93
|
+
*/
|
|
94
|
+
addLabel?: string;
|
|
95
|
+
/**
|
|
96
|
+
* fixed length list
|
|
97
|
+
*/
|
|
98
|
+
fixedList?: boolean;
|
|
99
|
+
arrayRef?: RefObject<{
|
|
100
|
+
addItem: (item: AnyObject) => void;
|
|
101
|
+
removeItem: (idx: number) => void;
|
|
102
|
+
}>;
|
|
103
|
+
/**
|
|
104
|
+
* Add item
|
|
105
|
+
*/
|
|
106
|
+
onAdd?: () => AnyObject;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export const ArrayInput = ({
|
|
110
|
+
name,
|
|
111
|
+
children,
|
|
112
|
+
addLabel,
|
|
113
|
+
fixedList,
|
|
114
|
+
focusName = "",
|
|
115
|
+
onAdd,
|
|
116
|
+
arrayRef,
|
|
117
|
+
enableSorting,
|
|
118
|
+
disabled = false,
|
|
119
|
+
readOnly = false,
|
|
120
|
+
minItems = 0,
|
|
121
|
+
...rest
|
|
122
|
+
}: ArrayInputProps) => {
|
|
123
|
+
const { t } = useTranslation("form");
|
|
124
|
+
const form = useFormContext();
|
|
125
|
+
|
|
126
|
+
if (!form?.control)
|
|
127
|
+
throw new Error("ArrayInput must be contained within a Form element");
|
|
128
|
+
|
|
129
|
+
const { fields, append, remove, move } = useFieldArray({
|
|
130
|
+
name,
|
|
131
|
+
control: form.control,
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
const error = useMemo<AnyObject>(
|
|
135
|
+
() =>
|
|
136
|
+
form.formState.errors[name]?.message ??
|
|
137
|
+
form.formState.errors[name]?.root?.message,
|
|
138
|
+
[form.formState.errors],
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
const handleAdd = useCallback(
|
|
142
|
+
(item: AnyObject) => {
|
|
143
|
+
append(item);
|
|
144
|
+
setTimeout(() => {
|
|
145
|
+
form.setFocus(
|
|
146
|
+
`${name}.${fields.length}${focusName ? "." + focusName : ""}`,
|
|
147
|
+
);
|
|
148
|
+
}, 100);
|
|
149
|
+
},
|
|
150
|
+
[name, fields, focusName],
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
useImperativeHandle(arrayRef, () => ({
|
|
154
|
+
addItem: handleAdd,
|
|
155
|
+
removeItem: remove,
|
|
156
|
+
}));
|
|
157
|
+
|
|
158
|
+
const Wrapper = useCallback(
|
|
159
|
+
({ children }: PropsWithChildren) => {
|
|
160
|
+
if (enableSorting) {
|
|
161
|
+
// pass id list to dnd context
|
|
162
|
+
const idMap = fields.map((item) => item.id);
|
|
163
|
+
const handleDragEnd = (e: DragEndEvent) => {
|
|
164
|
+
if (e.over && e.active.id !== e.over.id) {
|
|
165
|
+
// find index of item and drop over
|
|
166
|
+
const active = idMap.indexOf(`${e.active.id}`);
|
|
167
|
+
const end = idMap.indexOf(`${e.over.id}`);
|
|
168
|
+
move(active, end);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
return (
|
|
172
|
+
<DndContext
|
|
173
|
+
onDragEnd={handleDragEnd}
|
|
174
|
+
modifiers={[restrictToVerticalAxis]}
|
|
175
|
+
>
|
|
176
|
+
<SortableContext items={idMap}>{children}</SortableContext>
|
|
177
|
+
</DndContext>
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return children;
|
|
182
|
+
},
|
|
183
|
+
[enableSorting, fields],
|
|
184
|
+
);
|
|
185
|
+
// @ts-expect-error ignore
|
|
186
|
+
Wrapper.displayName = "SortableWrapper";
|
|
187
|
+
|
|
188
|
+
const SortableItem = useCallback(
|
|
189
|
+
({
|
|
190
|
+
id,
|
|
191
|
+
children,
|
|
192
|
+
className,
|
|
193
|
+
}: PropsWithChildren<{ id: AnyObject; className: string }>) => {
|
|
194
|
+
if (enableSorting) {
|
|
195
|
+
const {
|
|
196
|
+
attributes,
|
|
197
|
+
listeners,
|
|
198
|
+
setNodeRef,
|
|
199
|
+
transform,
|
|
200
|
+
transition,
|
|
201
|
+
isDragging,
|
|
202
|
+
} = useSortable({ id });
|
|
203
|
+
return (
|
|
204
|
+
<div
|
|
205
|
+
className={className}
|
|
206
|
+
ref={setNodeRef}
|
|
207
|
+
style={{
|
|
208
|
+
transform: CSS.Transform.toString(transform),
|
|
209
|
+
transition,
|
|
210
|
+
}}
|
|
211
|
+
>
|
|
212
|
+
<Icon
|
|
213
|
+
className={isDragging ? "cursor-grabbing" : "cursor-grab"}
|
|
214
|
+
icon={CoreIcons.dragHandle}
|
|
215
|
+
{...attributes}
|
|
216
|
+
{...listeners}
|
|
217
|
+
/>
|
|
218
|
+
{children}
|
|
219
|
+
</div>
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return <div className={className}>{children}</div>;
|
|
224
|
+
},
|
|
225
|
+
[enableSorting],
|
|
226
|
+
);
|
|
227
|
+
// @ts-expect-error ignore
|
|
228
|
+
SortableItem.displayName = "SortableItem";
|
|
229
|
+
|
|
230
|
+
return (
|
|
231
|
+
<div className={classes.arrayInput}>
|
|
232
|
+
<FieldWrapper {...rest}>
|
|
233
|
+
<Wrapper>
|
|
234
|
+
{fields.map((item, index) => (
|
|
235
|
+
<SortableItem
|
|
236
|
+
key={item.id}
|
|
237
|
+
id={item.id}
|
|
238
|
+
className="flex items-center flex-nowrap gap-2 mb-1"
|
|
239
|
+
>
|
|
240
|
+
{typeof children === "function"
|
|
241
|
+
? children({
|
|
242
|
+
index,
|
|
243
|
+
name: `${name}.${index}`,
|
|
244
|
+
})
|
|
245
|
+
: Children.map(children, (child: AnyObject) =>
|
|
246
|
+
cloneElement(
|
|
247
|
+
child,
|
|
248
|
+
Object.assign(
|
|
249
|
+
{
|
|
250
|
+
disabled,
|
|
251
|
+
readOnly,
|
|
252
|
+
"data-inner": true,
|
|
253
|
+
name: `${name}.${index}${
|
|
254
|
+
child.props.name
|
|
255
|
+
? "." + (child.props.name as string)
|
|
256
|
+
: ""
|
|
257
|
+
}`,
|
|
258
|
+
},
|
|
259
|
+
index > 0 && {
|
|
260
|
+
label: undefined,
|
|
261
|
+
required: undefined,
|
|
262
|
+
appendLabel: undefined,
|
|
263
|
+
},
|
|
264
|
+
),
|
|
265
|
+
),
|
|
266
|
+
)}
|
|
267
|
+
{!fixedList && (
|
|
268
|
+
<Button
|
|
269
|
+
aria-label="Remove item"
|
|
270
|
+
icon={CoreIcons.remove}
|
|
271
|
+
color="danger"
|
|
272
|
+
variant="link"
|
|
273
|
+
className="self-end"
|
|
274
|
+
disabled={disabled || readOnly || fields.length <= minItems}
|
|
275
|
+
onClick={() => remove(index)}
|
|
276
|
+
/>
|
|
277
|
+
)}
|
|
278
|
+
</SortableItem>
|
|
279
|
+
))}
|
|
280
|
+
</Wrapper>
|
|
281
|
+
</FieldWrapper>
|
|
282
|
+
{!fixedList && onAdd && (
|
|
283
|
+
<div className="flex justify-end">
|
|
284
|
+
<Tooltip
|
|
285
|
+
color="danger"
|
|
286
|
+
content={
|
|
287
|
+
isString(error)
|
|
288
|
+
? `${error}`
|
|
289
|
+
: (t(
|
|
290
|
+
error?.key,
|
|
291
|
+
error?.values
|
|
292
|
+
? {
|
|
293
|
+
...error?.values,
|
|
294
|
+
label: t(
|
|
295
|
+
error.values?.label ?? "form:badkey",
|
|
296
|
+
error.values?.path,
|
|
297
|
+
),
|
|
298
|
+
}
|
|
299
|
+
: {},
|
|
300
|
+
) as string)
|
|
301
|
+
}
|
|
302
|
+
>
|
|
303
|
+
<Button
|
|
304
|
+
aria-label="Add item"
|
|
305
|
+
icon={CoreIcons.insert}
|
|
306
|
+
onClick={() => handleAdd(onAdd())}
|
|
307
|
+
data-invalid={!!error}
|
|
308
|
+
disabled={!!disabled || readOnly}
|
|
309
|
+
className={classNames(classes.addButton, "me-10")}
|
|
310
|
+
>
|
|
311
|
+
{addLabel ?? t("addArray")}
|
|
312
|
+
</Button>
|
|
313
|
+
</Tooltip>
|
|
314
|
+
</div>
|
|
315
|
+
)}
|
|
316
|
+
</div>
|
|
317
|
+
);
|
|
318
|
+
};
|
|
@@ -0,0 +1,150 @@
|
|
|
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 { Avatar, Icon, ProgressBar } from "@react-fabric/core";
|
|
25
|
+
import { type RefProp } from "@react-fabric/core/dist/types/types";
|
|
26
|
+
import { useEffect, useState } from "react";
|
|
27
|
+
import { useFileUploader, type UploadHandler } from "../hooks/useFileUploader";
|
|
28
|
+
import { HiddenInput } from "./Hidden";
|
|
29
|
+
|
|
30
|
+
export interface Props extends RefProp {
|
|
31
|
+
/**
|
|
32
|
+
* field name
|
|
33
|
+
*/
|
|
34
|
+
name?: string;
|
|
35
|
+
/**
|
|
36
|
+
* actual avatar storage path
|
|
37
|
+
*/
|
|
38
|
+
value?: string;
|
|
39
|
+
/**
|
|
40
|
+
* avatar stream src
|
|
41
|
+
*/
|
|
42
|
+
src?: string;
|
|
43
|
+
size?: string | number;
|
|
44
|
+
/**
|
|
45
|
+
* upload handler to store file in temp storage
|
|
46
|
+
*/
|
|
47
|
+
uploadHandler: UploadHandler;
|
|
48
|
+
fileUrl?: (path: string) => string;
|
|
49
|
+
/**
|
|
50
|
+
* change handler
|
|
51
|
+
*
|
|
52
|
+
* @param file
|
|
53
|
+
* @param base64
|
|
54
|
+
* @returns storage path
|
|
55
|
+
*/
|
|
56
|
+
onChange?: (path: string) => void;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const AvatarInput = ({
|
|
60
|
+
src,
|
|
61
|
+
size = "6rem",
|
|
62
|
+
fileUrl,
|
|
63
|
+
uploadHandler,
|
|
64
|
+
...rest
|
|
65
|
+
}: Props) => {
|
|
66
|
+
const { files, list, upload, remove } = useFileUploader(
|
|
67
|
+
async (data, config) => await uploadHandler?.(data, config),
|
|
68
|
+
rest.value,
|
|
69
|
+
false,
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const [base64, setBase64] = useState("");
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
if (list?.length) {
|
|
75
|
+
const file = files?.[0]?.file;
|
|
76
|
+
void (
|
|
77
|
+
file &&
|
|
78
|
+
new Response(file).blob().then((blob) => {
|
|
79
|
+
const reader = new FileReader();
|
|
80
|
+
reader.onload = () => {
|
|
81
|
+
setBase64(reader.result as string);
|
|
82
|
+
};
|
|
83
|
+
reader.onerror = (e) => {
|
|
84
|
+
console.error("ERROR", e);
|
|
85
|
+
};
|
|
86
|
+
reader.readAsDataURL(blob);
|
|
87
|
+
})
|
|
88
|
+
);
|
|
89
|
+
} else {
|
|
90
|
+
setBase64("");
|
|
91
|
+
}
|
|
92
|
+
}, [files, list]);
|
|
93
|
+
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
src && setBase64(fileUrl?.(src) ?? src);
|
|
96
|
+
}, [src]);
|
|
97
|
+
|
|
98
|
+
return (
|
|
99
|
+
<div className="flex flex-nowrap items-end">
|
|
100
|
+
<div className="inline-block leading-none rounded-full relative outline overflow-hidden">
|
|
101
|
+
<Avatar name="temp" size={size} avatar={base64} />
|
|
102
|
+
<div className="absolute inset-x-0 bottom-0 cursor-pointer bg-opacity-20 hover:bg-opacity-50 bg-black text-white text-xs text-center py-1">
|
|
103
|
+
<span>Edit</span>
|
|
104
|
+
<input
|
|
105
|
+
type="file"
|
|
106
|
+
accept="image/*"
|
|
107
|
+
className="absolute inset-0 opacity-0 z-5 cursor-pointer"
|
|
108
|
+
onChange={(e) => [upload(e.target.files), (e.target.value = "")]}
|
|
109
|
+
/>
|
|
110
|
+
</div>
|
|
111
|
+
</div>
|
|
112
|
+
<HiddenInput hiddenValue={list?.[0]?.path} {...rest} />
|
|
113
|
+
{files.map((file, idx) => (
|
|
114
|
+
<div key={idx} className="px-2 py-1 flex-1">
|
|
115
|
+
<div className="flex flex-nowrap gap-1 items-center">
|
|
116
|
+
<div className="flex-initial truncate pe-2">{file.filename}</div>
|
|
117
|
+
{file.error && (
|
|
118
|
+
<div className="flex-1 truncate px-2">
|
|
119
|
+
<span className="text-danger-600">{file.error}</span>
|
|
120
|
+
</div>
|
|
121
|
+
)}
|
|
122
|
+
{file.progress && (
|
|
123
|
+
<Icon
|
|
124
|
+
icon="mdi mdi-stop-circle"
|
|
125
|
+
bg="danger"
|
|
126
|
+
color="white"
|
|
127
|
+
size="sm"
|
|
128
|
+
className="p-[2px]"
|
|
129
|
+
aria-label="abort"
|
|
130
|
+
onClick={() => file.abort?.()}
|
|
131
|
+
/>
|
|
132
|
+
)}
|
|
133
|
+
{!file.progress && (
|
|
134
|
+
<Icon
|
|
135
|
+
icon="mdi mdi-trash-can"
|
|
136
|
+
color="danger"
|
|
137
|
+
size="sm"
|
|
138
|
+
aria-label="remove"
|
|
139
|
+
onClick={() => remove(file.key)}
|
|
140
|
+
/>
|
|
141
|
+
)}
|
|
142
|
+
</div>
|
|
143
|
+
{file.progress && (
|
|
144
|
+
<ProgressBar animate value={file.progress * 100} size="xxs" />
|
|
145
|
+
)}
|
|
146
|
+
</div>
|
|
147
|
+
))}
|
|
148
|
+
</div>
|
|
149
|
+
);
|
|
150
|
+
};
|