@saas-ui/forms 3.0.0-next.0 → 3.0.0-next.10

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.mjs CHANGED
@@ -12,13 +12,14 @@ import {
12
12
  createListCollection
13
13
  } from "@chakra-ui/react";
14
14
  import { Checkbox } from "@saas-ui/react/checkbox";
15
- import { InputGroup } from "@saas-ui/react/input-group";
15
+ import { InputGroup } from "@saas-ui/react/input";
16
16
  import { NumberInput } from "@saas-ui/react/number-input";
17
17
  import {
18
18
  PasswordInput
19
19
  } from "@saas-ui/react/password-input";
20
20
  import { PinInput } from "@saas-ui/react/pin-input";
21
- import { Radio, RadioGroup } from "@saas-ui/react/radio";
21
+ import { Radio } from "@saas-ui/react/radio";
22
+ import { RadioGroup } from "@saas-ui/react/radio-group";
22
23
  import { Select } from "@saas-ui/react/select";
23
24
  import { Switch } from "@saas-ui/react/switch";
24
25
 
@@ -51,10 +52,9 @@ var useFormContext = () => {
51
52
  };
52
53
  };
53
54
  var useFieldProps = (name) => {
54
- var _a;
55
- const parsedName = name == null ? void 0 : name.replace(/\.[0-9]/g, ".$");
55
+ const parsedName = name?.replace(/\.[0-9]/g, ".$");
56
56
  const context = useFormContext();
57
- return ((_a = context == null ? void 0 : context.fields) == null ? void 0 : _a[parsedName]) || {};
57
+ return context?.fields?.[parsedName] || {};
58
58
  };
59
59
  var FormProvider = (props) => {
60
60
  const { children, fieldResolver, schema, fields, ...rest } = props;
@@ -100,8 +100,8 @@ var BaseField = (props) => {
100
100
  /* @__PURE__ */ jsx2(Field.RequiredIndicator, {})
101
101
  ] }) : null,
102
102
  props.children,
103
- help && !(error == null ? void 0 : error.message) ? /* @__PURE__ */ jsx2(Field.HelperText, { children: help }) : null,
104
- (error == null ? void 0 : error.message) && /* @__PURE__ */ jsx2(Field.ErrorText, { children: error == null ? void 0 : error.message })
103
+ help && !error?.message ? /* @__PURE__ */ jsx2(Field.HelperText, { children: help }) : null,
104
+ error?.message && /* @__PURE__ */ jsx2(Field.ErrorText, { children: error?.message })
105
105
  ] });
106
106
  };
107
107
  BaseField.displayName = "BaseField";
@@ -117,23 +117,21 @@ var useFieldsContext = () => {
117
117
  return React2.useContext(FieldsContext);
118
118
  };
119
119
  var useField = (type, fallback) => {
120
- var _a;
121
120
  const context = React2.useContext(FieldsContext);
122
- return ((_a = context == null ? void 0 : context.fields) == null ? void 0 : _a[type]) || fallback;
121
+ return context?.fields?.[type] || fallback;
123
122
  };
124
123
 
125
124
  // src/create-field.tsx
126
125
  import { jsx as jsx4 } from "react/jsx-runtime";
127
126
  var _createField = (InputComponent, { displayName, hideLabel, getBaseField: getBaseFieldProp }) => {
128
127
  const Field3 = forwardRef((props, ref) => {
129
- var _a;
130
128
  const { id, label, required, rules } = props;
131
129
  const inputRules = {
132
130
  required,
133
131
  ...rules
134
132
  };
135
133
  const fieldContext = useFieldsContext();
136
- const getBaseField = (_a = fieldContext == null ? void 0 : fieldContext.getBaseField) != null ? _a : getBaseFieldProp;
134
+ const getBaseField = fieldContext?.getBaseField ?? getBaseFieldProp;
137
135
  const { props: extraProps, Component } = React3.useMemo(
138
136
  () => getBaseField(),
139
137
  [getBaseField]
@@ -209,16 +207,15 @@ var withUncontrolledInput = (InputComponent) => {
209
207
  );
210
208
  };
211
209
  var createField = (component, options) => {
212
- var _a;
213
210
  let InputComponent;
214
- if (options == null ? void 0 : options.isControlled) {
211
+ if (options?.isControlled) {
215
212
  InputComponent = withControlledInput(forwardRef(component));
216
213
  } else {
217
214
  InputComponent = withUncontrolledInput(forwardRef(component));
218
215
  }
219
216
  const Field3 = _createField(InputComponent, {
220
- displayName: `${(_a = component.displayName) != null ? _a : "Custom"}Field`,
221
- hideLabel: options == null ? void 0 : options.hideLabel,
217
+ displayName: `${component.displayName ?? "Custom"}Field`,
218
+ hideLabel: options?.hideLabel,
222
219
  getBaseField: () => ({
223
220
  props: [],
224
221
  Component: BaseField
@@ -274,7 +271,7 @@ var SelectField = createField(
274
271
  }),
275
272
  [options]
276
273
  );
277
- const value = multiple ? [...valueProp != null ? valueProp : []] : valueProp ? [valueProp] : [];
274
+ const value = multiple ? [...valueProp ?? []] : valueProp ? [valueProp] : [];
278
275
  return /* @__PURE__ */ jsxs2(
279
276
  Select.Root,
280
277
  {
@@ -309,11 +306,11 @@ var RadioField = createField(
309
306
  (props, ref) => {
310
307
  const { options, onChange, flexDirection = "column", gap, ...rest } = props;
311
308
  return /* @__PURE__ */ jsx5(
312
- RadioGroup,
309
+ RadioGroup.Root,
313
310
  {
314
311
  ref,
315
312
  onValueChange: ({ value }) => {
316
- onChange == null ? void 0 : onChange(value);
313
+ onChange?.(value);
317
314
  },
318
315
  ...rest,
319
316
  children: /* @__PURE__ */ jsx5(Stack, { flexDirection, gap, children: options.map((option) => /* @__PURE__ */ jsx5(Radio, { value: option.value, children: option.label || option.value }, option.value)) })
@@ -327,7 +324,7 @@ var RadioField = createField(
327
324
  var PinField = createField(
328
325
  (props, ref) => {
329
326
  const { pinType, value: valueProp, onChange, ...inputProps } = props;
330
- const value = (valueProp == null ? void 0 : valueProp.split("")) || [];
327
+ const value = valueProp?.split("") || [];
331
328
  return /* @__PURE__ */ jsx5(
332
329
  PinInput,
333
330
  {
@@ -377,10 +374,9 @@ var objectFieldResolver = (schema) => {
377
374
  return mapFields(schema);
378
375
  };
379
376
  const getNestedFields = (name) => {
380
- var _a;
381
377
  const field = get2(schema, name);
382
378
  if (!field) return [];
383
- if (((_a = field.items) == null ? void 0 : _a.type) === "object") {
379
+ if (field.items?.type === "object") {
384
380
  return mapFields(field.items.properties);
385
381
  } else if (field.type === "object") {
386
382
  return mapFields(field.properties);
@@ -512,15 +508,19 @@ var useArrayFieldAddButton = () => {
512
508
  import * as React6 from "react";
513
509
  var mapNestedFields = (name, children) => {
514
510
  return React6.Children.map(children, (child) => {
515
- if (React6.isValidElement(child) && child.props.name) {
516
- let childName = child.props.name;
511
+ if (React6.isValidElement(child)) {
512
+ const props = child.props;
513
+ if (!props.name) {
514
+ return child;
515
+ }
516
+ let childName = props.name;
517
517
  if (childName.includes(".")) {
518
518
  childName = childName.replace(/^.*\.(.*)/, "$1");
519
519
  } else if (childName.includes(".$")) {
520
520
  childName = childName.replace(/^.*\.\$(.*)/, "$1");
521
521
  }
522
522
  return React6.cloneElement(child, {
523
- ...child.props,
523
+ ...props,
524
524
  name: `${name}.${childName}`
525
525
  });
526
526
  }
@@ -615,8 +615,8 @@ var ArrayFieldContainer = React7.forwardRef(
615
615
  name,
616
616
  defaultValue,
617
617
  keyName,
618
- min: min || (overrides == null ? void 0 : overrides.min),
619
- max: max || (overrides == null ? void 0 : overrides.max)
618
+ min: min || overrides?.min,
619
+ max: max || overrides?.max
620
620
  });
621
621
  React7.useImperativeHandle(ref, () => context, [ref, context]);
622
622
  return /* @__PURE__ */ jsx7(ArrayFieldProvider, { value: context, children: /* @__PURE__ */ jsx7(BaseField, { name, ...fieldProps, ...overrides, children }) });
@@ -655,7 +655,7 @@ var DisplayIf = ({
655
655
  }
656
656
  if (matchesRef.current === matches) return;
657
657
  matchesRef.current = matches;
658
- onToggle == null ? void 0 : onToggle(matches, context);
658
+ onToggle?.(matches, context);
659
659
  }, [value]);
660
660
  return matches ? children : null;
661
661
  };
@@ -669,7 +669,7 @@ var Field2 = React9.forwardRef(
669
669
  (props, ref) => {
670
670
  const { type = defaultInputType, name } = props;
671
671
  const overrides = useFieldProps(name);
672
- const InputComponent = useField((overrides == null ? void 0 : overrides.type) || type, InputField);
672
+ const InputComponent = useField(overrides?.type || type, InputField);
673
673
  return /* @__PURE__ */ jsx8(InputComponent, { ref, ...props, ...overrides });
674
674
  }
675
675
  );
@@ -706,8 +706,7 @@ ObjectField.displayName = "ObjectField";
706
706
  // src/fields.tsx
707
707
  import { jsx as jsx10 } from "react/jsx-runtime";
708
708
  var mapNestedFields2 = (resolver, name) => {
709
- var _a;
710
- return (_a = resolver.getNestedFields(name)) == null ? void 0 : _a.map(
709
+ return resolver.getNestedFields(name)?.map(
711
710
  ({ name: name2, type, ...nestedFieldProps }, i) => /* @__PURE__ */ jsx10(
712
711
  Field2,
713
712
  {
@@ -729,18 +728,17 @@ var AutoFields = ({
729
728
  const schema = schemaProp || context.schema;
730
729
  const fieldResolver = fieldResolverProp || context.fieldResolver;
731
730
  const resolver = React10.useMemo(() => fieldResolver, [schema, fieldResolver]);
732
- const fields = React10.useMemo(() => resolver == null ? void 0 : resolver.getFields(), [resolver]);
731
+ const fields = React10.useMemo(() => resolver?.getFields(), [resolver]);
733
732
  const form = useFormContext();
734
733
  React10.useEffect(() => {
735
- var _a;
736
- if (focusFirstField && ((_a = fields == null ? void 0 : fields[0]) == null ? void 0 : _a.name)) {
734
+ if (focusFirstField && fields?.[0]?.name) {
737
735
  form.setFocus(fields[0].name);
738
736
  }
739
737
  }, [schema, fieldResolver, focusFirstField]);
740
738
  if (!resolver) {
741
739
  return null;
742
740
  }
743
- return /* @__PURE__ */ jsx10(FormLayout, { ...props, children: fields == null ? void 0 : fields.map(
741
+ return /* @__PURE__ */ jsx10(FormLayout, { ...props, children: fields?.map(
744
742
  ({ name, type, ...fieldProps }) => {
745
743
  if (type === "array") {
746
744
  return /* @__PURE__ */ jsx10(ArrayField, { name, ...fieldProps, children: mapNestedFields2(resolver, name) }, name);
@@ -785,8 +783,8 @@ var SubmitButton = forwardRef5(
785
783
  disableIfInvalid: disableIfInvalidOverride,
786
784
  ...fieldProps
787
785
  } = field;
788
- const disableIfUntouched = disableIfUntouchedOverride != null ? disableIfUntouchedOverride : disableIfUntouchedProp;
789
- const disableIfInvalid = disableIfInvalidOverride != null ? disableIfInvalidOverride : disableIfInvalidProp;
786
+ const disableIfUntouched = disableIfUntouchedOverride ?? disableIfUntouchedProp;
787
+ const disableIfInvalid = disableIfInvalidOverride ?? disableIfInvalidProp;
790
788
  const isDisabled = disableIfUntouched && !formState.isDirty || disableIfInvalid && !formState.isValid || disabledProp;
791
789
  return /* @__PURE__ */ jsx11(
792
790
  Button2,
@@ -855,13 +853,13 @@ var Form = forwardRef6(
855
853
  if (onChange) {
856
854
  subscription = methods.watch(onChange);
857
855
  }
858
- return () => subscription == null ? void 0 : subscription.unsubscribe();
856
+ return () => subscription?.unsubscribe();
859
857
  }, [methods, onChange]);
860
858
  let _children = children;
861
859
  if (!_children && fieldResolver) {
862
860
  _children = /* @__PURE__ */ jsxs5(FormLayout, { children: [
863
861
  /* @__PURE__ */ jsx12(AutoFields, {}),
864
- /* @__PURE__ */ jsx12(SubmitButton, { ...fields == null ? void 0 : fields.submit })
862
+ /* @__PURE__ */ jsx12(SubmitButton, { ...fields?.submit })
865
863
  ] });
866
864
  }
867
865
  return /* @__PURE__ */ jsx12(
@@ -920,8 +918,8 @@ function createForm({
920
918
  Form,
921
919
  {
922
920
  ref,
923
- resolver: resolverProp != null ? resolverProp : resolver == null ? void 0 : resolver(props.schema),
924
- fieldResolver: fieldResolverProp != null ? fieldResolverProp : fieldResolver == null ? void 0 : fieldResolver(schema),
921
+ resolver: resolverProp ?? resolver?.(props.schema),
922
+ fieldResolver: fieldResolverProp ?? fieldResolver?.(schema),
925
923
  ...rest
926
924
  }
927
925
  ) });