@saas-ui/forms 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,30 @@
1
1
  # @saas-ui/forms
2
2
 
3
+ ## 2.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 68995558: Added new onToggle prop to DisplayIf
8
+ - a3180b02: Removed all Component.defaultProps definitions
9
+
10
+ ### Patch Changes
11
+
12
+ - Updated dependencies [31d05ed5]
13
+ - Updated dependencies [dddb3d1a]
14
+ - Updated dependencies [ebba8404]
15
+ - Updated dependencies [91412d77]
16
+ - Updated dependencies [a3180b02]
17
+ - Updated dependencies [91412d77]
18
+ - @saas-ui/core@2.2.0
19
+
20
+ ## 2.2.1
21
+
22
+ ### Patch Changes
23
+
24
+ - 1c64bfaf: Fixed issue where Zod or Yup schemas would not validate in FormDialog
25
+ - Updated dependencies [4a3c9dc7]
26
+ - @saas-ui/core@2.1.3
27
+
3
28
  ## 2.2.0
4
29
 
5
30
  ### Minor Changes
package/dist/index.d.ts CHANGED
@@ -534,6 +534,7 @@ interface DisplayIfProps<TFieldValues extends FieldValues = FieldValues, TName e
534
534
  isDisabled?: boolean;
535
535
  isExact?: boolean;
536
536
  condition?: (value: unknown, context: UseFormReturn$1<TFieldValues>) => boolean;
537
+ onToggle?: (conditionMatched: boolean, context: UseFormReturn$1<TFieldValues>) => void;
537
538
  }
538
539
  /**
539
540
  * Conditionally render parts of a form.
@@ -541,7 +542,7 @@ interface DisplayIfProps<TFieldValues extends FieldValues = FieldValues, TName e
541
542
  * @see Docs https://saas-ui.dev/docs/components/forms/form
542
543
  */
543
544
  declare const DisplayIf: {
544
- <TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ children, name, defaultValue, isDisabled, isExact, condition, }: DisplayIfProps<TFieldValues, TName>): React$1.ReactElement<any, string | React$1.JSXElementConstructor<any>> | null;
545
+ <TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ children, name, defaultValue, isDisabled, isExact, condition, onToggle, }: DisplayIfProps<TFieldValues, TName>): React$1.ReactElement<any, string | React$1.JSXElementConstructor<any>> | null;
545
546
  displayName: string;
546
547
  };
547
548
 
package/dist/index.js CHANGED
@@ -169,7 +169,7 @@ var import_core = require("@saas-ui/core");
169
169
  var import_jsx_runtime3 = require("react/jsx-runtime");
170
170
  var NumberInput = (0, import_react2.forwardRef)((props, ref) => {
171
171
  const {
172
- hideStepper,
172
+ hideStepper = false,
173
173
  incrementIcon = /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_core.ChevronUpIcon, {}),
174
174
  decrementIcon = /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_core.ChevronDownIcon, {}),
175
175
  placeholder,
@@ -185,9 +185,6 @@ var NumberInput = (0, import_react2.forwardRef)((props, ref) => {
185
185
  ] })
186
186
  ] });
187
187
  });
188
- NumberInput.defaultProps = {
189
- hideStepper: false
190
- };
191
188
  NumberInput.displayName = "NumberInput";
192
189
 
193
190
  // src/password-input/password-input.tsx
@@ -1082,9 +1079,10 @@ var import_jsx_runtime18 = require("react/jsx-runtime");
1082
1079
  var SubmitButton = (0, import_react18.forwardRef)(
1083
1080
  (props, ref) => {
1084
1081
  const {
1082
+ variant = "primary",
1085
1083
  children = "Submit",
1086
- disableIfUntouched,
1087
- disableIfInvalid,
1084
+ disableIfUntouched = false,
1085
+ disableIfInvalid = false,
1088
1086
  isDisabled: isDisabledProp,
1089
1087
  isLoading,
1090
1088
  ...rest
@@ -1096,6 +1094,7 @@ var SubmitButton = (0, import_react18.forwardRef)(
1096
1094
  {
1097
1095
  ...rest,
1098
1096
  ref,
1097
+ variant,
1099
1098
  type: "submit",
1100
1099
  isLoading: formState.isSubmitting || isLoading,
1101
1100
  isDisabled,
@@ -1104,14 +1103,10 @@ var SubmitButton = (0, import_react18.forwardRef)(
1104
1103
  );
1105
1104
  }
1106
1105
  );
1107
- SubmitButton.defaultProps = {
1108
- variant: "primary",
1109
- disableIfUntouched: false,
1110
- disableIfInvalid: false
1111
- };
1112
1106
  SubmitButton.displayName = "SubmitButton";
1113
1107
 
1114
1108
  // src/display-if.tsx
1109
+ var React10 = __toESM(require("react"));
1115
1110
  var import_react_hook_form6 = require("react-hook-form");
1116
1111
  var DisplayIf = ({
1117
1112
  children,
@@ -1119,8 +1114,11 @@ var DisplayIf = ({
1119
1114
  defaultValue,
1120
1115
  isDisabled,
1121
1116
  isExact,
1122
- condition = (value) => !!value
1117
+ condition = (value) => !!value,
1118
+ onToggle
1123
1119
  }) => {
1120
+ const initializedRef = React10.useRef(false);
1121
+ const matchesRef = React10.useRef(false);
1124
1122
  const value = (0, import_react_hook_form6.useWatch)({
1125
1123
  name,
1126
1124
  defaultValue,
@@ -1128,18 +1126,29 @@ var DisplayIf = ({
1128
1126
  exact: isExact
1129
1127
  });
1130
1128
  const context = useFormContext();
1131
- return condition(value, context) ? children : null;
1129
+ const matches = condition(value, context);
1130
+ React10.useEffect(() => {
1131
+ if (!initializedRef.current) {
1132
+ initializedRef.current = true;
1133
+ return;
1134
+ }
1135
+ if (matchesRef.current === matches)
1136
+ return;
1137
+ matchesRef.current = matches;
1138
+ onToggle == null ? void 0 : onToggle(matches, context);
1139
+ }, [value]);
1140
+ return matches ? children : null;
1132
1141
  };
1133
1142
  DisplayIf.displayName = "DisplayIf";
1134
1143
 
1135
1144
  // src/step-form.tsx
1136
- var React11 = __toESM(require("react"));
1145
+ var React12 = __toESM(require("react"));
1137
1146
  var import_react19 = require("@chakra-ui/react");
1138
1147
  var import_utils9 = require("@chakra-ui/utils");
1139
1148
  var import_core4 = require("@saas-ui/core");
1140
1149
 
1141
1150
  // src/use-step-form.tsx
1142
- var React10 = __toESM(require("react"));
1151
+ var React11 = __toESM(require("react"));
1143
1152
  var import_react_utils3 = require("@chakra-ui/react-utils");
1144
1153
  var import_core3 = require("@saas-ui/core");
1145
1154
  var [StepFormProvider, useStepFormContext] = (0, import_react_utils3.createContext)({
@@ -1155,11 +1164,11 @@ function useStepForm(props) {
1155
1164
  ...rest
1156
1165
  } = props;
1157
1166
  const stepper = (0, import_core3.useStepper)(rest);
1158
- const [options, setOptions] = React10.useState(stepsOptions);
1167
+ const [options, setOptions] = React11.useState(stepsOptions);
1159
1168
  const { activeStep, isLastStep, nextStep } = stepper;
1160
- const [steps, updateSteps] = React10.useState({});
1161
- const mergedData = React10.useRef({});
1162
- const onSubmitStep = React10.useCallback(
1169
+ const [steps, updateSteps] = React11.useState({});
1170
+ const mergedData = React11.useRef({});
1171
+ const onSubmitStep = React11.useCallback(
1163
1172
  async (data) => {
1164
1173
  var _a, _b;
1165
1174
  try {
@@ -1184,7 +1193,7 @@ function useStepForm(props) {
1184
1193
  },
1185
1194
  [steps, activeStep, isLastStep, mergedData]
1186
1195
  );
1187
- const getFormProps = React10.useCallback(() => {
1196
+ const getFormProps = React11.useCallback(() => {
1188
1197
  const step = steps[activeStep];
1189
1198
  return {
1190
1199
  onSubmit: onSubmitStep,
@@ -1196,7 +1205,7 @@ function useStepForm(props) {
1196
1205
  fieldResolver: (step == null ? void 0 : step.schema) ? fieldResolver == null ? void 0 : fieldResolver(step.schema) : void 0
1197
1206
  };
1198
1207
  }, [steps, onSubmitStep, activeStep, resolver, fieldResolver]);
1199
- const updateStep = React10.useCallback(
1208
+ const updateStep = React11.useCallback(
1200
1209
  (step) => {
1201
1210
  const stepOptions = options == null ? void 0 : options.find((s) => s.name === step.name);
1202
1211
  updateSteps((steps2) => {
@@ -1222,7 +1231,7 @@ function useFormStep(props) {
1222
1231
  const { name, schema, resolver, onSubmit } = props;
1223
1232
  const step = (0, import_core3.useStep)({ name });
1224
1233
  const { steps, updateStep } = useStepFormContext();
1225
- React10.useEffect(() => {
1234
+ React11.useEffect(() => {
1226
1235
  updateStep({ name, schema, resolver, onSubmit });
1227
1236
  }, [name, schema]);
1228
1237
  return {
@@ -1245,8 +1254,8 @@ var FormStepper = (props) => {
1245
1254
  render,
1246
1255
  ...rest
1247
1256
  } = props;
1248
- const elements = React11.Children.map(children, (child) => {
1249
- if (React11.isValidElement(child) && (child == null ? void 0 : child.type) === FormStep) {
1257
+ const elements = React12.Children.map(children, (child) => {
1258
+ if (React12.isValidElement(child) && (child == null ? void 0 : child.type) === FormStep) {
1250
1259
  const { isCompleted } = useFormStep(child.props);
1251
1260
  return /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
1252
1261
  import_core4.StepsItem,
@@ -1262,7 +1271,7 @@ var FormStepper = (props) => {
1262
1271
  }
1263
1272
  return child;
1264
1273
  });
1265
- const onChange = React11.useCallback((i) => {
1274
+ const onChange = React12.useCallback((i) => {
1266
1275
  setIndex(i);
1267
1276
  onChangeProp == null ? void 0 : onChangeProp(i);
1268
1277
  }, []);
@@ -1359,7 +1368,7 @@ var WatchField = (props) => {
1359
1368
  };
1360
1369
 
1361
1370
  // src/form.tsx
1362
- var React12 = __toESM(require("react"));
1371
+ var React13 = __toESM(require("react"));
1363
1372
  var import_react20 = require("@chakra-ui/react");
1364
1373
  var import_utils11 = require("@chakra-ui/utils");
1365
1374
  var import_react_hook_form8 = require("react-hook-form");
@@ -1405,8 +1414,8 @@ var Form = (0, import_react20.forwardRef)(
1405
1414
  };
1406
1415
  const methods = (0, import_react_hook_form8.useForm)(form);
1407
1416
  const { handleSubmit } = methods;
1408
- React12.useImperativeHandle(formRef, () => methods, [formRef, methods]);
1409
- React12.useEffect(() => {
1417
+ React13.useImperativeHandle(formRef, () => methods, [formRef, methods]);
1418
+ React13.useEffect(() => {
1410
1419
  let subscription;
1411
1420
  if (onChange) {
1412
1421
  subscription = methods.watch(onChange);
@@ -1459,13 +1468,18 @@ function createForm({
1459
1468
  } = {}) {
1460
1469
  const DefaultForm = (0, import_react21.forwardRef)(
1461
1470
  (props, ref) => {
1462
- const { schema, ...rest } = props;
1471
+ const {
1472
+ schema,
1473
+ resolver: resolverProp,
1474
+ fieldResolver: fieldResolverProp,
1475
+ ...rest
1476
+ } = props;
1463
1477
  return /* @__PURE__ */ (0, import_jsx_runtime21.jsx)(FieldsProvider, { value: fields || {}, children: /* @__PURE__ */ (0, import_jsx_runtime21.jsx)(
1464
1478
  Form,
1465
1479
  {
1466
1480
  ref,
1467
- resolver: resolver == null ? void 0 : resolver(props.schema),
1468
- fieldResolver: fieldResolver == null ? void 0 : fieldResolver(schema),
1481
+ resolver: resolverProp != null ? resolverProp : resolver == null ? void 0 : resolver(props.schema),
1482
+ fieldResolver: fieldResolverProp != null ? fieldResolverProp : fieldResolver == null ? void 0 : fieldResolver(schema),
1469
1483
  ...rest
1470
1484
  }
1471
1485
  ) });