@buildnbuzz/buzzform 0.1.5 → 0.1.7

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
@@ -51,6 +51,7 @@ __export(src_exports, {
51
51
  parseToDate: () => parseToDate,
52
52
  resolveFieldState: () => resolveFieldState,
53
53
  setNestedValue: () => setNestedValue,
54
+ transformFormOutput: () => transformFormOutput,
54
55
  useForm: () => useForm,
55
56
  validateAdapter: () => validateAdapter
56
57
  });
@@ -419,18 +420,18 @@ function createCheckboxGroupFieldSchema(field) {
419
420
  // src/schema/builders/boolean.ts
420
421
  var import_zod6 = require("zod");
421
422
  function createCheckboxFieldSchema(field) {
422
- let schema = import_zod6.z.boolean({ error: "Invalid value" });
423
+ let schema = import_zod6.z.boolean({ error: "Invalid value" }).default(false);
423
424
  if (field.required) {
424
- schema = import_zod6.z.boolean({ error: "This field is required" }).refine((val) => val === true, {
425
+ schema = import_zod6.z.boolean({ error: "Invalid value" }).default(false).refine((val) => val === true, {
425
426
  error: "This field is required"
426
427
  });
427
428
  }
428
429
  return schema;
429
430
  }
430
431
  function createSwitchFieldSchema(field) {
431
- let schema = import_zod6.z.boolean({ error: "Invalid value" });
432
+ let schema = import_zod6.z.boolean({ error: "Invalid value" }).default(false);
432
433
  if (field.required) {
433
- schema = import_zod6.z.boolean({ error: "This field is required" }).refine((val) => val === true, {
434
+ schema = import_zod6.z.boolean({ error: "Invalid value" }).default(false).refine((val) => val === true, {
434
435
  error: "This field is required"
435
436
  });
436
437
  }
@@ -884,6 +885,40 @@ function parseToDate(value) {
884
885
  return void 0;
885
886
  }
886
887
 
888
+ // src/lib/output.ts
889
+ function transformFormOutput(data, config) {
890
+ if (!config || config.type !== "path") {
891
+ return data;
892
+ }
893
+ if (typeof data !== "object" || data === null) {
894
+ return data;
895
+ }
896
+ return flattenToPathKeys(
897
+ data,
898
+ config.delimiter ?? "."
899
+ );
900
+ }
901
+ function flattenToPathKeys(data, delimiter, prefix = "") {
902
+ const result = {};
903
+ for (const [key, value] of Object.entries(data)) {
904
+ const path = prefix ? `${prefix}${delimiter}${key}` : key;
905
+ if (isPlainObject(value)) {
906
+ Object.assign(
907
+ result,
908
+ flattenToPathKeys(value, delimiter, path)
909
+ );
910
+ } else {
911
+ result[path] = value;
912
+ }
913
+ }
914
+ return result;
915
+ }
916
+ function isPlainObject(value) {
917
+ if (typeof value !== "object" || value === null) return false;
918
+ const proto = Object.getPrototypeOf(value);
919
+ return proto === Object.prototype || proto === null;
920
+ }
921
+
887
922
  // src/context/form-context.ts
888
923
  var import_react = require("react");
889
924
  var FormConfigContext = (0, import_react.createContext)(null);
@@ -915,9 +950,9 @@ function extractDefaultsFromFields(fields) {
915
950
  }
916
951
  return hasDefaults ? defaults : void 0;
917
952
  }
918
- function applySettings(form, settings) {
919
- if (!settings) return form;
920
- if (settings.submitOnlyWhenDirty) {
953
+ function applySettings(form, settings, outputConfig) {
954
+ if (!settings && !outputConfig) return form;
955
+ if (settings?.submitOnlyWhenDirty) {
921
956
  const originalHandleSubmit = form.handleSubmit;
922
957
  form.handleSubmit = (e) => {
923
958
  if (!form.formState.isDirty) {
@@ -930,6 +965,21 @@ function applySettings(form, settings) {
930
965
  if (settings) {
931
966
  form.settings = settings;
932
967
  }
968
+ if (outputConfig?.type === "path") {
969
+ const originalGetValues = form.getValues;
970
+ form.getValues = () => {
971
+ const values = originalGetValues();
972
+ return transformFormOutput(values, outputConfig);
973
+ };
974
+ const originalWatch = form.watch;
975
+ form.watch = (name) => {
976
+ const values = originalWatch(name);
977
+ if (!name) {
978
+ return transformFormOutput(values, outputConfig);
979
+ }
980
+ return values;
981
+ };
982
+ }
933
983
  return form;
934
984
  }
935
985
  function useForm(options) {
@@ -938,6 +988,7 @@ function useForm(options) {
938
988
  const resolverFn = globalConfig?.resolver;
939
989
  const mode = options.mode ?? globalConfig?.mode ?? "onChange";
940
990
  const reValidateMode = options.reValidateMode ?? globalConfig?.reValidateMode ?? "onChange";
991
+ const outputConfig = options.output ?? globalConfig?.output;
941
992
  const resolver = options.schema && resolverFn ? resolverFn(options.schema) : void 0;
942
993
  const schemaWithFields = options.schema;
943
994
  const fieldDefaults = (0, import_react2.useMemo)(
@@ -945,6 +996,11 @@ function useForm(options) {
945
996
  [schemaWithFields?.fields]
946
997
  );
947
998
  const defaultValues = options.defaultValues ?? fieldDefaults;
999
+ const userOnSubmit = options.onSubmit;
1000
+ const wrappedOnSubmit = userOnSubmit && outputConfig?.type === "path" ? (data) => {
1001
+ const transformed = transformFormOutput(data, outputConfig);
1002
+ return userOnSubmit(transformed);
1003
+ } : userOnSubmit;
948
1004
  const effectiveAdapter = adapter ?? ((_opts) => {
949
1005
  throw new Error("useForm: No adapter configured.");
950
1006
  });
@@ -953,7 +1009,7 @@ function useForm(options) {
953
1009
  resolver,
954
1010
  mode,
955
1011
  reValidateMode,
956
- onSubmit: options.onSubmit
1012
+ onSubmit: wrappedOnSubmit
957
1013
  });
958
1014
  if (!options.schema) {
959
1015
  throw new Error(
@@ -965,7 +1021,7 @@ function useForm(options) {
965
1021
  "useForm: No adapter configured. Either wrap your app in <FormProvider adapter={...}> or pass adapter in options."
966
1022
  );
967
1023
  }
968
- return applySettings(form, options.settings);
1024
+ return applySettings(form, options.settings, outputConfig);
969
1025
  }
970
1026
  // Annotate the CommonJS export names for ESM import in node:
971
1027
  0 && (module.exports = {
@@ -1000,6 +1056,7 @@ function useForm(options) {
1000
1056
  parseToDate,
1001
1057
  resolveFieldState,
1002
1058
  setNestedValue,
1059
+ transformFormOutput,
1003
1060
  useForm,
1004
1061
  validateAdapter
1005
1062
  });