@saas-ui/forms 1.5.3 → 2.0.0-next.0
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +11 -2
- package/dist/ajv/index.d.ts +21 -2
- package/dist/ajv/index.js +31 -2
- package/dist/ajv/index.js.map +1 -1
- package/dist/ajv/index.mjs +25 -0
- package/dist/ajv/index.mjs.map +1 -0
- package/dist/index.d.ts +606 -18
- package/dist/index.js +1251 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1140 -0
- package/dist/index.mjs.map +1 -0
- package/dist/yup/index.d.ts +41 -2
- package/dist/yup/index.js +95 -2
- package/dist/yup/index.js.map +1 -1
- package/dist/yup/index.mjs +86 -0
- package/dist/yup/index.mjs.map +1 -0
- package/dist/zod/index.d.ts +38 -2
- package/dist/zod/index.js +95 -2
- package/dist/zod/index.js.map +1 -1
- package/dist/zod/index.mjs +85 -0
- package/dist/zod/index.mjs.map +1 -0
- package/package.json +18 -28
- package/src/array-field.tsx +20 -13
- package/src/auto-form.tsx +13 -19
- package/src/field-resolver.ts +1 -1
- package/src/field.tsx +4 -9
- package/src/fields.tsx +1 -3
- package/src/form.tsx +69 -25
- package/src/index.ts +3 -0
- package/src/input-right-button/index.ts +1 -0
- package/src/input-right-button/input-right-button.stories.tsx +47 -0
- package/src/input-right-button/input-right-button.test.tsx +12 -0
- package/src/input-right-button/input-right-button.tsx +26 -0
- package/src/layout.tsx +1 -1
- package/src/number-input/index.ts +1 -0
- package/src/number-input/number-input.stories.tsx +39 -0
- package/src/number-input/number-input.test.tsx +6 -0
- package/src/number-input/number-input.tsx +56 -0
- package/src/object-field.tsx +1 -1
- package/src/password-input/index.ts +1 -0
- package/src/password-input/password-input.stories.tsx +50 -0
- package/src/password-input/password-input.test.tsx +20 -0
- package/src/password-input/password-input.tsx +69 -0
- package/src/pin-input/index.ts +1 -0
- package/src/pin-input/pin-input.stories.tsx +38 -0
- package/src/pin-input/pin-input.test.tsx +6 -0
- package/src/pin-input/pin-input.tsx +50 -0
- package/src/radio/index.ts +1 -0
- package/src/radio/radio-input.stories.tsx +45 -0
- package/src/radio/radio-input.tsx +58 -0
- package/src/radio/radio.test.tsx +6 -0
- package/src/select/index.ts +2 -0
- package/src/select/native-select.tsx +42 -0
- package/src/select/select.stories.tsx +144 -0
- package/src/select/select.test.tsx +8 -0
- package/src/select/select.tsx +185 -0
- package/src/step-form.tsx +24 -13
- package/src/submit-button.tsx +32 -38
- package/src/use-step-form.tsx +1 -1
- package/ajv/package.json +0 -28
- package/dist/ajv/ajv-resolver.d.ts +0 -11
- package/dist/ajv/ajv-resolver.d.ts.map +0 -1
- package/dist/ajv/index.d.ts.map +0 -1
- package/dist/ajv/index.modern.mjs +0 -2
- package/dist/ajv/index.modern.mjs.map +0 -1
- package/dist/array-field.d.ts +0 -64
- package/dist/array-field.d.ts.map +0 -1
- package/dist/auto-form.d.ts +0 -32
- package/dist/auto-form.d.ts.map +0 -1
- package/dist/display-field.d.ts +0 -10
- package/dist/display-field.d.ts.map +0 -1
- package/dist/display-if.d.ts +0 -15
- package/dist/display-if.d.ts.map +0 -1
- package/dist/field-resolver.d.ts +0 -13
- package/dist/field-resolver.d.ts.map +0 -1
- package/dist/field.d.ts +0 -147
- package/dist/field.d.ts.map +0 -1
- package/dist/fields.d.ts +0 -9
- package/dist/fields.d.ts.map +0 -1
- package/dist/form.d.ts +0 -44
- package/dist/form.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.modern.mjs +0 -2
- package/dist/index.modern.mjs.map +0 -1
- package/dist/layout.d.ts +0 -14
- package/dist/layout.d.ts.map +0 -1
- package/dist/object-field.d.ts +0 -12
- package/dist/object-field.d.ts.map +0 -1
- package/dist/step-form.d.ts +0 -38
- package/dist/step-form.d.ts.map +0 -1
- package/dist/submit-button.d.ts +0 -20
- package/dist/submit-button.d.ts.map +0 -1
- package/dist/use-array-field.d.ts +0 -95
- package/dist/use-array-field.d.ts.map +0 -1
- package/dist/use-step-form.d.ts +0 -40
- package/dist/use-step-form.d.ts.map +0 -1
- package/dist/utils.d.ts +0 -3
- package/dist/utils.d.ts.map +0 -1
- package/dist/watch-field.d.ts +0 -11
- package/dist/watch-field.d.ts.map +0 -1
- package/dist/yup/index.d.ts.map +0 -1
- package/dist/yup/index.modern.mjs +0 -2
- package/dist/yup/index.modern.mjs.map +0 -1
- package/dist/yup/yup-resolver.d.ts +0 -29
- package/dist/yup/yup-resolver.d.ts.map +0 -1
- package/dist/zod/index.d.ts.map +0 -1
- package/dist/zod/index.modern.mjs +0 -2
- package/dist/zod/index.modern.mjs.map +0 -1
- package/dist/zod/zod-resolver.d.ts +0 -35
- package/dist/zod/zod-resolver.d.ts.map +0 -1
- package/yup/package.json +0 -26
- package/zod/package.json +0 -27
package/dist/index.mjs
ADDED
@@ -0,0 +1,1140 @@
|
|
1
|
+
import * as React8 from 'react';
|
2
|
+
import React8__default, { useState } from 'react';
|
3
|
+
import { __DEV__, cx, runIfFn, callAllHandlers, get as get$1 } from '@chakra-ui/utils';
|
4
|
+
import { useForm, FormProvider, useFormContext, Controller, useFieldArray, useWatch, get } from 'react-hook-form';
|
5
|
+
export { Controller, FormProvider, appendErrors, useController, useFieldArray, useForm, useFormContext, useFormState, useWatch } from 'react-hook-form';
|
6
|
+
import { forwardRef, NumberInput as NumberInput$1, NumberInputField, NumberInputStepper, NumberIncrementStepper, NumberDecrementStepper, InputRightElement, Button, InputGroup, Input, RadioGroup, Stack, Radio, useMultiStyleConfig, MenuButton, omitThemingProps, useFormControl, Menu, chakra, MenuList, MenuOptionGroup, MenuItemOption, Select as Select$1, Switch, Checkbox, HStack, PinInput, FormControl, FormLabel, Text, Box, FormHelperText, FormErrorMessage, useMergeRefs, Textarea, PinInputField, useTheme, SimpleGrid, useStyleConfig } from '@chakra-ui/react';
|
7
|
+
import { ViewIcon, ViewOffIcon, ChevronDownIcon, MinusIcon, AddIcon } from '@chakra-ui/icons';
|
8
|
+
import { createContext } from '@chakra-ui/react-utils';
|
9
|
+
import { StepperProvider, useStepper, useStep, useStepperContext, StepperStep, StepperContainer, StepperSteps } from '@saas-ui/core';
|
10
|
+
|
11
|
+
// src/display-field.tsx
|
12
|
+
var DisplayField = ({
|
13
|
+
name,
|
14
|
+
label,
|
15
|
+
placeholder,
|
16
|
+
...props
|
17
|
+
}) => {
|
18
|
+
return /* @__PURE__ */ React8.createElement(FormControl, {
|
19
|
+
...props
|
20
|
+
}, label ? /* @__PURE__ */ React8.createElement(FormLabel, {
|
21
|
+
htmlFor: name
|
22
|
+
}, label) : null, /* @__PURE__ */ React8.createElement(Text, {
|
23
|
+
fontSize: "md"
|
24
|
+
}, /* @__PURE__ */ React8.createElement(FormValue, {
|
25
|
+
name
|
26
|
+
})));
|
27
|
+
};
|
28
|
+
if (__DEV__) {
|
29
|
+
DisplayField.displayName = "DisplayField";
|
30
|
+
}
|
31
|
+
var FormValue = ({ name }) => {
|
32
|
+
const { getValues } = useFormContext();
|
33
|
+
return getValues(name) || null;
|
34
|
+
};
|
35
|
+
if (__DEV__) {
|
36
|
+
FormValue.displayName = "FormValue";
|
37
|
+
}
|
38
|
+
var NumberInput = forwardRef((props, ref) => {
|
39
|
+
const { hideStepper, incrementIcon, decrementIcon, ...rest } = props;
|
40
|
+
return /* @__PURE__ */ React8.createElement(NumberInput$1, {
|
41
|
+
...rest,
|
42
|
+
ref
|
43
|
+
}, /* @__PURE__ */ React8.createElement(NumberInputField, null), !hideStepper && /* @__PURE__ */ React8.createElement(NumberInputStepper, null, /* @__PURE__ */ React8.createElement(NumberIncrementStepper, {
|
44
|
+
children: incrementIcon
|
45
|
+
}), /* @__PURE__ */ React8.createElement(NumberDecrementStepper, {
|
46
|
+
children: decrementIcon
|
47
|
+
})));
|
48
|
+
});
|
49
|
+
NumberInput.defaultProps = {
|
50
|
+
hideStepper: false
|
51
|
+
};
|
52
|
+
if (__DEV__) {
|
53
|
+
NumberInput.displayName = "NumberInput";
|
54
|
+
}
|
55
|
+
var InputRightButton = forwardRef(
|
56
|
+
(props, ref) => {
|
57
|
+
return /* @__PURE__ */ React8.createElement(InputRightElement, {
|
58
|
+
w: "auto",
|
59
|
+
px: "1",
|
60
|
+
py: "1",
|
61
|
+
alignItems: "stretch"
|
62
|
+
}, /* @__PURE__ */ React8.createElement(Button, {
|
63
|
+
ref,
|
64
|
+
height: "auto",
|
65
|
+
...props
|
66
|
+
}));
|
67
|
+
}
|
68
|
+
);
|
69
|
+
InputRightButton.id = "InputRightElement";
|
70
|
+
InputRightButton.displayName = "InputRightButton";
|
71
|
+
|
72
|
+
// src/password-input/password-input.tsx
|
73
|
+
var PasswordInput = forwardRef(
|
74
|
+
(props, ref) => {
|
75
|
+
const {
|
76
|
+
viewIcon,
|
77
|
+
viewOffIcon,
|
78
|
+
autoComplete,
|
79
|
+
w,
|
80
|
+
width,
|
81
|
+
size,
|
82
|
+
variant,
|
83
|
+
...inputProps
|
84
|
+
} = props;
|
85
|
+
const [show, setShow] = useState(false);
|
86
|
+
const handleClick = () => setShow(!show);
|
87
|
+
const label = show ? "Hide password" : "Show password";
|
88
|
+
let icon;
|
89
|
+
if (show) {
|
90
|
+
icon = viewIcon || /* @__PURE__ */ React8__default.createElement(ViewIcon, null);
|
91
|
+
} else {
|
92
|
+
icon = viewOffIcon || /* @__PURE__ */ React8__default.createElement(ViewOffIcon, null);
|
93
|
+
}
|
94
|
+
const groupProps = {
|
95
|
+
width: w || width,
|
96
|
+
size,
|
97
|
+
variant
|
98
|
+
};
|
99
|
+
return /* @__PURE__ */ React8__default.createElement(InputGroup, {
|
100
|
+
...groupProps
|
101
|
+
}, /* @__PURE__ */ React8__default.createElement(Input, {
|
102
|
+
...inputProps,
|
103
|
+
ref,
|
104
|
+
type: show ? "text" : "password",
|
105
|
+
autoComplete: show ? "off" : autoComplete
|
106
|
+
}), /* @__PURE__ */ React8__default.createElement(InputRightButton, {
|
107
|
+
onClick: handleClick,
|
108
|
+
"aria-label": label,
|
109
|
+
variant: "ghost"
|
110
|
+
}, icon));
|
111
|
+
}
|
112
|
+
);
|
113
|
+
if (__DEV__) {
|
114
|
+
PasswordInput.displayName = "PasswordInput";
|
115
|
+
}
|
116
|
+
var RadioInput = forwardRef(
|
117
|
+
({ options, spacing, direction, ...props }, ref) => {
|
118
|
+
const { onBlur, onChange, ...groupProps } = props;
|
119
|
+
return /* @__PURE__ */ React8.createElement(RadioGroup, {
|
120
|
+
onChange,
|
121
|
+
...groupProps
|
122
|
+
}, /* @__PURE__ */ React8.createElement(Stack, {
|
123
|
+
spacing,
|
124
|
+
direction
|
125
|
+
}, options.map(({ value, label, ...radioProps }, i) => {
|
126
|
+
return /* @__PURE__ */ React8.createElement(Radio, {
|
127
|
+
key: i,
|
128
|
+
onBlur,
|
129
|
+
value,
|
130
|
+
ref,
|
131
|
+
...radioProps
|
132
|
+
}, label || value);
|
133
|
+
})));
|
134
|
+
}
|
135
|
+
);
|
136
|
+
if (__DEV__) {
|
137
|
+
RadioInput.displayName = "RadioInput";
|
138
|
+
}
|
139
|
+
var SelectButton = forwardRef((props, ref) => {
|
140
|
+
const styles = useMultiStyleConfig("Input", props);
|
141
|
+
const focusStyles = styles.field._focusVisible;
|
142
|
+
const height = styles.field.h || styles.field.height;
|
143
|
+
const buttonStyles = {
|
144
|
+
fontWeight: "normal",
|
145
|
+
textAlign: "left",
|
146
|
+
color: "inherit",
|
147
|
+
_active: {
|
148
|
+
bg: "transparent"
|
149
|
+
},
|
150
|
+
minH: height,
|
151
|
+
_focus: focusStyles,
|
152
|
+
_expanded: focusStyles,
|
153
|
+
...styles.field,
|
154
|
+
h: "auto"
|
155
|
+
};
|
156
|
+
return /* @__PURE__ */ React8.createElement(MenuButton, {
|
157
|
+
as: Button,
|
158
|
+
...props,
|
159
|
+
ref,
|
160
|
+
sx: buttonStyles
|
161
|
+
});
|
162
|
+
});
|
163
|
+
if (__DEV__) {
|
164
|
+
SelectButton.displayName = "SelectButton";
|
165
|
+
}
|
166
|
+
var Select = forwardRef((props, ref) => {
|
167
|
+
const {
|
168
|
+
name,
|
169
|
+
options,
|
170
|
+
children,
|
171
|
+
onChange,
|
172
|
+
defaultValue,
|
173
|
+
value,
|
174
|
+
placeholder,
|
175
|
+
isDisabled,
|
176
|
+
leftIcon,
|
177
|
+
rightIcon = /* @__PURE__ */ React8.createElement(ChevronDownIcon, null),
|
178
|
+
multiple,
|
179
|
+
size,
|
180
|
+
variant,
|
181
|
+
menuListProps,
|
182
|
+
renderValue = (value2) => value2 == null ? void 0 : value2.join(", "),
|
183
|
+
...rest
|
184
|
+
} = props;
|
185
|
+
const menuProps = omitThemingProps(rest);
|
186
|
+
const [currentValue, setCurrentValue] = React8.useState(value || defaultValue);
|
187
|
+
const controlProps = useFormControl({ name });
|
188
|
+
const handleChange = (value2) => {
|
189
|
+
setCurrentValue(value2);
|
190
|
+
onChange == null ? void 0 : onChange(value2);
|
191
|
+
};
|
192
|
+
const buttonProps = {
|
193
|
+
isDisabled,
|
194
|
+
leftIcon,
|
195
|
+
rightIcon,
|
196
|
+
size,
|
197
|
+
variant
|
198
|
+
};
|
199
|
+
const getDisplayValue = React8.useCallback(
|
200
|
+
(value2) => {
|
201
|
+
if (!options) {
|
202
|
+
return value2;
|
203
|
+
}
|
204
|
+
for (const option of options) {
|
205
|
+
if (option.label && option.value === value2) {
|
206
|
+
return option.label;
|
207
|
+
}
|
208
|
+
}
|
209
|
+
return value2;
|
210
|
+
},
|
211
|
+
[options]
|
212
|
+
);
|
213
|
+
const displayValue = currentValue ? (Array.isArray(currentValue) ? currentValue : [currentValue]).map(
|
214
|
+
getDisplayValue
|
215
|
+
) : [];
|
216
|
+
return /* @__PURE__ */ React8.createElement(Menu, {
|
217
|
+
...menuProps,
|
218
|
+
closeOnSelect: !multiple
|
219
|
+
}, /* @__PURE__ */ React8.createElement(chakra.div, {
|
220
|
+
className: cx("sui-select")
|
221
|
+
}, /* @__PURE__ */ React8.createElement(SelectButton, {
|
222
|
+
ref,
|
223
|
+
...buttonProps
|
224
|
+
}, renderValue(displayValue) || placeholder), /* @__PURE__ */ React8.createElement(MenuList, {
|
225
|
+
maxH: "60vh",
|
226
|
+
overflowY: "auto",
|
227
|
+
...menuListProps
|
228
|
+
}, /* @__PURE__ */ React8.createElement(MenuOptionGroup, {
|
229
|
+
defaultValue: defaultValue || value,
|
230
|
+
onChange: handleChange,
|
231
|
+
type: multiple ? "checkbox" : "radio"
|
232
|
+
}, options ? options.map(({ value: value2, label, ...rest2 }, i) => /* @__PURE__ */ React8.createElement(MenuItemOption, {
|
233
|
+
key: i,
|
234
|
+
value: value2,
|
235
|
+
...rest2
|
236
|
+
}, label || value2)) : children)), /* @__PURE__ */ React8.createElement(chakra.input, {
|
237
|
+
...controlProps,
|
238
|
+
name,
|
239
|
+
type: "hidden",
|
240
|
+
value: currentValue,
|
241
|
+
className: "saas-select__input"
|
242
|
+
})));
|
243
|
+
});
|
244
|
+
if (__DEV__) {
|
245
|
+
Select.displayName = "Select";
|
246
|
+
}
|
247
|
+
var NativeSelect = forwardRef(
|
248
|
+
({ options, children, ...props }, ref) => {
|
249
|
+
return /* @__PURE__ */ React8.createElement(Select$1, {
|
250
|
+
ref,
|
251
|
+
...props
|
252
|
+
}, children || (options == null ? void 0 : options.map(({ value, label }) => {
|
253
|
+
return /* @__PURE__ */ React8.createElement("option", {
|
254
|
+
key: value,
|
255
|
+
value
|
256
|
+
}, label || value);
|
257
|
+
})));
|
258
|
+
}
|
259
|
+
);
|
260
|
+
if (__DEV__) {
|
261
|
+
NativeSelect.displayName = "NativeSelect";
|
262
|
+
}
|
263
|
+
|
264
|
+
// src/field.tsx
|
265
|
+
var inputTypes = {};
|
266
|
+
var defaultInputType = "text";
|
267
|
+
var getInput = (type) => {
|
268
|
+
return inputTypes[type] || inputTypes[defaultInputType];
|
269
|
+
};
|
270
|
+
var getError = (name, formState) => {
|
271
|
+
return get(formState.errors, name);
|
272
|
+
};
|
273
|
+
var BaseField = (props) => {
|
274
|
+
const { name, label, help, hideLabel, children, ...controlProps } = props;
|
275
|
+
const { formState } = useFormContext();
|
276
|
+
const error = getError(name, formState);
|
277
|
+
return /* @__PURE__ */ React8.createElement(FormControl, {
|
278
|
+
...controlProps,
|
279
|
+
isInvalid: !!error
|
280
|
+
}, label && !hideLabel ? /* @__PURE__ */ React8.createElement(FormLabel, null, label) : null, /* @__PURE__ */ React8.createElement(Box, null, children, help && !(error == null ? void 0 : error.message) ? /* @__PURE__ */ React8.createElement(FormHelperText, null, help) : null, (error == null ? void 0 : error.message) && /* @__PURE__ */ React8.createElement(FormErrorMessage, null, error == null ? void 0 : error.message)));
|
281
|
+
};
|
282
|
+
if (__DEV__) {
|
283
|
+
BaseField.displayName = "BaseField";
|
284
|
+
}
|
285
|
+
var Field = React8.forwardRef(
|
286
|
+
(props, ref) => {
|
287
|
+
const { type = defaultInputType } = props;
|
288
|
+
const InputComponent = getInput(type);
|
289
|
+
return /* @__PURE__ */ React8.createElement(InputComponent, {
|
290
|
+
ref,
|
291
|
+
...props
|
292
|
+
});
|
293
|
+
}
|
294
|
+
);
|
295
|
+
var createField = (InputComponent, { displayName, hideLabel, BaseField: BaseField2 }) => {
|
296
|
+
const Field2 = forwardRef((props, ref) => {
|
297
|
+
const {
|
298
|
+
id,
|
299
|
+
name,
|
300
|
+
label,
|
301
|
+
help,
|
302
|
+
isDisabled,
|
303
|
+
isInvalid,
|
304
|
+
isReadOnly,
|
305
|
+
isRequired,
|
306
|
+
rules,
|
307
|
+
...inputProps
|
308
|
+
} = props;
|
309
|
+
const inputRules = {
|
310
|
+
required: isRequired,
|
311
|
+
...rules
|
312
|
+
};
|
313
|
+
return /* @__PURE__ */ React8.createElement(BaseField2, {
|
314
|
+
id,
|
315
|
+
name,
|
316
|
+
label,
|
317
|
+
help,
|
318
|
+
hideLabel,
|
319
|
+
isDisabled,
|
320
|
+
isInvalid,
|
321
|
+
isReadOnly,
|
322
|
+
isRequired
|
323
|
+
}, /* @__PURE__ */ React8.createElement(InputComponent, {
|
324
|
+
ref,
|
325
|
+
id,
|
326
|
+
name,
|
327
|
+
label: hideLabel ? label : void 0,
|
328
|
+
rules: inputRules,
|
329
|
+
...inputProps
|
330
|
+
}));
|
331
|
+
});
|
332
|
+
Field2.displayName = displayName;
|
333
|
+
return Field2;
|
334
|
+
};
|
335
|
+
var withControlledInput = (InputComponent) => {
|
336
|
+
return forwardRef(
|
337
|
+
({ name, rules, ...inputProps }, ref) => {
|
338
|
+
const { control } = useFormContext();
|
339
|
+
return /* @__PURE__ */ React8.createElement(Controller, {
|
340
|
+
name,
|
341
|
+
control,
|
342
|
+
rules,
|
343
|
+
render: ({ field: { ref: _ref, ...field } }) => /* @__PURE__ */ React8.createElement(InputComponent, {
|
344
|
+
...field,
|
345
|
+
...inputProps,
|
346
|
+
onChange: callAllHandlers(inputProps.onChange, field.onChange),
|
347
|
+
onBlur: callAllHandlers(inputProps.onBlur, field.onBlur),
|
348
|
+
ref: useMergeRefs(ref, _ref)
|
349
|
+
})
|
350
|
+
});
|
351
|
+
}
|
352
|
+
);
|
353
|
+
};
|
354
|
+
var withUncontrolledInput = (InputComponent) => {
|
355
|
+
return forwardRef(
|
356
|
+
({ name, rules, ...inputProps }, ref) => {
|
357
|
+
const { register } = useFormContext();
|
358
|
+
const { ref: _ref, ...field } = register(name, rules);
|
359
|
+
return /* @__PURE__ */ React8.createElement(InputComponent, {
|
360
|
+
...field,
|
361
|
+
...inputProps,
|
362
|
+
onChange: callAllHandlers(inputProps.onChange, field.onChange),
|
363
|
+
onBlur: callAllHandlers(inputProps.onBlur, field.onBlur),
|
364
|
+
ref: useMergeRefs(ref, _ref)
|
365
|
+
});
|
366
|
+
}
|
367
|
+
);
|
368
|
+
};
|
369
|
+
var registerFieldType = (type, component, options) => {
|
370
|
+
let InputComponent;
|
371
|
+
if (options == null ? void 0 : options.isControlled) {
|
372
|
+
InputComponent = withControlledInput(component);
|
373
|
+
} else {
|
374
|
+
InputComponent = withUncontrolledInput(component);
|
375
|
+
}
|
376
|
+
const Field2 = createField(InputComponent, {
|
377
|
+
displayName: `${type.split("-").map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("")}Field`,
|
378
|
+
hideLabel: options == null ? void 0 : options.hideLabel,
|
379
|
+
BaseField: (options == null ? void 0 : options.BaseField) || BaseField
|
380
|
+
});
|
381
|
+
inputTypes[type] = Field2;
|
382
|
+
return Field2;
|
383
|
+
};
|
384
|
+
var InputField = registerFieldType(
|
385
|
+
"text",
|
386
|
+
forwardRef(({ type = "text", leftAddon, rightAddon, size, ...rest }, ref) => {
|
387
|
+
const input = /* @__PURE__ */ React8.createElement(Input, {
|
388
|
+
type,
|
389
|
+
size,
|
390
|
+
...rest,
|
391
|
+
ref
|
392
|
+
});
|
393
|
+
if (leftAddon || rightAddon) {
|
394
|
+
return /* @__PURE__ */ React8.createElement(InputGroup, {
|
395
|
+
size
|
396
|
+
}, leftAddon, input, rightAddon);
|
397
|
+
}
|
398
|
+
return input;
|
399
|
+
})
|
400
|
+
);
|
401
|
+
var NumberInputField2 = registerFieldType(
|
402
|
+
"number",
|
403
|
+
NumberInput,
|
404
|
+
{
|
405
|
+
isControlled: true
|
406
|
+
}
|
407
|
+
);
|
408
|
+
var PasswordInputField = registerFieldType(
|
409
|
+
"password",
|
410
|
+
forwardRef((props, ref) => /* @__PURE__ */ React8.createElement(PasswordInput, {
|
411
|
+
ref,
|
412
|
+
...props
|
413
|
+
}))
|
414
|
+
);
|
415
|
+
var TextareaField = registerFieldType(
|
416
|
+
"textarea",
|
417
|
+
Textarea
|
418
|
+
);
|
419
|
+
var SwitchField = registerFieldType(
|
420
|
+
"switch",
|
421
|
+
forwardRef(({ type, value, ...rest }, ref) => {
|
422
|
+
return /* @__PURE__ */ React8.createElement(Switch, {
|
423
|
+
isChecked: !!value,
|
424
|
+
...rest,
|
425
|
+
ref
|
426
|
+
});
|
427
|
+
}),
|
428
|
+
{
|
429
|
+
isControlled: true
|
430
|
+
}
|
431
|
+
);
|
432
|
+
var SelectField = registerFieldType("select", Select, {
|
433
|
+
isControlled: true
|
434
|
+
});
|
435
|
+
var CheckboxField = registerFieldType(
|
436
|
+
"checkbox",
|
437
|
+
forwardRef(({ label, type, ...props }, ref) => {
|
438
|
+
return /* @__PURE__ */ React8.createElement(Checkbox, {
|
439
|
+
ref,
|
440
|
+
...props
|
441
|
+
}, label);
|
442
|
+
}),
|
443
|
+
{
|
444
|
+
hideLabel: true
|
445
|
+
}
|
446
|
+
);
|
447
|
+
var RadioField = registerFieldType(
|
448
|
+
"radio",
|
449
|
+
RadioInput,
|
450
|
+
{
|
451
|
+
isControlled: true
|
452
|
+
}
|
453
|
+
);
|
454
|
+
var NativeSelectField = registerFieldType(
|
455
|
+
"native-select",
|
456
|
+
NativeSelect,
|
457
|
+
{ isControlled: true }
|
458
|
+
);
|
459
|
+
var PinField = registerFieldType(
|
460
|
+
"pin",
|
461
|
+
forwardRef((props, ref) => {
|
462
|
+
const { pinLength = 4, pinType, spacing, ...inputProps } = props;
|
463
|
+
const inputs = [];
|
464
|
+
for (let i = 0; i < pinLength; i++) {
|
465
|
+
inputs.push(/* @__PURE__ */ React8.createElement(PinInputField, {
|
466
|
+
key: i,
|
467
|
+
ref
|
468
|
+
}));
|
469
|
+
}
|
470
|
+
return /* @__PURE__ */ React8.createElement(HStack, {
|
471
|
+
spacing
|
472
|
+
}, /* @__PURE__ */ React8.createElement(PinInput, {
|
473
|
+
...inputProps,
|
474
|
+
type: pinType
|
475
|
+
}, inputs));
|
476
|
+
}),
|
477
|
+
{
|
478
|
+
isControlled: true
|
479
|
+
}
|
480
|
+
);
|
481
|
+
var mapFields = (schema) => schema && Object.entries(schema).map(([name, { items, label, title, ...field }]) => {
|
482
|
+
return {
|
483
|
+
...field,
|
484
|
+
name,
|
485
|
+
label: label || title || name
|
486
|
+
};
|
487
|
+
});
|
488
|
+
var objectFieldResolver = (schema) => {
|
489
|
+
const getFields = () => {
|
490
|
+
return mapFields(schema);
|
491
|
+
};
|
492
|
+
const getNestedFields = (name) => {
|
493
|
+
var _a;
|
494
|
+
const field = get$1(schema, name);
|
495
|
+
if (!field)
|
496
|
+
return [];
|
497
|
+
if (((_a = field.items) == null ? void 0 : _a.type) === "object") {
|
498
|
+
return mapFields(field.items.properties);
|
499
|
+
} else if (field.type === "object") {
|
500
|
+
return mapFields(field.properties);
|
501
|
+
}
|
502
|
+
return [field.items];
|
503
|
+
};
|
504
|
+
return { getFields, getNestedFields };
|
505
|
+
};
|
506
|
+
|
507
|
+
// src/form.tsx
|
508
|
+
var Form = forwardRef(
|
509
|
+
(props, ref) => {
|
510
|
+
var _a;
|
511
|
+
const {
|
512
|
+
mode = "all",
|
513
|
+
resolver,
|
514
|
+
reValidateMode,
|
515
|
+
shouldFocusError,
|
516
|
+
shouldUnregister,
|
517
|
+
shouldUseNativeValidation,
|
518
|
+
criteriaMode,
|
519
|
+
delayError,
|
520
|
+
schema,
|
521
|
+
defaultValues,
|
522
|
+
values,
|
523
|
+
context,
|
524
|
+
resetOptions,
|
525
|
+
onChange,
|
526
|
+
onSubmit,
|
527
|
+
onError,
|
528
|
+
formRef,
|
529
|
+
children,
|
530
|
+
...rest
|
531
|
+
} = props;
|
532
|
+
const form = {
|
533
|
+
mode,
|
534
|
+
resolver,
|
535
|
+
defaultValues,
|
536
|
+
values,
|
537
|
+
reValidateMode,
|
538
|
+
shouldFocusError,
|
539
|
+
shouldUnregister,
|
540
|
+
shouldUseNativeValidation,
|
541
|
+
criteriaMode,
|
542
|
+
delayError,
|
543
|
+
context,
|
544
|
+
resetOptions
|
545
|
+
};
|
546
|
+
if (schema && !resolver) {
|
547
|
+
form.resolver = (_a = Form.getResolver) == null ? void 0 : _a.call(Form, schema);
|
548
|
+
}
|
549
|
+
const methods = useForm(form);
|
550
|
+
const { handleSubmit } = methods;
|
551
|
+
React8.useImperativeHandle(formRef, () => methods, [formRef, methods]);
|
552
|
+
React8.useEffect(() => {
|
553
|
+
let subscription;
|
554
|
+
if (onChange) {
|
555
|
+
subscription = methods.watch(onChange);
|
556
|
+
}
|
557
|
+
return () => subscription == null ? void 0 : subscription.unsubscribe();
|
558
|
+
}, [methods, onChange]);
|
559
|
+
const Field2 = React8.useMemo(
|
560
|
+
() => (props2) => /* @__PURE__ */ React8.createElement(Field, {
|
561
|
+
...props2
|
562
|
+
}),
|
563
|
+
[]
|
564
|
+
);
|
565
|
+
return /* @__PURE__ */ React8.createElement(FormProvider, {
|
566
|
+
...methods
|
567
|
+
}, /* @__PURE__ */ React8.createElement(chakra.form, {
|
568
|
+
ref,
|
569
|
+
onSubmit: handleSubmit(onSubmit, onError),
|
570
|
+
...rest,
|
571
|
+
className: cx("sui-form", props.className)
|
572
|
+
}, runIfFn(children, {
|
573
|
+
Field: Field2,
|
574
|
+
...methods
|
575
|
+
})));
|
576
|
+
}
|
577
|
+
);
|
578
|
+
Form.getFieldResolver = objectFieldResolver;
|
579
|
+
Form.displayName = "Form";
|
580
|
+
function createForm({ resolver }) {
|
581
|
+
const CreateForm = (props) => {
|
582
|
+
const { schema, ...rest } = props;
|
583
|
+
return /* @__PURE__ */ React8.createElement(Form, {
|
584
|
+
resolver: resolver == null ? void 0 : resolver(props.schema),
|
585
|
+
...rest
|
586
|
+
});
|
587
|
+
};
|
588
|
+
return CreateForm;
|
589
|
+
}
|
590
|
+
var FormLayoutItem = ({ children }) => {
|
591
|
+
return /* @__PURE__ */ React8.createElement(chakra.div, null, children);
|
592
|
+
};
|
593
|
+
if (__DEV__) {
|
594
|
+
FormLayoutItem.displayName = "FormLayoutItem";
|
595
|
+
}
|
596
|
+
var FormLayout = ({ children, ...props }) => {
|
597
|
+
var _a, _b, _c;
|
598
|
+
const theme = useTheme();
|
599
|
+
const defaultProps = (_c = (_b = (_a = theme.components) == null ? void 0 : _a.FormLayout) == null ? void 0 : _b.defaultProps) != null ? _c : {
|
600
|
+
spacing: 4
|
601
|
+
};
|
602
|
+
const gridProps = {
|
603
|
+
...defaultProps,
|
604
|
+
...props
|
605
|
+
};
|
606
|
+
return /* @__PURE__ */ React8.createElement(SimpleGrid, {
|
607
|
+
...gridProps,
|
608
|
+
className: cx("sui-form__layout", props.className)
|
609
|
+
}, React8.Children.map(children, (child) => {
|
610
|
+
if (React8.isValidElement(child)) {
|
611
|
+
return /* @__PURE__ */ React8.createElement(FormLayoutItem, null, child);
|
612
|
+
}
|
613
|
+
return child;
|
614
|
+
}));
|
615
|
+
};
|
616
|
+
if (__DEV__) {
|
617
|
+
FormLayout.displayName = "FormLayout";
|
618
|
+
}
|
619
|
+
var mapNestedFields = (name, children) => {
|
620
|
+
return React8.Children.map(children, (child) => {
|
621
|
+
if (React8.isValidElement(child) && child.props.name) {
|
622
|
+
return React8.cloneElement(child, {
|
623
|
+
...child.props,
|
624
|
+
name: `${name}.${child.props.name}`
|
625
|
+
});
|
626
|
+
}
|
627
|
+
return child;
|
628
|
+
});
|
629
|
+
};
|
630
|
+
var [ArrayFieldProvider, useArrayFieldContext] = createContext({
|
631
|
+
name: "ArrayFieldContext"
|
632
|
+
});
|
633
|
+
var [ArrayFieldRowProvider, useArrayFieldRowContext] = createContext({
|
634
|
+
name: "ArrayFieldRowContext"
|
635
|
+
});
|
636
|
+
var useArrayField = ({
|
637
|
+
name,
|
638
|
+
defaultValue = {},
|
639
|
+
keyName,
|
640
|
+
min,
|
641
|
+
max
|
642
|
+
}) => {
|
643
|
+
const { control } = useFormContext();
|
644
|
+
const context = useFieldArray({
|
645
|
+
control,
|
646
|
+
name,
|
647
|
+
keyName
|
648
|
+
});
|
649
|
+
return {
|
650
|
+
...context,
|
651
|
+
name,
|
652
|
+
defaultValue,
|
653
|
+
min,
|
654
|
+
max
|
655
|
+
};
|
656
|
+
};
|
657
|
+
var useArrayFieldRow = ({ index }) => {
|
658
|
+
const { clearErrors } = useFormContext();
|
659
|
+
const { name, remove, fields } = useArrayFieldContext();
|
660
|
+
React8.useEffect(() => {
|
661
|
+
clearErrors(name);
|
662
|
+
}, []);
|
663
|
+
return {
|
664
|
+
index,
|
665
|
+
isFirst: index === 0,
|
666
|
+
isLast: index === fields.length - 1,
|
667
|
+
name: `${name}.${index}`,
|
668
|
+
remove: React8.useCallback(() => {
|
669
|
+
clearErrors(name);
|
670
|
+
remove(index);
|
671
|
+
}, [index])
|
672
|
+
};
|
673
|
+
};
|
674
|
+
var useArrayFieldRemoveButton = () => {
|
675
|
+
const { isFirst, remove } = useArrayFieldRowContext();
|
676
|
+
const { min, fields } = useArrayFieldContext();
|
677
|
+
const isDisabled = isFirst && !!(min && fields.length <= min);
|
678
|
+
return {
|
679
|
+
onClick: () => remove(),
|
680
|
+
isDisabled
|
681
|
+
};
|
682
|
+
};
|
683
|
+
var useArrayFieldAddButton = () => {
|
684
|
+
const { append, defaultValue, max, fields } = useArrayFieldContext();
|
685
|
+
const isDisabled = !!(max && fields.length >= max);
|
686
|
+
return {
|
687
|
+
onClick: () => append(defaultValue, {
|
688
|
+
shouldFocus: false
|
689
|
+
}),
|
690
|
+
isDisabled
|
691
|
+
};
|
692
|
+
};
|
693
|
+
|
694
|
+
// src/array-field.tsx
|
695
|
+
var ArrayFieldRow = ({
|
696
|
+
children,
|
697
|
+
index,
|
698
|
+
...rowFieldsProps
|
699
|
+
}) => {
|
700
|
+
return /* @__PURE__ */ React8.createElement(ArrayFieldRowContainer, {
|
701
|
+
index
|
702
|
+
}, /* @__PURE__ */ React8.createElement(ArrayFieldRowFields, {
|
703
|
+
...rowFieldsProps
|
704
|
+
}, children), /* @__PURE__ */ React8.createElement(ArrayFieldRemoveButton, null));
|
705
|
+
};
|
706
|
+
if (__DEV__) {
|
707
|
+
ArrayFieldRow.displayName = "ArrayFieldRow";
|
708
|
+
}
|
709
|
+
var ArrayFieldRowFields = ({
|
710
|
+
children,
|
711
|
+
...layoutProps
|
712
|
+
}) => {
|
713
|
+
const { name } = useArrayFieldRowContext();
|
714
|
+
return /* @__PURE__ */ React8.createElement(FormLayout, {
|
715
|
+
flex: "1",
|
716
|
+
mr: "2",
|
717
|
+
...layoutProps
|
718
|
+
}, mapNestedFields(name, children));
|
719
|
+
};
|
720
|
+
if (__DEV__) {
|
721
|
+
ArrayFieldRowFields.displayName = "ArrayFieldRowFields";
|
722
|
+
}
|
723
|
+
var ArrayFieldRowContainer = ({
|
724
|
+
children,
|
725
|
+
index
|
726
|
+
}) => {
|
727
|
+
const context = useArrayFieldRow({ index });
|
728
|
+
const styles = {
|
729
|
+
display: "flex",
|
730
|
+
flexDirection: "row",
|
731
|
+
alignItems: "flex-end",
|
732
|
+
width: "100%",
|
733
|
+
mb: 4
|
734
|
+
};
|
735
|
+
return /* @__PURE__ */ React8.createElement(ArrayFieldRowProvider, {
|
736
|
+
value: context
|
737
|
+
}, /* @__PURE__ */ React8.createElement(chakra.div, {
|
738
|
+
__css: styles
|
739
|
+
}, children));
|
740
|
+
};
|
741
|
+
if (__DEV__) {
|
742
|
+
ArrayFieldRowContainer.displayName = "ArrayFieldRowContainer";
|
743
|
+
}
|
744
|
+
var ArrayFieldRemoveButton = (props) => {
|
745
|
+
return /* @__PURE__ */ React8.createElement(Button, {
|
746
|
+
"aria-label": "Remove row",
|
747
|
+
...useArrayFieldRemoveButton(),
|
748
|
+
...props
|
749
|
+
}, props.children || /* @__PURE__ */ React8.createElement(MinusIcon, null));
|
750
|
+
};
|
751
|
+
if (__DEV__) {
|
752
|
+
ArrayFieldRemoveButton.displayName = "ArrayFieldRemoveButton";
|
753
|
+
}
|
754
|
+
var ArrayFieldAddButton = (props) => {
|
755
|
+
return /* @__PURE__ */ React8.createElement(Button, {
|
756
|
+
"aria-label": "Add row",
|
757
|
+
float: "right",
|
758
|
+
...useArrayFieldAddButton(),
|
759
|
+
...props
|
760
|
+
}, props.children || /* @__PURE__ */ React8.createElement(AddIcon, null));
|
761
|
+
};
|
762
|
+
if (__DEV__) {
|
763
|
+
ArrayFieldAddButton.displayName = "ArrayFieldAddButton";
|
764
|
+
}
|
765
|
+
var ArrayField = forwardRef(
|
766
|
+
(props, ref) => {
|
767
|
+
const { children, ...containerProps } = props;
|
768
|
+
return /* @__PURE__ */ React8.createElement(ArrayFieldContainer, {
|
769
|
+
ref,
|
770
|
+
...containerProps
|
771
|
+
}, /* @__PURE__ */ React8.createElement(ArrayFieldRows, null, (fields) => /* @__PURE__ */ React8.createElement(React8.Fragment, null, fields.map(({ id }, index) => /* @__PURE__ */ React8.createElement(ArrayFieldRow, {
|
772
|
+
key: id,
|
773
|
+
index
|
774
|
+
}, children)))), /* @__PURE__ */ React8.createElement(ArrayFieldAddButton, null));
|
775
|
+
}
|
776
|
+
);
|
777
|
+
if (__DEV__) {
|
778
|
+
ArrayField.displayName = "ArrayField";
|
779
|
+
}
|
780
|
+
var ArrayFieldRows = ({
|
781
|
+
children
|
782
|
+
}) => {
|
783
|
+
const { fields } = useArrayFieldContext();
|
784
|
+
return children(fields);
|
785
|
+
};
|
786
|
+
if (__DEV__) {
|
787
|
+
ArrayFieldRows.displayName = "ArrayFieldRows";
|
788
|
+
}
|
789
|
+
var ArrayFieldContainer = React8.forwardRef(
|
790
|
+
({
|
791
|
+
name,
|
792
|
+
defaultValue,
|
793
|
+
keyName,
|
794
|
+
min,
|
795
|
+
max,
|
796
|
+
children,
|
797
|
+
...fieldProps
|
798
|
+
}, ref) => {
|
799
|
+
const context = useArrayField({
|
800
|
+
name,
|
801
|
+
defaultValue,
|
802
|
+
keyName,
|
803
|
+
min,
|
804
|
+
max
|
805
|
+
});
|
806
|
+
React8.useImperativeHandle(ref, () => context, [ref, context]);
|
807
|
+
return /* @__PURE__ */ React8.createElement(ArrayFieldProvider, {
|
808
|
+
value: context
|
809
|
+
}, /* @__PURE__ */ React8.createElement(BaseField, {
|
810
|
+
name,
|
811
|
+
...fieldProps
|
812
|
+
}, children));
|
813
|
+
}
|
814
|
+
);
|
815
|
+
if (__DEV__) {
|
816
|
+
ArrayFieldContainer.displayName = "ArrayFieldContainer";
|
817
|
+
}
|
818
|
+
var FormLegend = (props) => {
|
819
|
+
const styles = useStyleConfig("SuiFormLegend");
|
820
|
+
return /* @__PURE__ */ React8.createElement(FormLabel, {
|
821
|
+
as: "legend",
|
822
|
+
sx: styles,
|
823
|
+
...props
|
824
|
+
});
|
825
|
+
};
|
826
|
+
var ObjectField = (props) => {
|
827
|
+
const { name, label, hideLabel, children, columns, spacing, ...fieldProps } = props;
|
828
|
+
return /* @__PURE__ */ React8.createElement(FormControl, {
|
829
|
+
name,
|
830
|
+
as: "fieldset",
|
831
|
+
...fieldProps
|
832
|
+
}, /* @__PURE__ */ React8.createElement(FormLegend, {
|
833
|
+
display: hideLabel ? "none" : "block"
|
834
|
+
}, label), /* @__PURE__ */ React8.createElement(FormLayout, {
|
835
|
+
columns,
|
836
|
+
gridGap: spacing
|
837
|
+
}, mapNestedFields(name, children)));
|
838
|
+
};
|
839
|
+
if (__DEV__) {
|
840
|
+
ObjectField.displayName = "ObjectField";
|
841
|
+
}
|
842
|
+
var mapNestedFields2 = (resolver, name) => {
|
843
|
+
var _a;
|
844
|
+
return (_a = resolver.getNestedFields(name)) == null ? void 0 : _a.map(
|
845
|
+
({ name: name2, type, ...nestedFieldProps }, i) => /* @__PURE__ */ React8.createElement(Field, {
|
846
|
+
key: name2 || i,
|
847
|
+
name: name2,
|
848
|
+
type,
|
849
|
+
...nestedFieldProps
|
850
|
+
})
|
851
|
+
);
|
852
|
+
};
|
853
|
+
var Fields = ({
|
854
|
+
schema,
|
855
|
+
fieldResolver,
|
856
|
+
focusFirstField,
|
857
|
+
...props
|
858
|
+
}) => {
|
859
|
+
const resolver = React8.useMemo(
|
860
|
+
() => fieldResolver || Form.getFieldResolver(schema),
|
861
|
+
[schema, fieldResolver]
|
862
|
+
);
|
863
|
+
const fields = React8.useMemo(() => resolver.getFields(), [resolver]);
|
864
|
+
const form = useFormContext();
|
865
|
+
React8.useEffect(() => {
|
866
|
+
var _a;
|
867
|
+
if (focusFirstField && ((_a = fields[0]) == null ? void 0 : _a.name)) {
|
868
|
+
form.setFocus(fields[0].name);
|
869
|
+
}
|
870
|
+
}, [schema, fieldResolver, focusFirstField]);
|
871
|
+
return /* @__PURE__ */ React8.createElement(FormLayout, {
|
872
|
+
...props
|
873
|
+
}, fields.map(
|
874
|
+
({
|
875
|
+
name,
|
876
|
+
type,
|
877
|
+
defaultValue,
|
878
|
+
...fieldProps
|
879
|
+
}) => {
|
880
|
+
if (type === "array") {
|
881
|
+
return /* @__PURE__ */ React8.createElement(ArrayField, {
|
882
|
+
key: name,
|
883
|
+
name,
|
884
|
+
...fieldProps
|
885
|
+
}, mapNestedFields2(resolver, name));
|
886
|
+
} else if (type === "object") {
|
887
|
+
return /* @__PURE__ */ React8.createElement(ObjectField, {
|
888
|
+
key: name,
|
889
|
+
name,
|
890
|
+
...fieldProps
|
891
|
+
}, mapNestedFields2(resolver, name));
|
892
|
+
}
|
893
|
+
return /* @__PURE__ */ React8.createElement(Field, {
|
894
|
+
key: name,
|
895
|
+
name,
|
896
|
+
type,
|
897
|
+
...fieldProps
|
898
|
+
});
|
899
|
+
}
|
900
|
+
));
|
901
|
+
};
|
902
|
+
Fields.displayName = "Fields";
|
903
|
+
var SubmitButton = forwardRef(
|
904
|
+
(props, ref) => {
|
905
|
+
const {
|
906
|
+
children = "Submit",
|
907
|
+
disableIfUntouched,
|
908
|
+
disableIfInvalid,
|
909
|
+
isDisabled: isDisabledProp,
|
910
|
+
isLoading,
|
911
|
+
...rest
|
912
|
+
} = props;
|
913
|
+
const { formState } = useFormContext();
|
914
|
+
const isDisabled = disableIfUntouched && !formState.isDirty || disableIfInvalid && !formState.isValid || isDisabledProp;
|
915
|
+
return /* @__PURE__ */ React8.createElement(Button, {
|
916
|
+
...rest,
|
917
|
+
ref,
|
918
|
+
type: "submit",
|
919
|
+
isLoading: formState.isSubmitting || isLoading,
|
920
|
+
isDisabled
|
921
|
+
}, children);
|
922
|
+
}
|
923
|
+
);
|
924
|
+
SubmitButton.defaultProps = {
|
925
|
+
variant: "primary",
|
926
|
+
disableIfUntouched: false,
|
927
|
+
disableIfInvalid: false
|
928
|
+
};
|
929
|
+
SubmitButton.displayName = "SubmitButton";
|
930
|
+
|
931
|
+
// src/auto-form.tsx
|
932
|
+
var AutoForm = forwardRef(
|
933
|
+
(props, ref) => {
|
934
|
+
const {
|
935
|
+
schema,
|
936
|
+
submitLabel = "Submit",
|
937
|
+
fieldResolver,
|
938
|
+
children,
|
939
|
+
...rest
|
940
|
+
} = props;
|
941
|
+
return /* @__PURE__ */ React8.createElement(Form, {
|
942
|
+
...rest,
|
943
|
+
schema,
|
944
|
+
ref
|
945
|
+
}, /* @__PURE__ */ React8.createElement(FormLayout, null, /* @__PURE__ */ React8.createElement(Fields, {
|
946
|
+
schema,
|
947
|
+
fieldResolver
|
948
|
+
}), submitLabel && /* @__PURE__ */ React8.createElement(SubmitButton, null, submitLabel), children));
|
949
|
+
}
|
950
|
+
);
|
951
|
+
AutoForm.displayName = "AutoForm";
|
952
|
+
var DisplayIf = ({
|
953
|
+
children,
|
954
|
+
name,
|
955
|
+
defaultValue,
|
956
|
+
isDisabled,
|
957
|
+
isExact,
|
958
|
+
condition = (value) => !!value
|
959
|
+
}) => {
|
960
|
+
const value = useWatch({
|
961
|
+
name,
|
962
|
+
defaultValue,
|
963
|
+
disabled: isDisabled,
|
964
|
+
exact: isExact
|
965
|
+
});
|
966
|
+
const context = useFormContext();
|
967
|
+
return condition(value, context) ? children : null;
|
968
|
+
};
|
969
|
+
if (__DEV__) {
|
970
|
+
DisplayIf.displayName = "DisplayIf";
|
971
|
+
}
|
972
|
+
var [StepFormProvider, useStepFormContext] = createContext({
|
973
|
+
name: "StepFormContext",
|
974
|
+
errorMessage: "useStepFormContext: `context` is undefined. Seems you forgot to wrap step form components in `<StepForm />`"
|
975
|
+
});
|
976
|
+
function useStepForm(props) {
|
977
|
+
const { onChange, ...rest } = props;
|
978
|
+
const stepper = useStepper(rest);
|
979
|
+
const { activeStep, isLastStep, nextStep } = stepper;
|
980
|
+
const [steps, updateSteps] = React8.useState({});
|
981
|
+
const onSubmitStep = React8.useCallback(
|
982
|
+
async (data) => {
|
983
|
+
var _a, _b;
|
984
|
+
try {
|
985
|
+
const step = steps[activeStep];
|
986
|
+
if (isLastStep) {
|
987
|
+
await ((_a = props.onSubmit) == null ? void 0 : _a.call(props, data));
|
988
|
+
updateStep({
|
989
|
+
...step,
|
990
|
+
isCompleted: true
|
991
|
+
});
|
992
|
+
nextStep();
|
993
|
+
return;
|
994
|
+
}
|
995
|
+
await ((_b = step.onSubmit) == null ? void 0 : _b.call(step, data, stepper));
|
996
|
+
nextStep();
|
997
|
+
} catch (e) {
|
998
|
+
}
|
999
|
+
},
|
1000
|
+
[steps, activeStep, isLastStep]
|
1001
|
+
);
|
1002
|
+
const getFormProps = React8.useCallback(() => {
|
1003
|
+
const step = steps[activeStep];
|
1004
|
+
return {
|
1005
|
+
onSubmit: onSubmitStep,
|
1006
|
+
schema: step == null ? void 0 : step.schema,
|
1007
|
+
resolver: step == null ? void 0 : step.resolver
|
1008
|
+
};
|
1009
|
+
}, [steps, onSubmitStep, activeStep]);
|
1010
|
+
const updateStep = React8.useCallback(
|
1011
|
+
(step) => {
|
1012
|
+
updateSteps((steps2) => {
|
1013
|
+
return {
|
1014
|
+
...steps2,
|
1015
|
+
[step.name]: step
|
1016
|
+
};
|
1017
|
+
});
|
1018
|
+
},
|
1019
|
+
[steps]
|
1020
|
+
);
|
1021
|
+
return {
|
1022
|
+
getFormProps,
|
1023
|
+
updateStep,
|
1024
|
+
steps,
|
1025
|
+
...stepper
|
1026
|
+
};
|
1027
|
+
}
|
1028
|
+
function useFormStep(props) {
|
1029
|
+
const { name, schema, resolver, onSubmit } = props;
|
1030
|
+
const step = useStep({ name });
|
1031
|
+
const { steps, updateStep } = useStepFormContext();
|
1032
|
+
React8.useEffect(() => {
|
1033
|
+
updateStep({ name, schema, resolver, onSubmit });
|
1034
|
+
}, [name, schema]);
|
1035
|
+
return {
|
1036
|
+
...step,
|
1037
|
+
...steps[name] || { name, schema }
|
1038
|
+
};
|
1039
|
+
}
|
1040
|
+
|
1041
|
+
// src/step-form.tsx
|
1042
|
+
var StepForm = React8.forwardRef(
|
1043
|
+
(props, ref) => {
|
1044
|
+
const { children, ...rest } = props;
|
1045
|
+
const stepper = useStepForm(props);
|
1046
|
+
const { getFormProps, ...ctx } = stepper;
|
1047
|
+
const context = React8.useMemo(() => ctx, [ctx]);
|
1048
|
+
return /* @__PURE__ */ React8.createElement(StepperProvider, {
|
1049
|
+
value: context
|
1050
|
+
}, /* @__PURE__ */ React8.createElement(StepFormProvider, {
|
1051
|
+
value: context
|
1052
|
+
}, /* @__PURE__ */ React8.createElement(Form, {
|
1053
|
+
ref,
|
1054
|
+
...rest,
|
1055
|
+
...getFormProps()
|
1056
|
+
}, runIfFn(children, stepper))));
|
1057
|
+
}
|
1058
|
+
);
|
1059
|
+
var FormStepper = (props) => {
|
1060
|
+
const { activeIndex, setIndex } = useStepperContext();
|
1061
|
+
const { children, orientation, variant, colorScheme, size, ...rest } = props;
|
1062
|
+
const elements = React8.Children.map(children, (child) => {
|
1063
|
+
if (React8.isValidElement(child) && (child == null ? void 0 : child.type) === FormStep) {
|
1064
|
+
const { isCompleted } = useFormStep(child.props);
|
1065
|
+
return /* @__PURE__ */ React8.createElement(StepperStep, {
|
1066
|
+
name: child.props.name,
|
1067
|
+
title: child.props.title,
|
1068
|
+
isCompleted,
|
1069
|
+
...rest
|
1070
|
+
}, child.props.children);
|
1071
|
+
}
|
1072
|
+
return child;
|
1073
|
+
});
|
1074
|
+
const onChange = React8.useCallback((i) => {
|
1075
|
+
setIndex(i);
|
1076
|
+
}, []);
|
1077
|
+
return /* @__PURE__ */ React8.createElement(StepperContainer, {
|
1078
|
+
orientation,
|
1079
|
+
step: activeIndex,
|
1080
|
+
variant,
|
1081
|
+
colorScheme,
|
1082
|
+
size,
|
1083
|
+
onChange
|
1084
|
+
}, /* @__PURE__ */ React8.createElement(StepperSteps, {
|
1085
|
+
mb: "4",
|
1086
|
+
...props
|
1087
|
+
}, elements));
|
1088
|
+
};
|
1089
|
+
var FormStep = (props) => {
|
1090
|
+
const { name, schema, resolver, children, className, onSubmit, ...rest } = props;
|
1091
|
+
const step = useFormStep({ name, schema, resolver, onSubmit });
|
1092
|
+
const { isActive } = step;
|
1093
|
+
return isActive ? /* @__PURE__ */ React8.createElement(chakra.div, {
|
1094
|
+
...rest,
|
1095
|
+
className: cx("sui-form__step", className)
|
1096
|
+
}, children) : null;
|
1097
|
+
};
|
1098
|
+
if (__DEV__) {
|
1099
|
+
FormStep.displayName = "FormStep";
|
1100
|
+
}
|
1101
|
+
var PrevButton = (props) => {
|
1102
|
+
const { isFirstStep, isCompleted, prevStep } = useStepperContext();
|
1103
|
+
return /* @__PURE__ */ React8.createElement(Button, {
|
1104
|
+
isDisabled: isFirstStep || isCompleted,
|
1105
|
+
label: "Back",
|
1106
|
+
...props,
|
1107
|
+
className: cx("sui-form__prev-button", props.className),
|
1108
|
+
onClick: callAllHandlers(props.onClick, prevStep)
|
1109
|
+
});
|
1110
|
+
};
|
1111
|
+
if (__DEV__) {
|
1112
|
+
PrevButton.displayName = "PrevButton";
|
1113
|
+
}
|
1114
|
+
var NextButton = (props) => {
|
1115
|
+
const { label = "Next", submitLabel = "Complete", ...rest } = props;
|
1116
|
+
const { isLastStep, isCompleted } = useStepperContext();
|
1117
|
+
return /* @__PURE__ */ React8.createElement(SubmitButton, {
|
1118
|
+
...rest,
|
1119
|
+
isDisabled: isCompleted,
|
1120
|
+
className: cx("sui-form__next-button", props.className)
|
1121
|
+
}, isLastStep || isCompleted ? submitLabel : label);
|
1122
|
+
};
|
1123
|
+
if (__DEV__) {
|
1124
|
+
NextButton.displayName = "NextButton";
|
1125
|
+
}
|
1126
|
+
var WatchField = (props) => {
|
1127
|
+
const { name, defaultValue, isDisabled, isExact } = props;
|
1128
|
+
const form = useFormContext();
|
1129
|
+
const field = useWatch({
|
1130
|
+
name,
|
1131
|
+
defaultValue,
|
1132
|
+
disabled: isDisabled,
|
1133
|
+
exact: isExact
|
1134
|
+
});
|
1135
|
+
return props.children(field, form) || null;
|
1136
|
+
};
|
1137
|
+
|
1138
|
+
export { ArrayField, ArrayFieldAddButton, ArrayFieldContainer, ArrayFieldProvider, ArrayFieldRemoveButton, ArrayFieldRow, ArrayFieldRowContainer, ArrayFieldRowFields, ArrayFieldRowProvider, ArrayFieldRows, AutoForm, BaseField, CheckboxField, DisplayField, DisplayIf, Field, Fields, Form, FormLayout, FormLegend, FormStep, FormStepper, FormValue, InputField, InputRightButton, NativeSelect, NativeSelectField, NextButton, NumberInputField2 as NumberInputField, ObjectField, PasswordInput, PasswordInputField, PinField, PrevButton, RadioField, Select, SelectField, StepForm, StepFormProvider, SubmitButton, SwitchField, TextareaField, WatchField, createForm, objectFieldResolver, registerFieldType, useArrayField, useArrayFieldAddButton, useArrayFieldContext, useArrayFieldRemoveButton, useArrayFieldRow, useArrayFieldRowContext, useFormStep, useStepForm, useStepFormContext, withControlledInput, withUncontrolledInput };
|
1139
|
+
//# sourceMappingURL=out.js.map
|
1140
|
+
//# sourceMappingURL=index.mjs.map
|