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