eddev 0.3.18 → 0.3.20

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.
@@ -1,2 +1,2 @@
1
1
  export * from "./useGravityForm";
2
- export * from "./gravity-forms";
2
+ export * from "./types";
@@ -11,4 +11,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  __exportStar(require("./useGravityForm"), exports);
14
- __exportStar(require("./gravity-forms"), exports);
14
+ __exportStar(require("./types"), exports);
@@ -0,0 +1,139 @@
1
+ import type { ComponentType } from "react";
2
+ import type { GravityFormsFieldState } from "./useGravityForm";
3
+ export declare type GravityFormsField = {
4
+ type: string;
5
+ id: number;
6
+ label: string;
7
+ isRequired: boolean;
8
+ description: string;
9
+ showLabel: boolean;
10
+ visibility: "hidden" | "visible";
11
+ disabled: boolean;
12
+ } & {
13
+ [key: string]: any;
14
+ };
15
+ export declare type GravityFormData = {
16
+ id: number;
17
+ fields: GravityFormsField[];
18
+ title: string;
19
+ description?: string;
20
+ button: {
21
+ text: string;
22
+ };
23
+ };
24
+ export declare type GFTextOptions = {
25
+ placeholder: string;
26
+ size: "small" | "medium" | "large";
27
+ };
28
+ export declare type GFGenericTextOptions = {
29
+ placeholder: string;
30
+ };
31
+ export declare type GFSelectOptions = {
32
+ placeholder: string;
33
+ choices: {
34
+ text: string;
35
+ value: string;
36
+ }[];
37
+ };
38
+ export declare type GFAddressData = {
39
+ address1?: string;
40
+ address2?: string;
41
+ city?: string;
42
+ state?: string;
43
+ zip?: string;
44
+ country?: string;
45
+ };
46
+ export declare type GFAddressOptions = {
47
+ addressType: "international" | "us";
48
+ defaultCountry: string;
49
+ defaultState: string;
50
+ subfields: {
51
+ [K in keyof GFAddressData]: {
52
+ name: K;
53
+ visible: boolean;
54
+ placeholder: string;
55
+ label: string;
56
+ autocomplete: string;
57
+ };
58
+ };
59
+ };
60
+ export declare type GFNameOptions = {
61
+ subfields: {
62
+ [K in keyof GFNameData]: {
63
+ name: K;
64
+ visible: boolean;
65
+ placeholder: string;
66
+ label: string;
67
+ autocomplete: string;
68
+ choices?: {
69
+ text: string;
70
+ value: string;
71
+ }[];
72
+ };
73
+ };
74
+ };
75
+ export declare type GFFileUploadOptions = {
76
+ allowedExtensions: string[];
77
+ multiple: boolean;
78
+ maxFileSize?: number;
79
+ maxFiles?: number;
80
+ };
81
+ export declare type GFNumberOptions = {
82
+ placeholder: string;
83
+ minRange: number;
84
+ maxRange: number;
85
+ };
86
+ export declare type GFNameData = {
87
+ prefix?: string;
88
+ first?: string;
89
+ middle?: string;
90
+ last?: string;
91
+ suffix?: string;
92
+ };
93
+ export declare type GFConsentOptions = {
94
+ checkboxLabel: string;
95
+ };
96
+ export declare type GFFieldTypes = {
97
+ text: string;
98
+ textarea: string;
99
+ select: string;
100
+ number: number;
101
+ checkbox: string[];
102
+ radio: string;
103
+ name: GFNameData;
104
+ date: string;
105
+ time: string;
106
+ phone: string;
107
+ address: GFAddressData;
108
+ website: string;
109
+ email: string;
110
+ fileupload: File | FileList | File[];
111
+ multiselect: string[];
112
+ consent: boolean;
113
+ captcha: any;
114
+ html: void;
115
+ section: void;
116
+ };
117
+ export declare type GFFieldOptions = {
118
+ text: GFTextOptions;
119
+ textarea: GFTextOptions;
120
+ select: GFSelectOptions;
121
+ number: GFNumberOptions;
122
+ checkbox: GFSelectOptions;
123
+ radio: GFSelectOptions;
124
+ name: GFNameOptions;
125
+ date: {};
126
+ time: {};
127
+ phone: GFGenericTextOptions;
128
+ address: GFAddressOptions;
129
+ website: GFGenericTextOptions;
130
+ email: GFGenericTextOptions;
131
+ fileupload: GFFileUploadOptions;
132
+ multiselect: GFSelectOptions;
133
+ consent: GFConsentOptions;
134
+ };
135
+ export declare type GFFieldComponentProps<K extends keyof GFFieldTypes | (string & {})> = GravityFormsFieldState<GFValue<K>, K extends keyof GFFieldOptions ? GFFieldOptions[K] : {}>;
136
+ export declare type GFValue<T extends keyof GFFieldTypes | (string & {})> = T extends keyof GFFieldTypes ? GFFieldTypes[T] : any;
137
+ export declare type GFFieldComponentSet = {
138
+ [K in keyof GFFieldTypes]?: ComponentType<GravityFormsFieldState<GFValue<K>, K extends keyof GFFieldOptions ? GFFieldOptions[K] : {}>>;
139
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,15 +1,14 @@
1
- import { GravityFormsField } from ".";
2
- import { GravityFormData } from "./gravity-forms";
1
+ import { GravityFormData, GravityFormsField } from ".";
3
2
  declare type Options = {
4
3
  form: GravityFormData;
5
4
  };
6
- export declare type GravityFormsFieldState<T = any> = GravityFormsField & {
5
+ export declare type GravityFormsFieldState<T = any, TExtra = {}> = GravityFormsField & {
7
6
  value: T | undefined;
8
7
  error: string | undefined;
9
8
  set(value: T | undefined): void;
10
- };
9
+ } & TExtra;
11
10
  export declare function useGravityForm(opts: Options): {
12
- fields: GravityFormsFieldState<any>[];
11
+ fields: GravityFormsFieldState<any, {}>[];
13
12
  submit(): void;
14
13
  updateField(id: number, value: any): void;
15
14
  submitting: boolean;
@@ -27,4 +26,5 @@ export declare function useGravityForm(opts: Options): {
27
26
  [key: number]: boolean;
28
27
  };
29
28
  };
29
+ export declare type GravityFormState = ReturnType<typeof useGravityForm>;
30
30
  export {};
@@ -72,23 +72,85 @@ var reducer = function (state, action) {
72
72
  return state;
73
73
  };
74
74
  var initializer = function (form) {
75
+ var initialValues = {};
75
76
  return {
76
77
  submitting: false,
77
78
  mainError: null,
78
79
  complete: false,
79
80
  successMessage: null,
80
81
  hasErrors: false,
81
- values: {},
82
+ values: initialValues,
82
83
  errors: {},
83
84
  validity: {},
84
85
  };
85
86
  };
87
+ function optionsForField(field) {
88
+ var _a, _b;
89
+ if (field.type === "fileupload") {
90
+ return {
91
+ allowedExtensions: (_b = (_a = field.allowedExtensions) === null || _a === void 0 ? void 0 : _a.split(/\s*,\s*/g)) !== null && _b !== void 0 ? _b : [],
92
+ maxFileSize: field.maxFileSize ? parseFloat(field.maxFileSize) : undefined,
93
+ multiple: field.multipleFiles ? true : false,
94
+ maxFiles: field.maxFiles ? parseInt(field.maxFiles) : undefined,
95
+ };
96
+ }
97
+ else if (field.type === "address") {
98
+ var subfield = function (id, name) {
99
+ var _a;
100
+ var item = field.inputs.find(function (input) { return String(input.id).endsWith("." + id); });
101
+ return {
102
+ name: name,
103
+ visible: !(item === null || item === void 0 ? void 0 : item.isHidden),
104
+ placeholder: item === null || item === void 0 ? void 0 : item.placeholder,
105
+ label: (_a = item === null || item === void 0 ? void 0 : item.customLabel) !== null && _a !== void 0 ? _a : item === null || item === void 0 ? void 0 : item.label,
106
+ autocomplete: item === null || item === void 0 ? void 0 : item.autocompleteAttribute,
107
+ };
108
+ };
109
+ return {
110
+ addressType: field.addressType,
111
+ defaultCountry: field.defaultCountry,
112
+ defaultState: field.defaultState,
113
+ subfields: {
114
+ address1: subfield(1, "address1"),
115
+ address2: subfield(2, "address2"),
116
+ city: subfield(3, "city"),
117
+ state: subfield(4, "state"),
118
+ zip: subfield(5, "zip"),
119
+ country: subfield(6, "country"),
120
+ },
121
+ };
122
+ }
123
+ else if (field.type === "name") {
124
+ var subfield = function (id, name) {
125
+ var _a;
126
+ var item = field.inputs.find(function (input) { return String(input.id).endsWith("." + id); });
127
+ return {
128
+ name: name,
129
+ visible: !(item === null || item === void 0 ? void 0 : item.isHidden),
130
+ placeholder: item === null || item === void 0 ? void 0 : item.placeholder,
131
+ label: (_a = item === null || item === void 0 ? void 0 : item.customLabel) !== null && _a !== void 0 ? _a : item === null || item === void 0 ? void 0 : item.label,
132
+ autocomplete: item === null || item === void 0 ? void 0 : item.autocompleteAttribute,
133
+ choices: item === null || item === void 0 ? void 0 : item.choices,
134
+ };
135
+ };
136
+ return {
137
+ subfields: {
138
+ prefix: subfield(2, "prefix"),
139
+ first: subfield(3, "first"),
140
+ middle: subfield(4, "middle"),
141
+ last: subfield(6, "last"),
142
+ suffix: subfield(8, "suffix"),
143
+ },
144
+ };
145
+ }
146
+ return {};
147
+ }
86
148
  function useGravityForm(opts) {
87
149
  var form = opts.form;
88
150
  var _a = (0, react_3.useReducer)(reducer, form, initializer), state = _a[0], dispatch = _a[1];
89
- var fields = form.fields.map(function (field) { return (__assign(__assign({}, field), { value: state.values[field.id], error: state.errors[field.id], set: function (value) {
151
+ var fields = form.fields.map(function (field) { return (__assign(__assign(__assign(__assign({}, field), { value: state.values[field.id], error: state.errors[field.id], set: function (value) {
90
152
  dispatch({ type: "field", id: field.id, value: value });
91
- } })); });
153
+ } }), optionsForField(field)), { showLabel: field.labelPlacement !== "hidden_label" })); });
92
154
  var _b = (0, react_2.useState)(null), submitter = _b[0], setSubmitter = _b[1];
93
155
  (0, react_1.useEffect)(function () {
94
156
  var cancelled = false;
@@ -122,18 +184,48 @@ function useGravityForm(opts) {
122
184
  }
123
185
  exports.useGravityForm = useGravityForm;
124
186
  function prepareValuesPayload(form, state, data) {
187
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
125
188
  var _loop_1 = function (field) {
126
189
  var value = state.values[field.id];
127
190
  var key = "input_" + field.id;
128
- if (field.type === "consent") {
129
- key += "_1";
130
- }
131
191
  if (field.type === "checkbox") {
132
192
  field.choices.forEach(function (choice, i) {
133
193
  data.append("".concat(key, ".").concat(i + 1), (value === null || value === void 0 ? void 0 : value.includes(choice.value)) ? choice.value : "");
134
- // data[`${key}.${i + 1}`] =
135
194
  });
136
195
  }
196
+ else if (field.type === "consent") {
197
+ data.append("".concat(key, "_1"), value ? "1" : "");
198
+ data.append("".concat(key, ".2"), (_a = field === null || field === void 0 ? void 0 : field.consentText) !== null && _a !== void 0 ? _a : "");
199
+ data.append("".concat(key, ".3"), "2");
200
+ }
201
+ else if (field.type === "name") {
202
+ data.append("".concat(key, "_2"), (_b = value === null || value === void 0 ? void 0 : value.prefix) !== null && _b !== void 0 ? _b : "");
203
+ data.append("".concat(key, "_3"), (_c = value === null || value === void 0 ? void 0 : value.first) !== null && _c !== void 0 ? _c : "");
204
+ data.append("".concat(key, "_4"), (_d = value === null || value === void 0 ? void 0 : value.middle) !== null && _d !== void 0 ? _d : "");
205
+ data.append("".concat(key, "_6"), (_e = value === null || value === void 0 ? void 0 : value.last) !== null && _e !== void 0 ? _e : "");
206
+ data.append("".concat(key, "_8"), (_f = value === null || value === void 0 ? void 0 : value.suffix) !== null && _f !== void 0 ? _f : "");
207
+ }
208
+ else if (field.type === "address") {
209
+ data.append("".concat(key, "_1"), (_g = value === null || value === void 0 ? void 0 : value.address1) !== null && _g !== void 0 ? _g : "");
210
+ data.append("".concat(key, "_2"), (_h = value === null || value === void 0 ? void 0 : value.address2) !== null && _h !== void 0 ? _h : "");
211
+ data.append("".concat(key, "_3"), (_j = value === null || value === void 0 ? void 0 : value.city) !== null && _j !== void 0 ? _j : "");
212
+ data.append("".concat(key, "_4"), (_k = value === null || value === void 0 ? void 0 : value.state) !== null && _k !== void 0 ? _k : "");
213
+ data.append("".concat(key, "_5"), (_l = value === null || value === void 0 ? void 0 : value.zip) !== null && _l !== void 0 ? _l : "");
214
+ data.append("".concat(key, "_6"), (_m = value === null || value === void 0 ? void 0 : value.country) !== null && _m !== void 0 ? _m : "");
215
+ }
216
+ else if (field.type === "captcha") {
217
+ data.append("g-recaptcha-response", value);
218
+ }
219
+ else if (field.type === "fileupload") {
220
+ if (Array.isArray(value)) {
221
+ value.forEach(function (file) {
222
+ data.append(key, file);
223
+ });
224
+ }
225
+ else if (value) {
226
+ data.append(key, value);
227
+ }
228
+ }
137
229
  else {
138
230
  if (value === undefined || value === null) {
139
231
  data.append(key, "");
@@ -142,14 +234,12 @@ function prepareValuesPayload(form, state, data) {
142
234
  console.log("Setting value", field.label, value);
143
235
  data.append(key, value);
144
236
  }
145
- // data[key] = value
146
237
  }
147
238
  };
148
- for (var _i = 0, _a = form.fields; _i < _a.length; _i++) {
149
- var field = _a[_i];
239
+ for (var _i = 0, _o = form.fields; _i < _o.length; _i++) {
240
+ var field = _o[_i];
150
241
  _loop_1(field);
151
242
  }
152
- // return data
153
243
  }
154
244
  function submit(form, state) {
155
245
  return __awaiter(this, void 0, void 0, function () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eddev",
3
- "version": "0.3.18",
3
+ "version": "0.3.20",
4
4
  "main": "./index.js",
5
5
  "license": "MIT",
6
6
  "bin": {