@saas-ui/forms 3.0.0-next.5 → 3.0.0-next.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -77,8 +77,7 @@ __export(index_exports, {
77
77
  useForm: () => useForm2,
78
78
  useFormContext: () => useFormContext,
79
79
  useFormState: () => import_react_hook_form10.useFormState,
80
- useWatch: () => import_react_hook_form10.useWatch,
81
- useZodForm: () => useZodForm
80
+ useWatch: () => import_react_hook_form10.useWatch
82
81
  });
83
82
  module.exports = __toCommonJS(index_exports);
84
83
 
@@ -89,7 +88,7 @@ var import_react14 = require("react");
89
88
  var import_react4 = require("react");
90
89
  var import_react5 = require("@chakra-ui/react");
91
90
  var import_checkbox = require("@saas-ui/react/checkbox");
92
- var import_input = require("@saas-ui/react/input");
91
+ var import_input_group = require("@saas-ui/react/input-group");
93
92
  var import_number_input = require("@saas-ui/react/number-input");
94
93
  var import_password_input = require("@saas-ui/react/password-input");
95
94
  var import_pin_input = require("@saas-ui/react/pin-input");
@@ -162,8 +161,8 @@ var useBaseField = (props) => {
162
161
  };
163
162
  var BaseField = (props) => {
164
163
  const { rootProps, label, hideLabel, help, error } = useBaseField(props);
165
- const isInvalid = !!error;
166
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_react.Field.Root, { invalid: isInvalid, ...rootProps, children: [
164
+ const isInvalid = !!error || rootProps.invalid;
165
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_react.Field.Root, { ...rootProps, invalid: isInvalid, children: [
167
166
  label && !hideLabel ? /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_react.Field.Label, { children: [
168
167
  label,
169
168
  " ",
@@ -195,7 +194,7 @@ var useField = (type, fallback) => {
195
194
  var import_jsx_runtime4 = require("react/jsx-runtime");
196
195
  var _createField = (InputComponent, { displayName, hideLabel, getBaseField: getBaseFieldProp }) => {
197
196
  const Field3 = (0, import_react3.forwardRef)((props, ref) => {
198
- const { id, label, required, rules } = props;
197
+ const { id, required, rules } = props;
199
198
  const inputRules = {
200
199
  required,
201
200
  ...rules
@@ -218,16 +217,15 @@ var _createField = (InputComponent, { displayName, hideLabel, getBaseField: getB
218
217
  props,
219
218
  ["orientation", "help", "hideLabel"].concat(extraProps)
220
219
  );
221
- return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Component, { ...rootProps, ...baseFieldProps, children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
222
- InputComponent,
220
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
221
+ Component,
223
222
  {
224
- ref,
225
- id,
226
- label: hideLabel ? label : void 0,
227
- ...inputProps,
228
- rules: inputRules
223
+ ...rootProps,
224
+ ...baseFieldProps,
225
+ hideLabel: hideLabel ?? baseFieldProps.hideLabel,
226
+ children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(InputComponent, { ref, id, ...inputProps, rules: inputRules })
229
227
  }
230
- ) });
228
+ );
231
229
  });
232
230
  Field3.displayName = displayName;
233
231
  return Field3;
@@ -299,7 +297,7 @@ var import_jsx_runtime5 = require("react/jsx-runtime");
299
297
  var InputField = createField(
300
298
  ({ type = "text", startElement, endElement, size, ...rest }, ref) => {
301
299
  return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
302
- import_input.InputGroup,
300
+ import_input_group.InputGroup,
303
301
  {
304
302
  startElement,
305
303
  endElement,
@@ -365,10 +363,22 @@ var SelectField = createField(
365
363
  }
366
364
  );
367
365
  var CheckboxField = createField(
368
- ({ label, type, ...props }, ref) => {
369
- return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_checkbox.Checkbox, { ref, ...props, children: label });
366
+ ({ label, type, onChange, value, ...props }, ref) => {
367
+ return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
368
+ import_checkbox.Checkbox,
369
+ {
370
+ ref,
371
+ checked: value,
372
+ onCheckedChange: ({ checked }) => {
373
+ onChange?.(checked);
374
+ },
375
+ ...props,
376
+ children: label
377
+ }
378
+ );
370
379
  },
371
380
  {
381
+ isControlled: true,
372
382
  hideLabel: true
373
383
  }
374
384
  );
@@ -994,34 +1004,51 @@ function createForm({
994
1004
  // src/use-form.tsx
995
1005
  var import_react15 = require("react");
996
1006
  var import_react16 = require("@chakra-ui/react");
997
- var import_zod = require("@hookform/resolvers/zod");
1007
+ var import_standard_schema = require("@hookform/resolvers/standard-schema");
998
1008
  var import_utils10 = require("@saas-ui/core/utils");
999
1009
  var import_react_hook_form8 = require("react-hook-form");
1000
1010
  var import_jsx_runtime14 = require("react/jsx-runtime");
1001
- function useForm2(props = {}) {
1002
- const form = (0, import_react_hook_form8.useForm)(props);
1003
- const FormComponent = (0, import_react15.forwardRef)(
1004
- function FormComponent2(props2, ref) {
1005
- return /* @__PURE__ */ (0, import_jsx_runtime14.jsx)(Form2, { ...props2, form, ref });
1006
- }
1011
+ function useForm2(props) {
1012
+ const { onSubmit, onInvalid, resolver, ...rest } = props;
1013
+ const form = (0, import_react_hook_form8.useForm)({
1014
+ ...rest,
1015
+ resolver: props.schema ? (0, import_standard_schema.standardSchemaResolver)(
1016
+ props.schema
1017
+ ) : resolver
1018
+ });
1019
+ const stableOnSubmit = (0, import_react15.useEffectEvent)(
1020
+ (data) => onSubmit(data)
1007
1021
  );
1008
- return {
1009
- ...form,
1010
- Form: FormComponent,
1011
- Field: Field2,
1012
- DisplayIf,
1013
- ArrayField,
1014
- ObjectField
1015
- };
1022
+ const stableOnInvalid = (0, import_react15.useEffectEvent)(
1023
+ (errors) => onInvalid?.(errors)
1024
+ );
1025
+ return (0, import_react15.useMemo)(() => {
1026
+ const extendedForm = form;
1027
+ extendedForm.submit = form.handleSubmit(stableOnSubmit, stableOnInvalid);
1028
+ extendedForm.Form = function FormAPI(props2) {
1029
+ return /* @__PURE__ */ (0, import_jsx_runtime14.jsx)(
1030
+ Form2,
1031
+ {
1032
+ ...props2,
1033
+ form,
1034
+ onSubmit: props2.onSubmit ?? extendedForm.submit
1035
+ }
1036
+ );
1037
+ };
1038
+ extendedForm.Field = Field2;
1039
+ extendedForm.DisplayIf = DisplayIf;
1040
+ extendedForm.ArrayField = ArrayField;
1041
+ extendedForm.ObjectField = ObjectField;
1042
+ return extendedForm;
1043
+ }, [form]);
1016
1044
  }
1017
1045
  var Form2 = (0, import_react15.forwardRef)(
1018
1046
  function Form3(props, ref) {
1019
- const { children, form, onSubmit, onError, ...rest } = props;
1047
+ const { children, form, ...rest } = props;
1020
1048
  return /* @__PURE__ */ (0, import_jsx_runtime14.jsx)(FormProvider, { ...form, children: /* @__PURE__ */ (0, import_jsx_runtime14.jsx)(
1021
1049
  import_react16.chakra.form,
1022
1050
  {
1023
1051
  ref,
1024
- onSubmit: form.handleSubmit(onSubmit, onError),
1025
1052
  ...rest,
1026
1053
  className: (0, import_utils10.cx)("sui-form", props.className),
1027
1054
  children: props.children
@@ -1029,13 +1056,6 @@ var Form2 = (0, import_react15.forwardRef)(
1029
1056
  ) });
1030
1057
  }
1031
1058
  );
1032
- function useZodForm(props) {
1033
- const { schema, ...rest } = props;
1034
- return useForm2({
1035
- resolver: (0, import_zod.zodResolver)(schema),
1036
- ...rest
1037
- });
1038
- }
1039
1059
 
1040
1060
  // src/display-field.tsx
1041
1061
  var import_react17 = require("@chakra-ui/react");
@@ -1122,7 +1142,6 @@ var Form4 = createForm();
1122
1142
  useForm,
1123
1143
  useFormContext,
1124
1144
  useFormState,
1125
- useWatch,
1126
- useZodForm
1145
+ useWatch
1127
1146
  });
1128
1147
  //# sourceMappingURL=index.js.map