@saas-ui/forms 2.0.0-next.3 → 2.0.0-next.6
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +43 -0
- package/README.md +53 -6
- package/dist/ajv/index.d.ts +358 -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 +448 -247
- package/dist/index.js +707 -682
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +691 -666
- package/dist/index.mjs.map +1 -1
- package/dist/yup/index.d.ts +580 -21
- package/dist/yup/index.js +6 -10
- package/dist/yup/index.js.map +1 -1
- package/dist/yup/index.mjs +4 -8
- package/dist/yup/index.mjs.map +1 -1
- package/dist/zod/index.d.ts +580 -11
- package/dist/zod/index.js +5 -0
- package/dist/zod/index.js.map +1 -1
- package/dist/zod/index.mjs +5 -1
- package/dist/zod/index.mjs.map +1 -1
- package/package.json +19 -10
- package/src/array-field.tsx +82 -45
- 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 +54 -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 +69 -52
- package/src/index.ts +44 -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 +13 -8
- 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 +160 -146
- package/src/step-form.tsx +29 -11
- package/src/submit-button.tsx +5 -1
- package/src/types.ts +144 -0
- package/src/use-array-field.tsx +9 -3
- 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 {
|
8
|
-
import {
|
9
|
-
import {
|
1
|
+
import * as React11 from 'react';
|
2
|
+
import React11__default, { createContext, useState, useContext } 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 { StepperProvider, ChevronUpIcon, ChevronDownIcon, useStepper, useStep, useStepperContext, StepperStep, StepperContainer, StepperSteps } 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';
|
10
|
+
import { createContext as createContext$1 } from '@chakra-ui/react-utils';
|
10
11
|
|
11
|
-
// src/
|
12
|
+
// src/form-context.tsx
|
13
|
+
var FormContext = createContext(null);
|
14
|
+
var useFormContext = () => {
|
15
|
+
const context = 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,259 @@ 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 React11.Children.map(children, (child) => {
|
131
|
+
if (React11.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 React11.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$1({
|
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 = React11__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
204
|
};
|
192
|
-
const
|
193
|
-
isDisabled,
|
194
|
-
leftIcon,
|
195
|
-
rightIcon,
|
196
|
-
size,
|
197
|
-
variant
|
198
|
-
};
|
199
|
-
const getDisplayValue = React8.useCallback(
|
205
|
+
const getDisplayValue = React11__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 = React11__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 {
|
249
|
+
isInvalid,
|
250
|
+
isReadOnly,
|
251
|
+
isDisabled,
|
252
|
+
isFocused,
|
253
|
+
isRequired,
|
254
|
+
id,
|
255
|
+
onBlur,
|
256
|
+
onFocus
|
257
|
+
} = useFormControlContext();
|
258
|
+
const { rightIcon = /* @__PURE__ */ jsx(ChevronDownIcon, {}), ...rest } = props;
|
259
|
+
const focusStyles = (_a = styles.field) == null ? void 0 : _a._focusVisible;
|
260
|
+
const readOnlyStyles = (_b = styles.field) == null ? void 0 : _b._readOnly;
|
261
|
+
const invalid = (_c = styles.field) == null ? void 0 : _c._invalid;
|
262
|
+
const height = ((_d = styles.field) == null ? void 0 : _d.h) || ((_e = styles.field) == null ? void 0 : _e.height);
|
263
|
+
const buttonStyles = {
|
264
|
+
fontWeight: "normal",
|
265
|
+
textAlign: "left",
|
266
|
+
color: "inherit",
|
267
|
+
_active: {
|
268
|
+
bg: "transparent"
|
269
|
+
},
|
270
|
+
minH: height,
|
271
|
+
_focus: focusStyles,
|
272
|
+
_expanded: focusStyles,
|
273
|
+
_readOnly: readOnlyStyles,
|
274
|
+
_invalid: invalid,
|
275
|
+
...styles.field,
|
276
|
+
h: "auto"
|
277
|
+
};
|
278
|
+
return /* @__PURE__ */ jsx(
|
279
|
+
MenuButton,
|
280
|
+
{
|
281
|
+
as: Button,
|
282
|
+
id,
|
283
|
+
...rest,
|
284
|
+
onFocus,
|
285
|
+
onBlur,
|
286
|
+
isDisabled: isDisabled || isSelectDisabled,
|
287
|
+
"data-invalid": dataAttr(isInvalid),
|
288
|
+
"data-read-only": dataAttr(isReadOnly),
|
289
|
+
"data-focus": dataAttr(isFocused),
|
290
|
+
"data-required": dataAttr(isRequired),
|
291
|
+
rightIcon,
|
292
|
+
ref,
|
293
|
+
sx: buttonStyles,
|
294
|
+
children: renderValue(displayValue) || placeholder
|
295
|
+
}
|
296
|
+
);
|
297
|
+
}
|
298
|
+
);
|
299
|
+
SelectButton.displayName = "SelectButton";
|
300
|
+
var Select = forwardRef((props, ref) => {
|
301
|
+
const { name, children, isDisabled, multiple, ...rest } = props;
|
302
|
+
const menuProps = omitThemingProps(rest);
|
303
|
+
const context = useSelect(props);
|
304
|
+
const { value, controlProps } = context;
|
305
|
+
return /* @__PURE__ */ jsx(SelectProvider, { value: context, children: /* @__PURE__ */ jsx(Menu, { ...menuProps, closeOnSelect: !multiple, children: /* @__PURE__ */ jsxs(chakra.div, { className: cx("sui-select"), children: [
|
306
|
+
children,
|
307
|
+
/* @__PURE__ */ jsx(
|
308
|
+
chakra.input,
|
309
|
+
{
|
310
|
+
...controlProps,
|
311
|
+
ref,
|
312
|
+
name,
|
313
|
+
type: "hidden",
|
314
|
+
value: value || "",
|
315
|
+
className: "saas-select__input"
|
316
|
+
}
|
317
|
+
)
|
318
|
+
] }) }) });
|
243
319
|
});
|
244
|
-
|
245
|
-
|
246
|
-
|
320
|
+
var SelectList = (props) => {
|
321
|
+
const { defaultValue, value, options, multiple, onChange } = useSelectContext();
|
322
|
+
return /* @__PURE__ */ jsx(MenuList, { maxH: "100vh", overflowY: "auto", ...props, children: /* @__PURE__ */ jsx(
|
323
|
+
MenuOptionGroup,
|
324
|
+
{
|
325
|
+
defaultValue: defaultValue || value,
|
326
|
+
value,
|
327
|
+
onChange,
|
328
|
+
type: multiple ? "checkbox" : "radio",
|
329
|
+
children: options ? options.map(({ value: value2, label, ...rest }, i) => /* @__PURE__ */ jsx(SelectOption, { value: value2, ...rest, children: label || value2 }, i)) : props.children
|
330
|
+
}
|
331
|
+
) });
|
332
|
+
};
|
333
|
+
Select.displayName = "Select";
|
334
|
+
var SelectOption = forwardRef(
|
335
|
+
(props, ref) => {
|
336
|
+
return /* @__PURE__ */ jsx(MenuItemOption, { ref, ...props });
|
337
|
+
}
|
338
|
+
);
|
339
|
+
SelectOption.id = "MenuItemOption";
|
340
|
+
SelectOption.displayName = "SelectOption";
|
247
341
|
var NativeSelect = forwardRef(
|
248
342
|
({ 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
|
-
})));
|
343
|
+
return /* @__PURE__ */ jsx(Select$1, { ref, ...props, children: children || (options == null ? void 0 : options.map(({ value, label }) => {
|
344
|
+
return /* @__PURE__ */ jsx("option", { value, children: label || value }, value);
|
345
|
+
})) });
|
258
346
|
}
|
259
347
|
);
|
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
|
-
};
|
348
|
+
NativeSelect.displayName = "NativeSelect";
|
270
349
|
var getError = (name, formState) => {
|
271
350
|
return get(formState.errors, name);
|
272
351
|
};
|
@@ -274,25 +353,17 @@ var BaseField = (props) => {
|
|
274
353
|
const { name, label, help, hideLabel, children, ...controlProps } = props;
|
275
354
|
const { formState } = useFormContext();
|
276
355
|
const error = getError(name, formState);
|
277
|
-
return /* @__PURE__ */
|
278
|
-
|
279
|
-
|
280
|
-
|
356
|
+
return /* @__PURE__ */ jsxs(FormControl, { ...controlProps, isInvalid: !!error, children: [
|
357
|
+
label && !hideLabel ? /* @__PURE__ */ jsx(FormLabel, { children: label }) : null,
|
358
|
+
/* @__PURE__ */ jsxs(Box, { children: [
|
359
|
+
children,
|
360
|
+
help && !(error == null ? void 0 : error.message) ? /* @__PURE__ */ jsx(FormHelperText, { children: help }) : null,
|
361
|
+
(error == null ? void 0 : error.message) && /* @__PURE__ */ jsx(FormErrorMessage, { children: error == null ? void 0 : error.message })
|
362
|
+
] })
|
363
|
+
] });
|
281
364
|
};
|
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 }) => {
|
365
|
+
BaseField.displayName = "BaseField";
|
366
|
+
var _createField = (InputComponent, { displayName, hideLabel, BaseField: BaseField2 }) => {
|
296
367
|
const Field2 = forwardRef((props, ref) => {
|
297
368
|
const {
|
298
369
|
id,
|
@@ -310,24 +381,31 @@ var createField = (InputComponent, { displayName, hideLabel, BaseField: BaseFiel
|
|
310
381
|
required: isRequired,
|
311
382
|
...rules
|
312
383
|
};
|
313
|
-
return /* @__PURE__ */
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
384
|
+
return /* @__PURE__ */ jsx(
|
385
|
+
BaseField2,
|
386
|
+
{
|
387
|
+
id,
|
388
|
+
name,
|
389
|
+
label,
|
390
|
+
help,
|
391
|
+
hideLabel,
|
392
|
+
isDisabled,
|
393
|
+
isInvalid,
|
394
|
+
isReadOnly,
|
395
|
+
isRequired,
|
396
|
+
children: /* @__PURE__ */ jsx(
|
397
|
+
InputComponent,
|
398
|
+
{
|
399
|
+
ref,
|
400
|
+
id,
|
401
|
+
name,
|
402
|
+
label: hideLabel ? label : void 0,
|
403
|
+
rules: inputRules,
|
404
|
+
...inputProps
|
405
|
+
}
|
406
|
+
)
|
407
|
+
}
|
408
|
+
);
|
331
409
|
});
|
332
410
|
Field2.displayName = displayName;
|
333
411
|
return Field2;
|
@@ -336,18 +414,24 @@ var withControlledInput = (InputComponent) => {
|
|
336
414
|
return forwardRef(
|
337
415
|
({ name, rules, ...inputProps }, ref) => {
|
338
416
|
const { control } = useFormContext();
|
339
|
-
return /* @__PURE__ */
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
...
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
417
|
+
return /* @__PURE__ */ jsx(
|
418
|
+
Controller,
|
419
|
+
{
|
420
|
+
name,
|
421
|
+
control,
|
422
|
+
rules,
|
423
|
+
render: ({ field: { ref: _ref, ...field } }) => /* @__PURE__ */ jsx(
|
424
|
+
InputComponent,
|
425
|
+
{
|
426
|
+
...field,
|
427
|
+
...inputProps,
|
428
|
+
onChange: callAllHandlers(inputProps.onChange, field.onChange),
|
429
|
+
onBlur: callAllHandlers(inputProps.onBlur, field.onBlur),
|
430
|
+
ref: useMergeRefs(ref, _ref)
|
431
|
+
}
|
432
|
+
)
|
433
|
+
}
|
434
|
+
);
|
351
435
|
}
|
352
436
|
);
|
353
437
|
};
|
@@ -356,281 +440,171 @@ var withUncontrolledInput = (InputComponent) => {
|
|
356
440
|
({ name, rules, ...inputProps }, ref) => {
|
357
441
|
const { register } = useFormContext();
|
358
442
|
const { ref: _ref, ...field } = register(name, rules);
|
359
|
-
return /* @__PURE__ */
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
443
|
+
return /* @__PURE__ */ jsx(
|
444
|
+
InputComponent,
|
445
|
+
{
|
446
|
+
...field,
|
447
|
+
...inputProps,
|
448
|
+
onChange: callAllHandlers(inputProps.onChange, field.onChange),
|
449
|
+
onBlur: callAllHandlers(inputProps.onBlur, field.onBlur),
|
450
|
+
ref: useMergeRefs(ref, _ref)
|
451
|
+
}
|
452
|
+
);
|
366
453
|
}
|
367
454
|
);
|
368
455
|
};
|
369
|
-
var
|
456
|
+
var createField = (component, options) => {
|
457
|
+
var _a;
|
370
458
|
let InputComponent;
|
371
459
|
if (options == null ? void 0 : options.isControlled) {
|
372
460
|
InputComponent = withControlledInput(component);
|
373
461
|
} else {
|
374
462
|
InputComponent = withUncontrolledInput(component);
|
375
463
|
}
|
376
|
-
const Field2 =
|
377
|
-
displayName: `${
|
464
|
+
const Field2 = _createField(InputComponent, {
|
465
|
+
displayName: `${(_a = component.displayName) != null ? _a : "Custom"}Field`,
|
378
466
|
hideLabel: options == null ? void 0 : options.hideLabel,
|
379
467
|
BaseField: (options == null ? void 0 : options.BaseField) || BaseField
|
380
468
|
});
|
381
|
-
inputTypes[type] = Field2;
|
382
469
|
return Field2;
|
383
470
|
};
|
384
|
-
var InputField =
|
385
|
-
"text",
|
471
|
+
var InputField = createField(
|
386
472
|
forwardRef(({ type = "text", leftAddon, rightAddon, size, ...rest }, ref) => {
|
387
|
-
const input = /* @__PURE__ */
|
388
|
-
type,
|
389
|
-
size,
|
390
|
-
...rest,
|
391
|
-
ref
|
392
|
-
});
|
473
|
+
const input = /* @__PURE__ */ jsx(Input, { type, size, ...rest, ref });
|
393
474
|
if (leftAddon || rightAddon) {
|
394
|
-
return /* @__PURE__ */
|
395
|
-
|
396
|
-
|
475
|
+
return /* @__PURE__ */ jsxs(InputGroup, { size, children: [
|
476
|
+
leftAddon,
|
477
|
+
input,
|
478
|
+
rightAddon
|
479
|
+
] });
|
397
480
|
}
|
398
481
|
return input;
|
399
482
|
})
|
400
483
|
);
|
401
|
-
var NumberInputField2 =
|
402
|
-
"number",
|
484
|
+
var NumberInputField2 = createField(
|
403
485
|
NumberInput,
|
404
486
|
{
|
405
487
|
isControlled: true
|
406
488
|
}
|
407
489
|
);
|
408
|
-
var PasswordInputField =
|
409
|
-
|
410
|
-
forwardRef((props, ref) => /* @__PURE__ */ React8.createElement(PasswordInput, {
|
411
|
-
ref,
|
412
|
-
...props
|
413
|
-
}))
|
414
|
-
);
|
415
|
-
var TextareaField = registerFieldType(
|
416
|
-
"textarea",
|
417
|
-
Textarea
|
490
|
+
var PasswordInputField = createField(
|
491
|
+
forwardRef((props, ref) => /* @__PURE__ */ jsx(PasswordInput, { ref, ...props }))
|
418
492
|
);
|
419
|
-
var
|
420
|
-
|
493
|
+
var TextareaField = createField(Textarea);
|
494
|
+
var SwitchField = createField(
|
421
495
|
forwardRef(({ type, value, ...rest }, ref) => {
|
422
|
-
return /* @__PURE__ */
|
423
|
-
isChecked: !!value,
|
424
|
-
...rest,
|
425
|
-
ref
|
426
|
-
});
|
496
|
+
return /* @__PURE__ */ jsx(Switch, { isChecked: !!value, ...rest, ref });
|
427
497
|
}),
|
428
498
|
{
|
429
499
|
isControlled: true
|
430
500
|
}
|
431
501
|
);
|
432
|
-
var SelectField =
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
return /* @__PURE__ */ React8.createElement(Checkbox, {
|
439
|
-
ref,
|
440
|
-
...props
|
441
|
-
}, label);
|
502
|
+
var SelectField = createField(
|
503
|
+
forwardRef((props, ref) => {
|
504
|
+
return /* @__PURE__ */ jsxs(Select, { ref, ...props, children: [
|
505
|
+
/* @__PURE__ */ jsx(SelectButton, {}),
|
506
|
+
/* @__PURE__ */ jsx(SelectList, {})
|
507
|
+
] });
|
442
508
|
}),
|
443
509
|
{
|
444
|
-
|
510
|
+
isControlled: true
|
445
511
|
}
|
446
512
|
);
|
447
|
-
var
|
448
|
-
|
449
|
-
|
513
|
+
var CheckboxField = createField(
|
514
|
+
forwardRef(({ label, type, ...props }, ref) => {
|
515
|
+
return /* @__PURE__ */ jsx(Checkbox, { ref, ...props, children: label });
|
516
|
+
}),
|
450
517
|
{
|
451
|
-
|
518
|
+
hideLabel: true
|
452
519
|
}
|
453
520
|
);
|
454
|
-
var
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
521
|
+
var RadioField = createField(RadioInput, {
|
522
|
+
isControlled: true
|
523
|
+
});
|
524
|
+
var NativeSelectField = createField(NativeSelect, {
|
525
|
+
isControlled: true
|
526
|
+
});
|
527
|
+
var PinField = createField(
|
461
528
|
forwardRef((props, ref) => {
|
462
529
|
const { pinLength = 4, pinType, spacing, ...inputProps } = props;
|
463
530
|
const inputs = [];
|
464
531
|
for (let i = 0; i < pinLength; i++) {
|
465
|
-
inputs.push(/* @__PURE__ */
|
466
|
-
key: i,
|
467
|
-
ref
|
468
|
-
}));
|
532
|
+
inputs.push(/* @__PURE__ */ jsx(PinInputField, { ref }, i));
|
469
533
|
}
|
470
|
-
return /* @__PURE__ */
|
471
|
-
spacing
|
472
|
-
}, /* @__PURE__ */ React8.createElement(PinInput, {
|
473
|
-
...inputProps,
|
474
|
-
type: pinType
|
475
|
-
}, inputs));
|
534
|
+
return /* @__PURE__ */ jsx(HStack, { spacing, children: /* @__PURE__ */ jsx(PinInput, { ...inputProps, type: pinType, children: inputs }) });
|
476
535
|
}),
|
477
536
|
{
|
478
537
|
isControlled: true
|
479
538
|
}
|
480
539
|
);
|
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 };
|
540
|
+
var defaultFieldTypes = {
|
541
|
+
text: InputField,
|
542
|
+
email: InputField,
|
543
|
+
url: InputField,
|
544
|
+
phone: InputField,
|
545
|
+
number: NumberInputField2,
|
546
|
+
password: PasswordInputField,
|
547
|
+
textarea: TextareaField,
|
548
|
+
switch: SwitchField,
|
549
|
+
select: SelectField,
|
550
|
+
checkbox: CheckboxField,
|
551
|
+
radio: RadioField,
|
552
|
+
pin: PinField,
|
553
|
+
"native-select": NativeSelectField
|
505
554
|
};
|
506
|
-
|
507
|
-
|
508
|
-
|
555
|
+
var FieldsContext = React11__default.createContext(
|
556
|
+
null
|
557
|
+
);
|
558
|
+
var FieldsProvider = (props) => {
|
559
|
+
const fields = { ...defaultFieldTypes, ...props.value };
|
560
|
+
return /* @__PURE__ */ jsx(FieldsContext.Provider, { value: fields, children: props.children });
|
561
|
+
};
|
562
|
+
var useField = (type) => {
|
563
|
+
const context = React11__default.useContext(FieldsContext);
|
564
|
+
return (context == null ? void 0 : context[type]) || InputField;
|
565
|
+
};
|
566
|
+
var defaultInputType = "text";
|
567
|
+
var Field = React11.forwardRef(
|
509
568
|
(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
|
-
})));
|
569
|
+
const { type = defaultInputType, name } = props;
|
570
|
+
const overrides = useFieldProps(name);
|
571
|
+
const InputComponent = useField((overrides == null ? void 0 : overrides.type) || type);
|
572
|
+
return /* @__PURE__ */ jsx(InputComponent, { ref, ...props, ...overrides });
|
576
573
|
}
|
577
574
|
);
|
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
575
|
var FormLayoutItem = ({ children }) => {
|
591
|
-
return /* @__PURE__ */
|
576
|
+
return /* @__PURE__ */ jsx(chakra.div, { children });
|
592
577
|
};
|
593
|
-
|
594
|
-
FormLayoutItem.displayName = "FormLayoutItem";
|
595
|
-
}
|
578
|
+
FormLayoutItem.displayName = "FormLayoutItem";
|
596
579
|
var FormLayout = ({ children, ...props }) => {
|
597
580
|
var _a, _b, _c;
|
598
581
|
const theme = useTheme();
|
599
|
-
const defaultProps = (_c = (_b = (_a = theme.components) == null ? void 0 : _a.
|
582
|
+
const defaultProps = (_c = (_b = (_a = theme.components) == null ? void 0 : _a.SuiFormLayout) == null ? void 0 : _b.defaultProps) != null ? _c : {
|
600
583
|
spacing: 4
|
601
584
|
};
|
602
585
|
const gridProps = {
|
603
586
|
...defaultProps,
|
604
587
|
...props
|
605
588
|
};
|
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
|
-
});
|
589
|
+
return /* @__PURE__ */ jsx(
|
590
|
+
SimpleGrid,
|
591
|
+
{
|
592
|
+
...gridProps,
|
593
|
+
className: cx("sui-form__layout", props.className),
|
594
|
+
children: React11.Children.map(children, (child) => {
|
595
|
+
if (React11.isValidElement(child)) {
|
596
|
+
return /* @__PURE__ */ jsx(FormLayoutItem, { children: child });
|
597
|
+
}
|
598
|
+
return child;
|
599
|
+
})
|
626
600
|
}
|
627
|
-
|
628
|
-
});
|
601
|
+
);
|
629
602
|
};
|
630
|
-
|
603
|
+
FormLayout.displayName = "FormLayout";
|
604
|
+
var [ArrayFieldProvider, useArrayFieldContext] = createContext$1({
|
631
605
|
name: "ArrayFieldContext"
|
632
606
|
});
|
633
|
-
var [ArrayFieldRowProvider, useArrayFieldRowContext] = createContext({
|
607
|
+
var [ArrayFieldRowProvider, useArrayFieldRowContext] = createContext$1({
|
634
608
|
name: "ArrayFieldRowContext"
|
635
609
|
});
|
636
610
|
var useArrayField = ({
|
@@ -657,7 +631,7 @@ var useArrayField = ({
|
|
657
631
|
var useArrayFieldRow = ({ index }) => {
|
658
632
|
const { clearErrors } = useFormContext();
|
659
633
|
const { name, remove, fields } = useArrayFieldContext();
|
660
|
-
|
634
|
+
React11.useEffect(() => {
|
661
635
|
clearErrors(name);
|
662
636
|
}, []);
|
663
637
|
return {
|
@@ -665,7 +639,7 @@ var useArrayFieldRow = ({ index }) => {
|
|
665
639
|
isFirst: index === 0,
|
666
640
|
isLast: index === fields.length - 1,
|
667
641
|
name: `${name}.${index}`,
|
668
|
-
remove:
|
642
|
+
remove: React11.useCallback(() => {
|
669
643
|
clearErrors(name);
|
670
644
|
remove(index);
|
671
645
|
}, [index])
|
@@ -690,39 +664,29 @@ var useArrayFieldAddButton = () => {
|
|
690
664
|
isDisabled
|
691
665
|
};
|
692
666
|
};
|
693
|
-
|
694
|
-
// src/array-field.tsx
|
695
667
|
var ArrayFieldRow = ({
|
696
668
|
children,
|
697
669
|
index,
|
698
670
|
...rowFieldsProps
|
699
671
|
}) => {
|
700
|
-
return /* @__PURE__ */
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
}, children), /* @__PURE__ */ React8.createElement(ArrayFieldRemoveButton, null));
|
672
|
+
return /* @__PURE__ */ jsxs(ArrayFieldRowContainer, { index, children: [
|
673
|
+
/* @__PURE__ */ jsx(ArrayFieldRowFields, { ...rowFieldsProps, children }),
|
674
|
+
/* @__PURE__ */ jsx(ArrayFieldRemoveButton, {})
|
675
|
+
] });
|
705
676
|
};
|
706
|
-
|
707
|
-
ArrayFieldRow.displayName = "ArrayFieldRow";
|
708
|
-
}
|
677
|
+
ArrayFieldRow.displayName = "ArrayFieldRow";
|
709
678
|
var ArrayFieldRowFields = ({
|
710
679
|
children,
|
711
680
|
...layoutProps
|
712
681
|
}) => {
|
713
682
|
const { name } = useArrayFieldRowContext();
|
714
|
-
return /* @__PURE__ */
|
715
|
-
flex: "1",
|
716
|
-
mr: "2",
|
717
|
-
...layoutProps
|
718
|
-
}, mapNestedFields(name, children));
|
683
|
+
return /* @__PURE__ */ jsx(FormLayout, { flex: "1", mr: "2", ...layoutProps, children: mapNestedFields(name, children) });
|
719
684
|
};
|
720
|
-
|
721
|
-
ArrayFieldRowFields.displayName = "ArrayFieldRowFields";
|
722
|
-
}
|
685
|
+
ArrayFieldRowFields.displayName = "ArrayFieldRowFields";
|
723
686
|
var ArrayFieldRowContainer = ({
|
724
687
|
children,
|
725
|
-
index
|
688
|
+
index,
|
689
|
+
...rest
|
726
690
|
}) => {
|
727
691
|
const context = useArrayFieldRow({ index });
|
728
692
|
const styles = {
|
@@ -732,61 +696,45 @@ var ArrayFieldRowContainer = ({
|
|
732
696
|
width: "100%",
|
733
697
|
mb: 4
|
734
698
|
};
|
735
|
-
return /* @__PURE__ */
|
736
|
-
value: context
|
737
|
-
}, /* @__PURE__ */ React8.createElement(chakra.div, {
|
738
|
-
__css: styles
|
739
|
-
}, children));
|
699
|
+
return /* @__PURE__ */ jsx(ArrayFieldRowProvider, { value: context, children: /* @__PURE__ */ jsx(chakra.div, { ...rest, __css: styles, children }) });
|
740
700
|
};
|
741
|
-
|
742
|
-
ArrayFieldRowContainer.displayName = "ArrayFieldRowContainer";
|
743
|
-
}
|
701
|
+
ArrayFieldRowContainer.displayName = "ArrayFieldRowContainer";
|
744
702
|
var ArrayFieldRemoveButton = (props) => {
|
745
|
-
return /* @__PURE__ */
|
746
|
-
"aria-label": "Remove row",
|
747
|
-
...useArrayFieldRemoveButton(),
|
748
|
-
...props
|
749
|
-
}, props.children || /* @__PURE__ */ React8.createElement(MinusIcon, null));
|
703
|
+
return /* @__PURE__ */ jsx(Button, { "aria-label": "Remove row", ...useArrayFieldRemoveButton(), ...props, children: props.children || /* @__PURE__ */ jsx(MinusIcon, {}) });
|
750
704
|
};
|
751
|
-
|
752
|
-
ArrayFieldRemoveButton.displayName = "ArrayFieldRemoveButton";
|
753
|
-
}
|
705
|
+
ArrayFieldRemoveButton.displayName = "ArrayFieldRemoveButton";
|
754
706
|
var ArrayFieldAddButton = (props) => {
|
755
|
-
return /* @__PURE__ */
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
707
|
+
return /* @__PURE__ */ jsx(
|
708
|
+
Button,
|
709
|
+
{
|
710
|
+
"aria-label": "Add row",
|
711
|
+
float: "right",
|
712
|
+
...useArrayFieldAddButton(),
|
713
|
+
...props,
|
714
|
+
children: props.children || /* @__PURE__ */ jsx(PlusIcon, {})
|
715
|
+
}
|
716
|
+
);
|
761
717
|
};
|
762
|
-
|
763
|
-
ArrayFieldAddButton.displayName = "ArrayFieldAddButton";
|
764
|
-
}
|
718
|
+
ArrayFieldAddButton.displayName = "ArrayFieldAddButton";
|
765
719
|
var ArrayField = forwardRef(
|
766
720
|
(props, ref) => {
|
767
721
|
const { children, ...containerProps } = props;
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
|
773
|
-
index
|
774
|
-
}, children)))), /* @__PURE__ */ React8.createElement(ArrayFieldAddButton, null));
|
722
|
+
const rowFn = isFunction(children) ? children : (fields) => /* @__PURE__ */ jsx(Fragment, { children: fields.map(({ id }, index) => /* @__PURE__ */ jsx(ArrayFieldRow, { index, children }, id)) || null });
|
723
|
+
return /* @__PURE__ */ jsxs(ArrayFieldContainer, { ref, ...containerProps, children: [
|
724
|
+
/* @__PURE__ */ jsx(ArrayFieldRows, { children: rowFn }),
|
725
|
+
/* @__PURE__ */ jsx(ArrayFieldAddButton, {})
|
726
|
+
] });
|
775
727
|
}
|
776
728
|
);
|
777
|
-
|
778
|
-
ArrayField.displayName = "ArrayField";
|
779
|
-
}
|
729
|
+
ArrayField.displayName = "ArrayField";
|
780
730
|
var ArrayFieldRows = ({
|
781
731
|
children
|
782
732
|
}) => {
|
783
733
|
const { fields } = useArrayFieldContext();
|
784
734
|
return children(fields);
|
785
735
|
};
|
786
|
-
|
787
|
-
|
788
|
-
}
|
789
|
-
var ArrayFieldContainer = React8.forwardRef(
|
736
|
+
ArrayFieldRows.displayName = "ArrayFieldRows";
|
737
|
+
var ArrayFieldContainer = React11.forwardRef(
|
790
738
|
({
|
791
739
|
name,
|
792
740
|
defaultValue,
|
@@ -803,74 +751,59 @@ var ArrayFieldContainer = React8.forwardRef(
|
|
803
751
|
min,
|
804
752
|
max
|
805
753
|
});
|
806
|
-
|
807
|
-
return /* @__PURE__ */
|
808
|
-
value: context
|
809
|
-
}, /* @__PURE__ */ React8.createElement(BaseField, {
|
810
|
-
name,
|
811
|
-
...fieldProps
|
812
|
-
}, children));
|
754
|
+
React11.useImperativeHandle(ref, () => context, [ref, context]);
|
755
|
+
return /* @__PURE__ */ jsx(ArrayFieldProvider, { value: context, children: /* @__PURE__ */ jsx(BaseField, { name, ...fieldProps, children }) });
|
813
756
|
}
|
814
757
|
);
|
815
|
-
|
816
|
-
ArrayFieldContainer.displayName = "ArrayFieldContainer";
|
817
|
-
}
|
758
|
+
ArrayFieldContainer.displayName = "ArrayFieldContainer";
|
818
759
|
var FormLegend = (props) => {
|
819
760
|
const styles = useStyleConfig("SuiFormLegend");
|
820
|
-
return /* @__PURE__ */
|
821
|
-
as: "legend",
|
822
|
-
sx: styles,
|
823
|
-
...props
|
824
|
-
});
|
761
|
+
return /* @__PURE__ */ jsx(FormLabel, { as: "legend", sx: styles, ...props });
|
825
762
|
};
|
826
763
|
var ObjectField = (props) => {
|
827
764
|
const { name, label, hideLabel, children, columns, spacing, ...fieldProps } = props;
|
828
|
-
return /* @__PURE__ */
|
829
|
-
|
830
|
-
|
831
|
-
|
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)));
|
765
|
+
return /* @__PURE__ */ jsxs(FormControl, { name, as: "fieldset", ...fieldProps, children: [
|
766
|
+
/* @__PURE__ */ jsx(FormLegend, { display: hideLabel ? "none" : "block", children: label }),
|
767
|
+
/* @__PURE__ */ jsx(FormLayout, { columns, gridGap: spacing, children: mapNestedFields(name, children) })
|
768
|
+
] });
|
838
769
|
};
|
839
|
-
|
840
|
-
ObjectField.displayName = "ObjectField";
|
841
|
-
}
|
770
|
+
ObjectField.displayName = "ObjectField";
|
842
771
|
var mapNestedFields2 = (resolver, name) => {
|
843
772
|
var _a;
|
844
773
|
return (_a = resolver.getNestedFields(name)) == null ? void 0 : _a.map(
|
845
|
-
({ name: name2, type, ...nestedFieldProps }, i) => /* @__PURE__ */
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
774
|
+
({ name: name2, type, ...nestedFieldProps }, i) => /* @__PURE__ */ jsx(
|
775
|
+
Field,
|
776
|
+
{
|
777
|
+
name: name2,
|
778
|
+
type,
|
779
|
+
...nestedFieldProps
|
780
|
+
},
|
781
|
+
name2 || i
|
782
|
+
)
|
851
783
|
);
|
852
784
|
};
|
853
|
-
var
|
854
|
-
schema,
|
855
|
-
fieldResolver,
|
785
|
+
var AutoFields = ({
|
786
|
+
schema: schemaProp,
|
787
|
+
fieldResolver: fieldResolverProp,
|
856
788
|
focusFirstField,
|
857
789
|
...props
|
858
790
|
}) => {
|
859
|
-
const
|
860
|
-
|
861
|
-
|
862
|
-
);
|
863
|
-
const fields =
|
791
|
+
const context = useFormContext();
|
792
|
+
const schema = schemaProp || context.schema;
|
793
|
+
const fieldResolver = fieldResolverProp || context.fieldResolver;
|
794
|
+
const resolver = React11.useMemo(() => fieldResolver, [schema, fieldResolver]);
|
795
|
+
const fields = React11.useMemo(() => resolver == null ? void 0 : resolver.getFields(), [resolver]);
|
864
796
|
const form = useFormContext();
|
865
|
-
|
797
|
+
React11.useEffect(() => {
|
866
798
|
var _a;
|
867
|
-
if (focusFirstField && ((_a = fields[0]) == null ? void 0 : _a.name)) {
|
799
|
+
if (focusFirstField && ((_a = fields == null ? void 0 : fields[0]) == null ? void 0 : _a.name)) {
|
868
800
|
form.setFocus(fields[0].name);
|
869
801
|
}
|
870
802
|
}, [schema, fieldResolver, focusFirstField]);
|
871
|
-
|
872
|
-
|
873
|
-
}
|
803
|
+
if (!resolver) {
|
804
|
+
return null;
|
805
|
+
}
|
806
|
+
return /* @__PURE__ */ jsx(FormLayout, { ...props, children: fields == null ? void 0 : fields.map(
|
874
807
|
({
|
875
808
|
name,
|
876
809
|
type,
|
@@ -878,28 +811,15 @@ var Fields = ({
|
|
878
811
|
...fieldProps
|
879
812
|
}) => {
|
880
813
|
if (type === "array") {
|
881
|
-
return /* @__PURE__ */
|
882
|
-
key: name,
|
883
|
-
name,
|
884
|
-
...fieldProps
|
885
|
-
}, mapNestedFields2(resolver, name));
|
814
|
+
return /* @__PURE__ */ jsx(ArrayField, { name, ...fieldProps, children: mapNestedFields2(resolver, name) }, name);
|
886
815
|
} else if (type === "object") {
|
887
|
-
return /* @__PURE__ */
|
888
|
-
key: name,
|
889
|
-
name,
|
890
|
-
...fieldProps
|
891
|
-
}, mapNestedFields2(resolver, name));
|
816
|
+
return /* @__PURE__ */ jsx(ObjectField, { name, ...fieldProps, children: mapNestedFields2(resolver, name) }, name);
|
892
817
|
}
|
893
|
-
return /* @__PURE__ */
|
894
|
-
key: name,
|
895
|
-
name,
|
896
|
-
type,
|
897
|
-
...fieldProps
|
898
|
-
});
|
818
|
+
return /* @__PURE__ */ jsx(Field, { name, type, ...fieldProps }, name);
|
899
819
|
}
|
900
|
-
));
|
820
|
+
) });
|
901
821
|
};
|
902
|
-
|
822
|
+
AutoFields.displayName = "Fields";
|
903
823
|
var SubmitButton = forwardRef(
|
904
824
|
(props, ref) => {
|
905
825
|
const {
|
@@ -910,15 +830,19 @@ var SubmitButton = forwardRef(
|
|
910
830
|
isLoading,
|
911
831
|
...rest
|
912
832
|
} = props;
|
913
|
-
const { formState } = useFormContext();
|
833
|
+
const { formState } = useFormContext$1();
|
914
834
|
const isDisabled = disableIfUntouched && !formState.isDirty || disableIfInvalid && !formState.isValid || isDisabledProp;
|
915
|
-
return /* @__PURE__ */
|
916
|
-
|
917
|
-
|
918
|
-
|
919
|
-
|
920
|
-
|
921
|
-
|
835
|
+
return /* @__PURE__ */ jsx(
|
836
|
+
Button,
|
837
|
+
{
|
838
|
+
...rest,
|
839
|
+
ref,
|
840
|
+
type: "submit",
|
841
|
+
isLoading: formState.isSubmitting || isLoading,
|
842
|
+
isDisabled,
|
843
|
+
children
|
844
|
+
}
|
845
|
+
);
|
922
846
|
}
|
923
847
|
);
|
924
848
|
SubmitButton.defaultProps = {
|
@@ -927,28 +851,6 @@ SubmitButton.defaultProps = {
|
|
927
851
|
disableIfInvalid: false
|
928
852
|
};
|
929
853
|
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
854
|
var DisplayIf = ({
|
953
855
|
children,
|
954
856
|
name,
|
@@ -966,10 +868,92 @@ var DisplayIf = ({
|
|
966
868
|
const context = useFormContext();
|
967
869
|
return condition(value, context) ? children : null;
|
968
870
|
};
|
969
|
-
|
970
|
-
|
971
|
-
|
972
|
-
|
871
|
+
DisplayIf.displayName = "DisplayIf";
|
872
|
+
var Form = forwardRef(
|
873
|
+
(props, ref) => {
|
874
|
+
const {
|
875
|
+
mode = "all",
|
876
|
+
resolver,
|
877
|
+
fieldResolver,
|
878
|
+
fields,
|
879
|
+
reValidateMode,
|
880
|
+
shouldFocusError,
|
881
|
+
shouldUnregister,
|
882
|
+
shouldUseNativeValidation,
|
883
|
+
criteriaMode,
|
884
|
+
delayError,
|
885
|
+
schema,
|
886
|
+
defaultValues,
|
887
|
+
values,
|
888
|
+
context,
|
889
|
+
resetOptions,
|
890
|
+
onChange,
|
891
|
+
onSubmit,
|
892
|
+
onError,
|
893
|
+
formRef,
|
894
|
+
children,
|
895
|
+
...rest
|
896
|
+
} = props;
|
897
|
+
const form = {
|
898
|
+
mode,
|
899
|
+
resolver,
|
900
|
+
defaultValues,
|
901
|
+
values,
|
902
|
+
reValidateMode,
|
903
|
+
shouldFocusError,
|
904
|
+
shouldUnregister,
|
905
|
+
shouldUseNativeValidation,
|
906
|
+
criteriaMode,
|
907
|
+
delayError,
|
908
|
+
context,
|
909
|
+
resetOptions
|
910
|
+
};
|
911
|
+
const methods = useForm(form);
|
912
|
+
const { handleSubmit } = methods;
|
913
|
+
React11.useImperativeHandle(formRef, () => methods, [formRef, methods]);
|
914
|
+
React11.useEffect(() => {
|
915
|
+
let subscription;
|
916
|
+
if (onChange) {
|
917
|
+
subscription = methods.watch(onChange);
|
918
|
+
}
|
919
|
+
return () => subscription == null ? void 0 : subscription.unsubscribe();
|
920
|
+
}, [methods, onChange]);
|
921
|
+
let _children = children;
|
922
|
+
if (!_children && fieldResolver) {
|
923
|
+
_children = /* @__PURE__ */ jsxs(FormLayout, { children: [
|
924
|
+
/* @__PURE__ */ jsx(AutoFields, {}),
|
925
|
+
/* @__PURE__ */ jsx(SubmitButton, { ...fields == null ? void 0 : fields.submit })
|
926
|
+
] });
|
927
|
+
}
|
928
|
+
return /* @__PURE__ */ jsx(
|
929
|
+
FormProvider,
|
930
|
+
{
|
931
|
+
...methods,
|
932
|
+
schema,
|
933
|
+
fieldResolver,
|
934
|
+
fields,
|
935
|
+
children: /* @__PURE__ */ jsx(
|
936
|
+
chakra.form,
|
937
|
+
{
|
938
|
+
ref,
|
939
|
+
onSubmit: handleSubmit(onSubmit, onError),
|
940
|
+
...rest,
|
941
|
+
className: cx("sui-form", props.className),
|
942
|
+
children: runIfFn(_children, {
|
943
|
+
Field,
|
944
|
+
DisplayIf,
|
945
|
+
ArrayField,
|
946
|
+
ObjectField,
|
947
|
+
...methods
|
948
|
+
})
|
949
|
+
}
|
950
|
+
)
|
951
|
+
}
|
952
|
+
);
|
953
|
+
}
|
954
|
+
);
|
955
|
+
Form.displayName = "Form";
|
956
|
+
var [StepFormProvider, useStepFormContext] = createContext$1({
|
973
957
|
name: "StepFormContext",
|
974
958
|
errorMessage: "useStepFormContext: `context` is undefined. Seems you forgot to wrap step form components in `<StepForm />`"
|
975
959
|
});
|
@@ -977,8 +961,8 @@ function useStepForm(props) {
|
|
977
961
|
const { onChange, ...rest } = props;
|
978
962
|
const stepper = useStepper(rest);
|
979
963
|
const { activeStep, isLastStep, nextStep } = stepper;
|
980
|
-
const [steps, updateSteps] =
|
981
|
-
const onSubmitStep =
|
964
|
+
const [steps, updateSteps] = React11.useState({});
|
965
|
+
const onSubmitStep = React11.useCallback(
|
982
966
|
async (data) => {
|
983
967
|
var _a, _b;
|
984
968
|
try {
|
@@ -999,7 +983,7 @@ function useStepForm(props) {
|
|
999
983
|
},
|
1000
984
|
[steps, activeStep, isLastStep]
|
1001
985
|
);
|
1002
|
-
const getFormProps =
|
986
|
+
const getFormProps = React11.useCallback(() => {
|
1003
987
|
const step = steps[activeStep];
|
1004
988
|
return {
|
1005
989
|
onSubmit: onSubmitStep,
|
@@ -1007,7 +991,7 @@ function useStepForm(props) {
|
|
1007
991
|
resolver: step == null ? void 0 : step.resolver
|
1008
992
|
};
|
1009
993
|
}, [steps, onSubmitStep, activeStep]);
|
1010
|
-
const updateStep =
|
994
|
+
const updateStep = React11.useCallback(
|
1011
995
|
(step) => {
|
1012
996
|
updateSteps((steps2) => {
|
1013
997
|
return {
|
@@ -1029,7 +1013,7 @@ function useFormStep(props) {
|
|
1029
1013
|
const { name, schema, resolver, onSubmit } = props;
|
1030
1014
|
const step = useStep({ name });
|
1031
1015
|
const { steps, updateStep } = useStepFormContext();
|
1032
|
-
|
1016
|
+
React11.useEffect(() => {
|
1033
1017
|
updateStep({ name, schema, resolver, onSubmit });
|
1034
1018
|
}, [name, schema]);
|
1035
1019
|
return {
|
@@ -1037,92 +1021,111 @@ function useFormStep(props) {
|
|
1037
1021
|
...steps[name] || { name, schema }
|
1038
1022
|
};
|
1039
1023
|
}
|
1040
|
-
|
1041
|
-
// src/step-form.tsx
|
1042
|
-
var StepForm = React8.forwardRef(
|
1024
|
+
var StepForm = React11.forwardRef(
|
1043
1025
|
(props, ref) => {
|
1044
1026
|
const { children, ...rest } = props;
|
1045
1027
|
const stepper = useStepForm(props);
|
1046
1028
|
const { getFormProps, ...ctx } = stepper;
|
1047
|
-
const context =
|
1048
|
-
return /* @__PURE__ */
|
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))));
|
1029
|
+
const context = React11.useMemo(() => ctx, [ctx]);
|
1030
|
+
return /* @__PURE__ */ jsx(StepperProvider, { value: context, children: /* @__PURE__ */ jsx(StepFormProvider, { value: context, children: /* @__PURE__ */ jsx(Form, { ref, ...rest, ...getFormProps(), children: runIfFn(children, stepper) }) }) });
|
1057
1031
|
}
|
1058
1032
|
);
|
1059
1033
|
var FormStepper = (props) => {
|
1060
1034
|
const { activeIndex, setIndex } = useStepperContext();
|
1061
1035
|
const { children, orientation, variant, colorScheme, size, ...rest } = props;
|
1062
|
-
const elements =
|
1063
|
-
if (
|
1036
|
+
const elements = React11.Children.map(children, (child) => {
|
1037
|
+
if (React11.isValidElement(child) && (child == null ? void 0 : child.type) === FormStep) {
|
1064
1038
|
const { isCompleted } = useFormStep(child.props);
|
1065
|
-
return /* @__PURE__ */
|
1066
|
-
|
1067
|
-
|
1068
|
-
|
1069
|
-
|
1070
|
-
|
1039
|
+
return /* @__PURE__ */ jsx(
|
1040
|
+
StepperStep,
|
1041
|
+
{
|
1042
|
+
name: child.props.name,
|
1043
|
+
title: child.props.title,
|
1044
|
+
isCompleted,
|
1045
|
+
...rest,
|
1046
|
+
children: child.props.children
|
1047
|
+
}
|
1048
|
+
);
|
1071
1049
|
}
|
1072
1050
|
return child;
|
1073
1051
|
});
|
1074
|
-
const onChange =
|
1052
|
+
const onChange = React11.useCallback((i) => {
|
1075
1053
|
setIndex(i);
|
1076
1054
|
}, []);
|
1077
|
-
return /* @__PURE__ */
|
1078
|
-
|
1079
|
-
|
1080
|
-
|
1081
|
-
|
1082
|
-
|
1083
|
-
|
1084
|
-
|
1085
|
-
|
1086
|
-
|
1087
|
-
|
1055
|
+
return /* @__PURE__ */ jsx(
|
1056
|
+
StepperContainer,
|
1057
|
+
{
|
1058
|
+
orientation,
|
1059
|
+
step: activeIndex,
|
1060
|
+
variant,
|
1061
|
+
colorScheme,
|
1062
|
+
size,
|
1063
|
+
onChange,
|
1064
|
+
children: /* @__PURE__ */ jsx(StepperSteps, { mb: "4", ...props, children: elements })
|
1065
|
+
}
|
1066
|
+
);
|
1088
1067
|
};
|
1089
1068
|
var FormStep = (props) => {
|
1090
1069
|
const { name, schema, resolver, children, className, onSubmit, ...rest } = props;
|
1091
1070
|
const step = useFormStep({ name, schema, resolver, onSubmit });
|
1092
1071
|
const { isActive } = step;
|
1093
|
-
return isActive ? /* @__PURE__ */
|
1094
|
-
...rest,
|
1095
|
-
className: cx("sui-form__step", className)
|
1096
|
-
}, children) : null;
|
1072
|
+
return isActive ? /* @__PURE__ */ jsx(chakra.div, { ...rest, className: cx("sui-form__step", className), children }) : null;
|
1097
1073
|
};
|
1098
|
-
|
1099
|
-
FormStep.displayName = "FormStep";
|
1100
|
-
}
|
1074
|
+
FormStep.displayName = "FormStep";
|
1101
1075
|
var PrevButton = (props) => {
|
1102
1076
|
const { isFirstStep, isCompleted, prevStep } = useStepperContext();
|
1103
|
-
return /* @__PURE__ */
|
1104
|
-
|
1105
|
-
|
1106
|
-
|
1107
|
-
|
1108
|
-
|
1109
|
-
|
1077
|
+
return /* @__PURE__ */ jsx(
|
1078
|
+
Button,
|
1079
|
+
{
|
1080
|
+
isDisabled: isFirstStep || isCompleted,
|
1081
|
+
label: "Back",
|
1082
|
+
...props,
|
1083
|
+
className: cx("sui-form__prev-button", props.className),
|
1084
|
+
onClick: callAllHandlers(props.onClick, prevStep)
|
1085
|
+
}
|
1086
|
+
);
|
1110
1087
|
};
|
1111
|
-
|
1112
|
-
PrevButton.displayName = "PrevButton";
|
1113
|
-
}
|
1088
|
+
PrevButton.displayName = "PrevButton";
|
1114
1089
|
var NextButton = (props) => {
|
1115
1090
|
const { label = "Next", submitLabel = "Complete", ...rest } = props;
|
1116
1091
|
const { isLastStep, isCompleted } = useStepperContext();
|
1117
|
-
return /* @__PURE__ */
|
1118
|
-
|
1119
|
-
|
1120
|
-
|
1121
|
-
|
1092
|
+
return /* @__PURE__ */ jsx(
|
1093
|
+
SubmitButton,
|
1094
|
+
{
|
1095
|
+
...rest,
|
1096
|
+
isDisabled: isCompleted,
|
1097
|
+
className: cx("sui-form__next-button", props.className),
|
1098
|
+
children: isLastStep || isCompleted ? submitLabel : label
|
1099
|
+
}
|
1100
|
+
);
|
1101
|
+
};
|
1102
|
+
NextButton.displayName = "NextButton";
|
1103
|
+
var mapFields = (schema) => schema && Object.entries(schema).map(([name, { items, label, title, ...field }]) => {
|
1104
|
+
return {
|
1105
|
+
...field,
|
1106
|
+
name,
|
1107
|
+
label: label || title || name
|
1108
|
+
// json schema compatibility
|
1109
|
+
};
|
1110
|
+
});
|
1111
|
+
var objectFieldResolver = (schema) => {
|
1112
|
+
const getFields = () => {
|
1113
|
+
return mapFields(schema);
|
1114
|
+
};
|
1115
|
+
const getNestedFields = (name) => {
|
1116
|
+
var _a;
|
1117
|
+
const field = get$1(schema, name);
|
1118
|
+
if (!field)
|
1119
|
+
return [];
|
1120
|
+
if (((_a = field.items) == null ? void 0 : _a.type) === "object") {
|
1121
|
+
return mapFields(field.items.properties);
|
1122
|
+
} else if (field.type === "object") {
|
1123
|
+
return mapFields(field.properties);
|
1124
|
+
}
|
1125
|
+
return [field.items];
|
1126
|
+
};
|
1127
|
+
return { getFields, getNestedFields };
|
1122
1128
|
};
|
1123
|
-
if (__DEV__) {
|
1124
|
-
NextButton.displayName = "NextButton";
|
1125
|
-
}
|
1126
1129
|
var WatchField = (props) => {
|
1127
1130
|
const { name, defaultValue, isDisabled, isExact } = props;
|
1128
1131
|
const form = useFormContext();
|
@@ -1134,7 +1137,29 @@ var WatchField = (props) => {
|
|
1134
1137
|
});
|
1135
1138
|
return props.children(field, form) || null;
|
1136
1139
|
};
|
1140
|
+
function createForm({
|
1141
|
+
resolver,
|
1142
|
+
fieldResolver = objectFieldResolver,
|
1143
|
+
fields
|
1144
|
+
} = {}) {
|
1145
|
+
const CreateForm = forwardRef(
|
1146
|
+
(props, ref) => {
|
1147
|
+
const { schema, ...rest } = props;
|
1148
|
+
return /* @__PURE__ */ jsx(FieldsProvider, { value: fields || {}, children: /* @__PURE__ */ jsx(
|
1149
|
+
Form,
|
1150
|
+
{
|
1151
|
+
ref,
|
1152
|
+
resolver: resolver == null ? void 0 : resolver(props.schema),
|
1153
|
+
fieldResolver: fieldResolver == null ? void 0 : fieldResolver(schema),
|
1154
|
+
...rest
|
1155
|
+
}
|
1156
|
+
) });
|
1157
|
+
}
|
1158
|
+
);
|
1159
|
+
return CreateForm;
|
1160
|
+
}
|
1161
|
+
var Form2 = createForm();
|
1137
1162
|
|
1138
|
-
export { ArrayField, ArrayFieldAddButton, ArrayFieldContainer, ArrayFieldProvider, ArrayFieldRemoveButton, ArrayFieldRow, ArrayFieldRowContainer, ArrayFieldRowFields, ArrayFieldRowProvider, ArrayFieldRows,
|
1163
|
+
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, defaultFieldTypes, objectFieldResolver, useArrayField, useArrayFieldAddButton, useArrayFieldContext, useArrayFieldRemoveButton, useArrayFieldRow, useArrayFieldRowContext, useField, useFormContext, useFormStep, useStepForm, useStepFormContext };
|
1139
1164
|
//# sourceMappingURL=out.js.map
|
1140
1165
|
//# sourceMappingURL=index.mjs.map
|