@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.
Files changed (187) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +10 -0
  3. package/dist/cjs/builder/FileField.js +131 -0
  4. package/dist/cjs/builder/FormField.js +119 -0
  5. package/dist/cjs/form/Controller.js +64 -0
  6. package/dist/cjs/form/Form.js +128 -0
  7. package/dist/cjs/hooks/useFileUploader.js +204 -0
  8. package/dist/cjs/hooks/useFormBuilder.js +87 -0
  9. package/dist/cjs/i18n/ar.json.js +37 -0
  10. package/dist/cjs/i18n/en.json.js +38 -0
  11. package/dist/cjs/i18n/index.js +33 -0
  12. package/dist/cjs/index.js +56 -0
  13. package/dist/cjs/input/ArrayInput.js +189 -0
  14. package/dist/cjs/input/ArrayInput.module.css +1 -0
  15. package/dist/cjs/input/AvatarInput.js +141 -0
  16. package/dist/cjs/input/Checkbox.js +83 -0
  17. package/dist/cjs/input/ColorInput.js +100 -0
  18. package/dist/cjs/input/ColorPicker.js +122 -0
  19. package/dist/cjs/input/ColorPicker.module.css +1 -0
  20. package/dist/cjs/input/Field.js +31 -0
  21. package/dist/cjs/input/File.js +165 -0
  22. package/dist/cjs/input/File.module.css +1 -0
  23. package/dist/cjs/input/Hidden.js +54 -0
  24. package/dist/cjs/input/Input.js +79 -0
  25. package/dist/cjs/input/Number.js +80 -0
  26. package/dist/cjs/input/Password.js +104 -0
  27. package/dist/cjs/input/Radio.js +70 -0
  28. package/dist/cjs/input/RadioGroup.js +31 -0
  29. package/dist/cjs/input/Range.js +169 -0
  30. package/dist/cjs/input/Search.js +109 -0
  31. package/dist/cjs/input/Slider.js +172 -0
  32. package/dist/cjs/input/Switch.js +79 -0
  33. package/dist/cjs/input/Textarea.js +87 -0
  34. package/dist/cjs/internal/ErrorIcon.js +32 -0
  35. package/dist/cjs/internal/Field.module.css +1 -0
  36. package/dist/cjs/internal/FieldWrapper.js +78 -0
  37. package/dist/cjs/internal/InputWrapper.js +113 -0
  38. package/dist/cjs/ranger/index.js +352 -0
  39. package/dist/cjs/ranger/utils.js +49 -0
  40. package/dist/cjs/select/AutoComplete.js +268 -0
  41. package/dist/cjs/select/DualList.js +238 -0
  42. package/dist/cjs/select/List.js +241 -0
  43. package/dist/cjs/select/Options.js +76 -0
  44. package/dist/cjs/select/Select.js +283 -0
  45. package/dist/cjs/select/helpers.js +41 -0
  46. package/dist/cjs/select/useSelect.js +337 -0
  47. package/dist/cjs/utils/eventHandlers.js +50 -0
  48. package/dist/cjs/utils/getColor.js +40 -0
  49. package/dist/esm/builder/FileField.js +131 -0
  50. package/dist/esm/builder/FormField.js +119 -0
  51. package/dist/esm/form/Controller.js +64 -0
  52. package/dist/esm/form/Form.js +128 -0
  53. package/dist/esm/hooks/useFileUploader.js +204 -0
  54. package/dist/esm/hooks/useFormBuilder.js +87 -0
  55. package/dist/esm/i18n/ar.json.js +31 -0
  56. package/dist/esm/i18n/en.json.js +32 -0
  57. package/dist/esm/i18n/index.js +33 -0
  58. package/dist/esm/index.js +28 -0
  59. package/dist/esm/input/ArrayInput.js +189 -0
  60. package/dist/esm/input/ArrayInput.module.css +1 -0
  61. package/dist/esm/input/AvatarInput.js +141 -0
  62. package/dist/esm/input/Checkbox.js +83 -0
  63. package/dist/esm/input/ColorInput.js +100 -0
  64. package/dist/esm/input/ColorPicker.js +122 -0
  65. package/dist/esm/input/ColorPicker.module.css +1 -0
  66. package/dist/esm/input/Field.js +31 -0
  67. package/dist/esm/input/File.js +165 -0
  68. package/dist/esm/input/File.module.css +1 -0
  69. package/dist/esm/input/Hidden.js +54 -0
  70. package/dist/esm/input/Input.js +79 -0
  71. package/dist/esm/input/Number.js +80 -0
  72. package/dist/esm/input/Password.js +104 -0
  73. package/dist/esm/input/Radio.js +70 -0
  74. package/dist/esm/input/RadioGroup.js +31 -0
  75. package/dist/esm/input/Range.js +169 -0
  76. package/dist/esm/input/Search.js +109 -0
  77. package/dist/esm/input/Slider.js +172 -0
  78. package/dist/esm/input/Switch.js +79 -0
  79. package/dist/esm/input/Textarea.js +87 -0
  80. package/dist/esm/internal/ErrorIcon.js +32 -0
  81. package/dist/esm/internal/Field.module.css +1 -0
  82. package/dist/esm/internal/FieldWrapper.js +78 -0
  83. package/dist/esm/internal/InputWrapper.js +113 -0
  84. package/dist/esm/ranger/index.js +352 -0
  85. package/dist/esm/ranger/utils.js +47 -0
  86. package/dist/esm/select/AutoComplete.js +268 -0
  87. package/dist/esm/select/DualList.js +238 -0
  88. package/dist/esm/select/List.js +241 -0
  89. package/dist/esm/select/Options.js +76 -0
  90. package/dist/esm/select/Select.js +283 -0
  91. package/dist/esm/select/helpers.js +40 -0
  92. package/dist/esm/select/useSelect.js +337 -0
  93. package/dist/esm/utils/eventHandlers.js +50 -0
  94. package/dist/esm/utils/getColor.js +39 -0
  95. package/dist/types/builder/FileField.d.ts +8 -0
  96. package/dist/types/builder/FormField.d.ts +9 -0
  97. package/dist/types/form/Controller.d.ts +20 -0
  98. package/dist/types/form/Form.d.ts +33 -0
  99. package/dist/types/hooks/useFileUploader.d.ts +23 -0
  100. package/dist/types/hooks/useFormBuilder.d.ts +16 -0
  101. package/dist/types/i18n/index.d.ts +1 -0
  102. package/dist/types/index.d.ts +27 -0
  103. package/dist/types/input/ArrayInput.d.ts +64 -0
  104. package/dist/types/input/AvatarInput.d.ts +31 -0
  105. package/dist/types/input/Checkbox.d.ts +46 -0
  106. package/dist/types/input/ColorInput.d.ts +10 -0
  107. package/dist/types/input/ColorPicker.d.ts +24 -0
  108. package/dist/types/input/Field.d.ts +41 -0
  109. package/dist/types/input/File.d.ts +17 -0
  110. package/dist/types/input/Hidden.d.ts +16 -0
  111. package/dist/types/input/Input.d.ts +9 -0
  112. package/dist/types/input/Number.d.ts +17 -0
  113. package/dist/types/input/Password.d.ts +13 -0
  114. package/dist/types/input/Radio.d.ts +42 -0
  115. package/dist/types/input/RadioGroup.d.ts +30 -0
  116. package/dist/types/input/Range.d.ts +59 -0
  117. package/dist/types/input/Search.d.ts +17 -0
  118. package/dist/types/input/Slider.d.ts +63 -0
  119. package/dist/types/input/Switch.d.ts +42 -0
  120. package/dist/types/input/Textarea.d.ts +17 -0
  121. package/dist/types/internal/ErrorIcon.d.ts +1 -0
  122. package/dist/types/internal/FieldWrapper.d.ts +1 -0
  123. package/dist/types/internal/InputWrapper.d.ts +1 -0
  124. package/dist/types/ranger/index.d.ts +70 -0
  125. package/dist/types/ranger/utils.d.ts +25 -0
  126. package/dist/types/select/AutoComplete.d.ts +17 -0
  127. package/dist/types/select/DualList.d.ts +38 -0
  128. package/dist/types/select/List.d.ts +4 -0
  129. package/dist/types/select/Options.d.ts +14 -0
  130. package/dist/types/select/Select.d.ts +2 -0
  131. package/dist/types/select/helpers.d.ts +2 -0
  132. package/dist/types/select/useSelect.d.ts +50 -0
  133. package/dist/types/types/index.d.ts +169 -0
  134. package/dist/types/types/schema.d.ts +59 -0
  135. package/dist/types/utils/eventHandlers.d.ts +11 -0
  136. package/dist/types/utils/getColor.d.ts +4 -0
  137. package/dist/types/utils/index.d.ts +2 -0
  138. package/package.json +64 -0
  139. package/src/builder/FileField.tsx +122 -0
  140. package/src/builder/FormField.tsx +161 -0
  141. package/src/form/Controller.tsx +103 -0
  142. package/src/form/Form.tsx +160 -0
  143. package/src/hooks/useFileUploader.ts +234 -0
  144. package/src/hooks/useFormBuilder.tsx +128 -0
  145. package/src/i18n/ar.json +23 -0
  146. package/src/i18n/en.json +24 -0
  147. package/src/i18n/index.ts +28 -0
  148. package/src/index.ts +54 -0
  149. package/src/input/ArrayInput.module.css +41 -0
  150. package/src/input/ArrayInput.tsx +318 -0
  151. package/src/input/AvatarInput.tsx +150 -0
  152. package/src/input/Checkbox.tsx +165 -0
  153. package/src/input/ColorInput.tsx +157 -0
  154. package/src/input/ColorPicker.module.css +51 -0
  155. package/src/input/ColorPicker.tsx +210 -0
  156. package/src/input/Field.tsx +96 -0
  157. package/src/input/File.module.css +42 -0
  158. package/src/input/File.tsx +224 -0
  159. package/src/input/Hidden.tsx +65 -0
  160. package/src/input/Input.tsx +114 -0
  161. package/src/input/Number.tsx +126 -0
  162. package/src/input/Password.tsx +150 -0
  163. package/src/input/Radio.tsx +146 -0
  164. package/src/input/RadioGroup.tsx +75 -0
  165. package/src/input/Range.tsx +276 -0
  166. package/src/input/Search.tsx +160 -0
  167. package/src/input/Slider.tsx +282 -0
  168. package/src/input/Switch.tsx +168 -0
  169. package/src/input/Textarea.tsx +127 -0
  170. package/src/internal/ErrorIcon.tsx +61 -0
  171. package/src/internal/Field.module.css +139 -0
  172. package/src/internal/FieldWrapper.tsx +84 -0
  173. package/src/internal/InputWrapper.tsx +187 -0
  174. package/src/ranger/index.tsx +514 -0
  175. package/src/ranger/utils.ts +48 -0
  176. package/src/select/AutoComplete.tsx +354 -0
  177. package/src/select/DualList.tsx +286 -0
  178. package/src/select/List.tsx +288 -0
  179. package/src/select/Options.tsx +121 -0
  180. package/src/select/Select.tsx +350 -0
  181. package/src/select/helpers.ts +36 -0
  182. package/src/select/useSelect.tsx +413 -0
  183. package/src/types/index.ts +203 -0
  184. package/src/types/schema.ts +100 -0
  185. package/src/utils/eventHandlers.ts +72 -0
  186. package/src/utils/getColor.ts +47 -0
  187. package/src/utils/index.ts +25 -0
@@ -0,0 +1,337 @@
1
+ 'use client';
2
+
3
+ import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
4
+ import _defineProperty from '@babel/runtime/helpers/defineProperty';
5
+ import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
6
+ import { useDebounce } from '@react-fabric/core';
7
+ import { matchString, dedupe, groupBy, isArray, EMPTY_ARRAY } from '@react-fabric/utilities';
8
+ import { useMemo, useDeferredValue, useRef, useCallback, useReducer, useEffect } from 'react';
9
+ import { getSelectedValue, makeOption } from './helpers.js';
10
+
11
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
12
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
13
+ var useSelect = function useSelect(_ref) {
14
+ var _ref$options = _ref.options,
15
+ options = _ref$options === void 0 ? EMPTY_ARRAY : _ref$options,
16
+ value = _ref.value,
17
+ autoComplete = _ref.autoComplete,
18
+ _ref$multiple = _ref.multiple,
19
+ multiple = _ref$multiple === void 0 ? false : _ref$multiple,
20
+ _ref$allowCreate = _ref.allowCreate,
21
+ allowCreate = _ref$allowCreate === void 0 ? false : _ref$allowCreate,
22
+ _ref$groupProperty = _ref.groupProperty,
23
+ groupProperty = _ref$groupProperty === void 0 ? "" : _ref$groupProperty,
24
+ _ref$valueProperty = _ref.valueProperty,
25
+ valueProperty = _ref$valueProperty === void 0 ? "id" : _ref$valueProperty,
26
+ _ref$labelProperty = _ref.labelProperty,
27
+ labelProperty = _ref$labelProperty === void 0 ? "label" : _ref$labelProperty,
28
+ _ref$createOption = _ref.createOption,
29
+ createOption = _ref$createOption === void 0 ? makeOption : _ref$createOption,
30
+ matcher = _ref.matcher,
31
+ onQuery = _ref.onQuery,
32
+ onChange = _ref.onChange,
33
+ onSelect = _ref.onSelect;
34
+ // value state
35
+ var emptyValue = useMemo(function () {
36
+ return multiple ? [] : "";
37
+ }, [multiple]);
38
+ var deferred = useDeferredValue(value !== null && value !== void 0 ? value : emptyValue);
39
+
40
+ // list items
41
+ var listRef = useRef([]);
42
+ var focusableRef = useRef(null);
43
+ var listContentRef = useRef([]);
44
+ var optionsRef = useRef([]);
45
+ var macthOption = useCallback(function (item, query) {
46
+ var _matcher, _item$labelProperty;
47
+ return (_matcher = matcher === null || matcher === void 0 ? void 0 : matcher(item, query)) !== null && _matcher !== void 0 ? _matcher : matchString((_item$labelProperty = item[labelProperty]) !== null && _item$labelProperty !== void 0 ? _item$labelProperty : item, query);
48
+ }, [matcher]);
49
+ var fireChange = useCallback(function (value) {
50
+ setTimeout(function () {
51
+ onSelect === null || onSelect === void 0 || onSelect(value !== null && value !== void 0 ? value : emptyValue);
52
+ onChange === null || onChange === void 0 || onChange(getSelectedValue(value, emptyValue, valueProperty));
53
+ }, 50);
54
+ }, [emptyValue, onSelect, onChange]);
55
+ var _useReducer = useReducer(function (state, action) {
56
+ if (action.type === "open") {
57
+ var index = state.items.indexOf(state.multiple ? state.value[0] : state.value);
58
+ return _objectSpread(_objectSpread({}, state), {}, {
59
+ open: true,
60
+ activeIndex: index > -1 ? index : null
61
+ });
62
+ }
63
+ if (action.type === "close") {
64
+ return _objectSpread(_objectSpread({}, state), {}, {
65
+ activeIndex: null,
66
+ open: false
67
+ });
68
+ }
69
+ if (action.type === "active" && state.open) {
70
+ if (autoComplete && action.index !== null) state.query = state.items[action.index];
71
+ return _objectSpread(_objectSpread({}, state), {}, {
72
+ activeIndex: action.index
73
+ });
74
+ }
75
+ if (action.type === "setValue") {
76
+ return _objectSpread(_objectSpread({}, state), {}, {
77
+ value: action.value,
78
+ selectedIndex: state.items.indexOf(state.multiple ? action.value[0] : action.value)
79
+ });
80
+ }
81
+ if (action.type === "changeValue") {
82
+ var _action$value;
83
+ var newState = _objectSpread(_objectSpread({}, state), {}, {
84
+ value: state.emptyValue,
85
+ activeIndex: null,
86
+ selectedIndex: autoComplete ? state.multiple ? (_action$value = action.value) === null || _action$value === void 0 ? void 0 : _action$value.length : 0 : state.items.indexOf(action.value)
87
+ });
88
+ if (action.value !== undefined) {
89
+ if (multiple) {
90
+ var values = [];
91
+ if (action.value && autoComplete) values.push.apply(values, _toConsumableArray(action.value.split(/,\s?/)));else if (action.value) values.push(action.value);
92
+ newState.value = _toConsumableArray(state.value);
93
+ values.forEach(function (value) {
94
+ var _newState$value$inclu, _newState$value;
95
+ newState.value = (_newState$value$inclu = (_newState$value = newState.value).includes) !== null && _newState$value$inclu !== void 0 && _newState$value$inclu.call(_newState$value, value) ? newState.value.filter(function (item) {
96
+ return item !== value;
97
+ }) : [].concat(_toConsumableArray(newState.value), [value]);
98
+ });
99
+ } else {
100
+ newState.value = action.value;
101
+ }
102
+ }
103
+ if (!state.multiple) newState.open = false;
104
+ fireChange(newState.value);
105
+ return newState;
106
+ }
107
+ if (action.type === "removeItem") {
108
+ var newValue = state.value;
109
+ if (state.multiple) {
110
+ var _action$index;
111
+ newValue = _toConsumableArray(newValue);
112
+ newValue.splice((_action$index = action.index) !== null && _action$index !== void 0 ? _action$index : -1, 1);
113
+ } else {
114
+ newValue = "";
115
+ }
116
+ fireChange(newValue);
117
+ return _objectSpread(_objectSpread({}, state), {}, {
118
+ value: newValue
119
+ });
120
+ }
121
+ if (action.type === "startQuery") {
122
+ return _objectSpread(_objectSpread({}, state), {}, {
123
+ loading: true,
124
+ query: action.query
125
+ });
126
+ }
127
+ if (action.type === "options") {
128
+ var _newState = _objectSpread(_objectSpread({}, state), {}, {
129
+ loading: false,
130
+ query: action.query
131
+ });
132
+ var optionList = _toConsumableArray(action.options);
133
+ if (autoComplete && action.query !== undefined) {
134
+ optionList = dedupe([action.query].concat(_toConsumableArray(optionList)));
135
+ }
136
+ var grouped = groupBy(optionList, groupProperty, "");
137
+ if (action.open) _newState.open = true;
138
+ _newState.items = [];
139
+ // add new item if allowCreate
140
+ if (allowCreate && action.query) _newState.items.push({
141
+ ___create___: true,
142
+ value: createOption(action.query)
143
+ });
144
+ Object.entries(grouped).forEach(function (_ref2) {
145
+ var _newState$items;
146
+ var _ref3 = _slicedToArray(_ref2, 2),
147
+ key = _ref3[0],
148
+ list = _ref3[1];
149
+ if (key) {
150
+ _newState.items.push({
151
+ ___group___: true,
152
+ label: key
153
+ });
154
+ }
155
+ (_newState$items = _newState.items).push.apply(_newState$items, _toConsumableArray(list));
156
+ });
157
+ if (action.setActive) {
158
+ var _index = _newState.items.findIndex(function (opt) {
159
+ return !opt.___group___;
160
+ });
161
+ _newState.activeIndex = _index >= 0 ? _index : null;
162
+ } else {
163
+ _newState.activeIndex = null;
164
+ }
165
+ listContentRef.current = _newState.items;
166
+ return _newState;
167
+ }
168
+ return state;
169
+ }, {
170
+ multiple: multiple,
171
+ emptyValue: emptyValue,
172
+ open: false,
173
+ loading: false,
174
+ activeIndex: null,
175
+ selectedIndex: null,
176
+ value: emptyValue,
177
+ // filter
178
+ query: undefined,
179
+ // dropdown items
180
+ items: []
181
+ }),
182
+ _useReducer2 = _slicedToArray(_useReducer, 2),
183
+ state = _useReducer2[0],
184
+ dispatch = _useReducer2[1];
185
+ useEffect(function () {
186
+ if (!multiple) {
187
+ var _ref4;
188
+ var newOption = (allowCreate || autoComplete) && deferred ? createOption(deferred) : undefined;
189
+ var hasValue = options.find(function (opt) {
190
+ var _opt$valueProperty;
191
+ return ((_opt$valueProperty = opt[valueProperty]) !== null && _opt$valueProperty !== void 0 ? _opt$valueProperty : opt) === deferred;
192
+ });
193
+ optionsRef.current = options;
194
+ // if current value missing in list inject it
195
+ if (!hasValue && newOption) {
196
+ optionsRef.current = [newOption].concat(_toConsumableArray(options !== null && options !== void 0 ? options : []));
197
+ }
198
+ dispatch({
199
+ type: "options",
200
+ options: optionsRef.current
201
+ });
202
+ dispatch({
203
+ type: "setValue",
204
+ value: (_ref4 = hasValue !== null && hasValue !== void 0 ? hasValue : newOption) !== null && _ref4 !== void 0 ? _ref4 : ""
205
+ });
206
+ }
207
+ if (multiple) {
208
+ optionsRef.current = [];
209
+ var _value = (deferred !== null && deferred !== void 0 ? deferred : []).map(function (item) {
210
+ var _ref5;
211
+ var newOption = (allowCreate || autoComplete) && item ? createOption(item) : undefined;
212
+ var hasValue = options.find(function (opt) {
213
+ var _opt$valueProperty2;
214
+ return ((_opt$valueProperty2 = opt[valueProperty]) !== null && _opt$valueProperty2 !== void 0 ? _opt$valueProperty2 : opt) === deferred;
215
+ });
216
+ if (!hasValue && newOption) {
217
+ optionsRef.current.push(newOption);
218
+ }
219
+ return (_ref5 = hasValue !== null && hasValue !== void 0 ? hasValue : newOption) !== null && _ref5 !== void 0 ? _ref5 : "";
220
+ });
221
+ optionsRef.current = [].concat(_toConsumableArray(optionsRef.current), _toConsumableArray(options !== null && options !== void 0 ? options : []));
222
+ dispatch({
223
+ type: "options",
224
+ options: optionsRef.current
225
+ });
226
+ dispatch({
227
+ type: "setValue",
228
+ value: _value
229
+ });
230
+ }
231
+ }, [deferred, options, multiple, allowCreate, autoComplete, createOption]);
232
+ var fireQuery = useDebounce(function (query) {
233
+ void Promise.resolve(onQuery === null || onQuery === void 0 ? void 0 : onQuery(query)).then(function (options) {
234
+ var newOptions = _toConsumableArray(options);
235
+ // if autocomplete inject other filtered options from history
236
+ if (autoComplete) {
237
+ newOptions.push.apply(newOptions, _toConsumableArray(optionsRef.current.filter(function (opt) {
238
+ return macthOption === null || macthOption === void 0 ? void 0 : macthOption(opt, query);
239
+ })));
240
+ }
241
+ dispatch({
242
+ type: "options",
243
+ query: query,
244
+ options: newOptions,
245
+ open: true,
246
+ setActive: true
247
+ });
248
+ });
249
+ }, [onQuery]);
250
+
251
+ // handlers
252
+ var handleQuery = useCallback(function (query) {
253
+ if (!query) {
254
+ dispatch({
255
+ type: "options",
256
+ query: "",
257
+ options: optionsRef.current
258
+ });
259
+ } else {
260
+ if (onQuery) {
261
+ dispatch({
262
+ type: "startQuery",
263
+ query: query
264
+ });
265
+ return fireQuery(query);
266
+ }
267
+ dispatch({
268
+ type: "options",
269
+ query: query,
270
+ open: true,
271
+ setActive: true,
272
+ options: optionsRef.current.filter(function (opt) {
273
+ return macthOption === null || macthOption === void 0 ? void 0 : macthOption(opt, query);
274
+ })
275
+ });
276
+ }
277
+ }, [macthOption, onQuery]);
278
+ var handleChange = useCallback(function (value) {
279
+ var values = value;
280
+ if (autoComplete && multiple && value) {
281
+ values = value.split(/,\s?/);
282
+ }
283
+ dispatch({
284
+ type: "changeValue",
285
+ value: value !== null && value !== void 0 && value.___create___ ? value.value : value
286
+ });
287
+ if (value !== null && value !== void 0 && value.___create___) {
288
+ optionsRef.current = [].concat(_toConsumableArray(optionsRef.current), [createOption(value.value)]);
289
+ }
290
+ if (autoComplete) {
291
+ optionsRef.current = dedupe([].concat(_toConsumableArray(isArray(values) ? values : [values]), _toConsumableArray(optionsRef.current)));
292
+ }
293
+ dispatch({
294
+ type: "options",
295
+ options: optionsRef.current
296
+ });
297
+ }, []);
298
+ var handleRemove = useCallback(function (index) {
299
+ dispatch({
300
+ type: "removeItem",
301
+ index: index
302
+ });
303
+ }, []);
304
+ var setOpen = useCallback(function (open) {
305
+ dispatch({
306
+ type: open ? "open" : "close"
307
+ });
308
+ dispatch({
309
+ type: "options",
310
+ query: undefined,
311
+ options: optionsRef.current
312
+ });
313
+ }, []);
314
+ var setActiveIndex = useCallback(function (index) {
315
+ dispatch({
316
+ type: "active",
317
+ index: index
318
+ });
319
+ }, []);
320
+ return {
321
+ state: state,
322
+ listRef: listRef,
323
+ focusableRef: focusableRef,
324
+ listContentRef: listContentRef,
325
+ handleChange: handleChange,
326
+ handleRemove: handleRemove,
327
+ handleQuery: handleQuery,
328
+ macthOption: macthOption,
329
+ setActiveIndex: setActiveIndex,
330
+ setOpen: setOpen,
331
+ setItemRef: function setItemRef(index, node) {
332
+ listRef.current[index] = node;
333
+ }
334
+ };
335
+ };
336
+
337
+ export { useSelect };
@@ -0,0 +1,50 @@
1
+ 'use client';
2
+
3
+ import { debounce } from '@react-fabric/utilities';
4
+
5
+ /*
6
+ * React Fabric
7
+ * @version: 1.0.0
8
+ *
9
+ *
10
+ * The MIT License (MIT)
11
+ * Copyright (c) 2024 Adarsh Pastakia
12
+ *
13
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
14
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
15
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
16
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
17
+ * subject to the following conditions:
18
+ *
19
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
20
+ * portions of the Software.
21
+ *
22
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
23
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+ */
27
+
28
+ /** ***************** common handler for enter press *******************/
29
+ var handleEnter = function handleEnter(callback) {
30
+ var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
31
+ _ref$stopPropagation = _ref.stopPropagation,
32
+ stopPropagation = _ref$stopPropagation === void 0 ? false : _ref$stopPropagation,
33
+ _ref$preventDefault = _ref.preventDefault,
34
+ preventDefault = _ref$preventDefault === void 0 ? false : _ref$preventDefault;
35
+ var cbHandle = debounce(callback !== null && callback !== void 0 ? callback : function () {
36
+ return undefined;
37
+ }, 100);
38
+ return function (e) {
39
+ if (!e.shiftKey && !e.altKey && !e.metaKey && e.key === "Enter" && callback) {
40
+ if (stopPropagation) e === null || e === void 0 || e.stopPropagation();
41
+ cbHandle(e);
42
+ if (preventDefault) {
43
+ e.preventDefault();
44
+ return false;
45
+ }
46
+ }
47
+ };
48
+ };
49
+
50
+ export { handleEnter };
@@ -0,0 +1,39 @@
1
+ 'use client';
2
+
3
+ import { isColor } from '@react-fabric/utilities';
4
+
5
+ /*
6
+ * React Fabric
7
+ * @version: 1.0.0
8
+ *
9
+ *
10
+ * The MIT License (MIT)
11
+ * Copyright (c) 2024 Adarsh Pastakia
12
+ *
13
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
14
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
15
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
16
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
17
+ * subject to the following conditions:
18
+ *
19
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
20
+ * portions of the Software.
21
+ *
22
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
23
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+ */
27
+
28
+ var getColor = function getColor(color) {
29
+ if (color && isColor(color)) {
30
+ return color;
31
+ }
32
+ };
33
+ var getBgClass = function getBgClass(bg) {
34
+ if (bg && !isColor(bg)) {
35
+ return "bg-".concat(bg);
36
+ }
37
+ };
38
+
39
+ export { getBgClass, getColor };
@@ -0,0 +1,8 @@
1
+ import { type UploadHandler } from "../hooks/useFileUploader";
2
+ import { type FileSchema } from "../types/schema";
3
+ export declare const FileField: ({ uploadHandler, inline, label, accept, multiple, value, ...rest }: Partial<FileSchema> & {
4
+ value?: AnyObject;
5
+ inline?: boolean;
6
+ fileUrl?: (path: string) => string;
7
+ uploadHandler: UploadHandler;
8
+ }) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,9 @@
1
+ import { type UploadHandler } from "../hooks/useFileUploader";
2
+ import { type SchemaType } from "../types/schema";
3
+ export declare const FormField: ({ id, label, required, inline, prefix, autoFocus, defaultValue, fileUrl, uploadHandler, ...rest }: SchemaType & {
4
+ inline?: boolean;
5
+ prefix?: string;
6
+ autoFocus?: boolean;
7
+ fileUrl?: (path: string) => string;
8
+ uploadHandler?: UploadHandler;
9
+ }) => import("react/jsx-runtime").JSX.Element | undefined;
@@ -0,0 +1,20 @@
1
+ import { type ReactElement } from "react";
2
+ export interface ControllerProps {
3
+ /**
4
+ * field name
5
+ */
6
+ name?: string;
7
+ children: ReactElement | ((props: {
8
+ name: string;
9
+ error?: string;
10
+ checked: boolean;
11
+ value: AnyObject;
12
+ invalid?: boolean;
13
+ readOnly?: boolean;
14
+ disabled?: boolean;
15
+ onBlur: () => void;
16
+ onChange: (value: AnyObject) => void;
17
+ inputRef?: React.Ref<HTMLInputElement>;
18
+ }) => ReactElement);
19
+ }
20
+ export declare const Controller: ({ name, children, ...rest }: ControllerProps) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,33 @@
1
+ import { type yup } from "@react-fabric/utilities";
2
+ import { type PropsWithChildren, type Ref } from "react";
3
+ import { type FormState } from "react-hook-form";
4
+ export interface FormRef<K extends KeyValue> {
5
+ state: FormState<K>;
6
+ reset: () => void;
7
+ clear: () => void;
8
+ submit: () => void;
9
+ validate: () => Promise<boolean>;
10
+ getValues: () => K;
11
+ setValue: (key: keyof K, value: AnyObject) => void;
12
+ setValues: (values: K) => void;
13
+ }
14
+ export interface FormProps<K extends KeyValue = KeyValue> {
15
+ formRef?: Ref<FormRef<K>>;
16
+ /**
17
+ * form data schema
18
+ */
19
+ schema?: yup.ObjectSchema<K>;
20
+ /**
21
+ * default data values
22
+ */
23
+ defaultValues?: K;
24
+ /**
25
+ * change callback
26
+ */
27
+ onChange?: (values: K) => void;
28
+ /**
29
+ * submit callback after successful validation
30
+ */
31
+ onSubmit?: (values: K) => void;
32
+ }
33
+ export declare const Form: <K extends KeyValue<any>>({ formRef, schema, children, defaultValues, onSubmit, onChange, ...rest }: PropsWithChildren<FormProps<K>>) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,23 @@
1
+ import { type AxiosRequestConfig } from "axios";
2
+ export type UploadHandler = (data: FormData, config: AxiosRequestConfig) => Promise<string>;
3
+ interface FileInfo {
4
+ key: string;
5
+ filename: string;
6
+ size: number;
7
+ mime: string;
8
+ uploaded?: true;
9
+ file?: File;
10
+ path?: string;
11
+ error?: string;
12
+ progress?: number;
13
+ abort?: AbortController["abort"];
14
+ }
15
+ export declare const useFileUploader: (uploader: UploadHandler, initialList?: string | FileInfo | FileInfo[], multiple?: boolean) => {
16
+ upload: (files: FileList | null) => void;
17
+ remove: (key: string) => void;
18
+ retry: (key: string) => void;
19
+ pending: number;
20
+ list: FileInfo[];
21
+ files: FileInfo[];
22
+ };
23
+ export {};
@@ -0,0 +1,16 @@
1
+ import { yup } from "@react-fabric/utilities";
2
+ import { type FormSchema } from "../types/schema";
3
+ import { type UploadHandler } from "./useFileUploader";
4
+ export declare const useFormBuilder: (schema: FormSchema[], options?: {
5
+ prefix?: string;
6
+ inline?: boolean;
7
+ fileUrl?: (path: string) => string;
8
+ uploadHandler?: UploadHandler;
9
+ }) => {
10
+ formDef: import("react/jsx-runtime").JSX.Element;
11
+ schemaDef: yup.ObjectSchema<{
12
+ [x: string]: any;
13
+ }, yup.AnyObject, {
14
+ [x: string]: any;
15
+ }, "">;
16
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ import "./i18n";
2
+ export { Controller } from "./form/Controller";
3
+ export { Form, type FormRef } from "./form/Form";
4
+ export { useFileUploader } from "./hooks/useFileUploader";
5
+ export { useFormBuilder } from "./hooks/useFormBuilder";
6
+ export { type FormSchema } from "./types/schema";
7
+ export { ArrayInput } from "./input/ArrayInput";
8
+ export { Checkbox } from "./input/Checkbox";
9
+ export { ColorInput } from "./input/ColorInput";
10
+ export { ColorPicker } from "./input/ColorPicker";
11
+ export { Field } from "./input/Field";
12
+ export { FileInput } from "./input/File";
13
+ export { HiddenInput } from "./input/Hidden";
14
+ export { Input } from "./input/Input";
15
+ export { Number } from "./input/Number";
16
+ export { Password } from "./input/Password";
17
+ export { Radio } from "./input/Radio";
18
+ export { RadioGroup } from "./input/RadioGroup";
19
+ export { Range } from "./input/Range";
20
+ export { Search } from "./input/Search";
21
+ export { Slider } from "./input/Slider";
22
+ export { Switch } from "./input/Switch";
23
+ export { Textarea } from "./input/Textarea";
24
+ export { AutoComplete } from "./select/AutoComplete";
25
+ export { DualList } from "./select/DualList";
26
+ export { List } from "./select/List";
27
+ export { Select } from "./select/Select";
@@ -0,0 +1,64 @@
1
+ import { type ReactNode, type RefObject } from "react";
2
+ export interface ArrayInputProps {
3
+ /**
4
+ * input label
5
+ */
6
+ label?: string;
7
+ /**
8
+ * append to label end
9
+ */
10
+ appendLabel?: JSX.Element | string | number | boolean;
11
+ /**
12
+ * info label at bottom
13
+ */
14
+ info?: string;
15
+ /**
16
+ * required
17
+ */
18
+ required?: boolean;
19
+ /**
20
+ * disabled input
21
+ */
22
+ disabled?: boolean;
23
+ /**
24
+ * read-only input
25
+ */
26
+ readOnly?: boolean;
27
+ /**
28
+ * field name
29
+ */
30
+ name: string;
31
+ /**
32
+ * minimum required length
33
+ */
34
+ minItems?: number;
35
+ /**
36
+ * field name to focus on add
37
+ */
38
+ focusName?: string;
39
+ /**
40
+ * enable drag-n-drop sorting
41
+ */
42
+ enableSorting?: boolean;
43
+ children: React.ReactNode | ((props: {
44
+ index: number;
45
+ name: string;
46
+ }) => ReactNode);
47
+ /**
48
+ * add item button label
49
+ */
50
+ addLabel?: string;
51
+ /**
52
+ * fixed length list
53
+ */
54
+ fixedList?: boolean;
55
+ arrayRef?: RefObject<{
56
+ addItem: (item: AnyObject) => void;
57
+ removeItem: (idx: number) => void;
58
+ }>;
59
+ /**
60
+ * Add item
61
+ */
62
+ onAdd?: () => AnyObject;
63
+ }
64
+ export declare const ArrayInput: ({ name, children, addLabel, fixedList, focusName, onAdd, arrayRef, enableSorting, disabled, readOnly, minItems, ...rest }: ArrayInputProps) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,31 @@
1
+ import { type RefProp } from "@react-fabric/core/dist/types/types";
2
+ import { type UploadHandler } from "../hooks/useFileUploader";
3
+ export interface Props extends RefProp {
4
+ /**
5
+ * field name
6
+ */
7
+ name?: string;
8
+ /**
9
+ * actual avatar storage path
10
+ */
11
+ value?: string;
12
+ /**
13
+ * avatar stream src
14
+ */
15
+ src?: string;
16
+ size?: string | number;
17
+ /**
18
+ * upload handler to store file in temp storage
19
+ */
20
+ uploadHandler: UploadHandler;
21
+ fileUrl?: (path: string) => string;
22
+ /**
23
+ * change handler
24
+ *
25
+ * @param file
26
+ * @param base64
27
+ * @returns storage path
28
+ */
29
+ onChange?: (path: string) => void;
30
+ }
31
+ export declare const AvatarInput: ({ src, size, fileUrl, uploadHandler, ...rest }: Props) => import("react/jsx-runtime").JSX.Element;