@maif/react-forms 1.1.0 → 1.1.1

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/lib/esm/index.js CHANGED
@@ -27958,27 +27958,46 @@ const usePrevious = (value) => {
27958
27958
  // Return previous value (happens before update in useEffect above)
27959
27959
  return ref.current;
27960
27960
  };
27961
- const BasicWrapper = ({ entry, className, help, children, render, functionalProperty, label }) => {
27961
+ const BasicWrapper = ({ entry, children, render, functionalProperty, step }) => {
27962
+ const { formState, watch } = useFormContext();
27963
+ console.debug({ entry, step });
27962
27964
  if (typeof entry === 'object') {
27963
27965
  return children;
27964
27966
  }
27965
- const computedLabel = functionalProperty(entry, label === null ? null : label || entry);
27967
+ const visibleStep = option(step)
27968
+ .map(s => s.visible)
27969
+ .map(visible => {
27970
+ let value;
27971
+ switch (typeof visible) {
27972
+ case 'object':
27973
+ value = watch(visible.ref);
27974
+ return option(visible.test).map(test => test(value)).getOrElse(value);
27975
+ case 'boolean':
27976
+ return visible;
27977
+ default:
27978
+ return true;
27979
+ }
27980
+ })
27981
+ .getOrElse(true);
27982
+ if (!visibleStep) {
27983
+ return null;
27984
+ }
27985
+ const computedLabel = functionalProperty(entry, (step === null || step === void 0 ? void 0 : step.label) === null ? null : (step === null || step === void 0 ? void 0 : step.label) || entry);
27966
27986
  const id = v4();
27967
- const { formState } = useFormContext();
27968
27987
  // FIXME not sure it works as intended with more two or more parts
27969
27988
  const error = entry.split('.').reduce((acc, curr) => acc && acc[curr], formState.errors);
27970
27989
  const isDirty = entry.split('.').reduce((acc, curr) => acc && acc[curr], formState.dirtyFields);
27971
27990
  const isTouched = entry.split('.').reduce((acc, curr) => acc && acc[curr], formState.touchedFields);
27972
27991
  const errorDisplayed = formState.isSubmitted || isDirty || isTouched;
27973
27992
  if (render) {
27974
- return render({ entry, label: computedLabel, error, help, children });
27993
+ return render({ entry, label: computedLabel, error, help: step === null || step === void 0 ? void 0 : step.help, children });
27975
27994
  }
27976
27995
  return (React__default.createElement("div", { className: 'mrf-mt_10', style: { position: 'relative' } },
27977
27996
  computedLabel && React__default.createElement("label", { className: 'mrf-flex mrf-ai_center mrf-mb_5', htmlFor: entry },
27978
27997
  React__default.createElement("span", null, computedLabel),
27979
- help && React__default.createElement(React__default.Fragment, null,
27998
+ (step === null || step === void 0 ? void 0 : step.help) && React__default.createElement(React__default.Fragment, null,
27980
27999
  React__default.createElement(ReactToolTip, { html: true, place: 'bottom', id: id }),
27981
- React__default.createElement("span", { className: 'mrf-flex mrf-ai_center', "data-html": true, "data-tip": help, "data-for": id },
28000
+ React__default.createElement("span", { className: 'mrf-flex mrf-ai_center', "data-html": true, "data-tip": step === null || step === void 0 ? void 0 : step.help, "data-for": id },
27982
28001
  React__default.createElement(HelpCircle, { style: { color: 'gray', width: 17, marginLeft: '.5rem', cursor: 'help' } })))),
27983
28002
  children,
27984
28003
  error && React__default.createElement("div", { className: classNames('mrf-feedback', { ['mrf-txt_red']: !!errorDisplayed }) }, error.message)));
@@ -28167,6 +28186,7 @@ const Form = React__default.forwardRef(function Form({ schema, flow, value, inpu
28167
28186
  trigger,
28168
28187
  methods: Object.assign(Object.assign({}, methods), { data: () => cleanOutputArray(getValues(), schema) })
28169
28188
  }));
28189
+ console.debug({ formFlow });
28170
28190
  return (React__default.createElement(FormProvider, Object.assign({}, methods),
28171
28191
  React__default.createElement(Watcher, { options: options, control: methods.control, schema: schema, onSubmit: onSubmit, handleSubmit: handleSubmit }),
28172
28192
  React__default.createElement("form", { className: className || `mrf-pr_15 mrf-w_100`, onSubmit: handleSubmit(data => {
@@ -28179,25 +28199,7 @@ const Form = React__default.forwardRef(function Form({ schema, flow, value, inpu
28179
28199
  console.error(`no step found for the entry "${entry}" in the given schema. Your form might not work properly. Please fix it`);
28180
28200
  return null;
28181
28201
  }
28182
- const visibleStep = option(step)
28183
- .map((s) => s.visible)
28184
- .map((visible) => {
28185
- let value;
28186
- switch (typeof visible) {
28187
- case 'object':
28188
- value = getValues(visible.ref);
28189
- return option(visible.test).map(test => test(value, idx)).getOrElse(value);
28190
- case 'boolean':
28191
- return visible;
28192
- default:
28193
- return true;
28194
- }
28195
- })
28196
- .getOrElse(true);
28197
- if (!visibleStep) {
28198
- return null;
28199
- }
28200
- return (React__default.createElement(BasicWrapper, { key: `${entry}-${idx}`, entry: entry, functionalProperty: functionalProperty, label: step === null || step === void 0 ? void 0 : step.label, help: step === null || step === void 0 ? void 0 : step.help, render: inputWrapper },
28202
+ return (React__default.createElement(BasicWrapper, { key: `${entry}-${idx}`, entry: entry, functionalProperty: functionalProperty, render: inputWrapper, step: step },
28201
28203
  React__default.createElement(Step, { key: idx, entry: entry, step: step, schema: schema, inputWrapper: inputWrapper, httpClient: maybeCustomHttpClient, functionalProperty: functionalProperty })));
28202
28204
  }),
28203
28205
  React__default.createElement(Footer, { render: footer, reset: () => reset(defaultValues), valid: handleSubmit(data => onSubmit(cleanOutputArray(data, schema)), onError), actions: options.actions }))));
@@ -28224,25 +28226,7 @@ const Step = (props) => {
28224
28226
  console.error(`no step found for the entry "${en}" in the given schema. Your form might not work properly. Please fix it`);
28225
28227
  return null;
28226
28228
  }
28227
- const visibleStep = option(stp)
28228
- .map(s => s.visible)
28229
- .map(visible => {
28230
- let value;
28231
- switch (typeof visible) {
28232
- case 'object':
28233
- value = getValues(visible.ref);
28234
- return option(visible.test).map(test => test(value, index)).getOrElse(value);
28235
- case 'boolean':
28236
- return visible;
28237
- default:
28238
- return true;
28239
- }
28240
- })
28241
- .getOrElse(true);
28242
- if (!visibleStep) {
28243
- return null;
28244
- }
28245
- return (React__default.createElement(BasicWrapper, { key: `collapse-${en}-${entryIdx}`, entry: en, functionalProperty: functionalProperty, label: stp === null || stp === void 0 ? void 0 : stp.label, help: stp === null || stp === void 0 ? void 0 : stp.help, render: inputWrapper },
28229
+ return (React__default.createElement(BasicWrapper, { key: `collapse-${en}-${entryIdx}`, entry: en, functionalProperty: functionalProperty, step: stp, render: inputWrapper },
28246
28230
  React__default.createElement(Step, { entry: en, step: stp, schema: schema, inputWrapper: inputWrapper, httpClient: httpClient, defaultValue: stp === null || stp === void 0 ? void 0 : stp.defaultValue, functionalProperty: functionalProperty })));
28247
28231
  })));
28248
28232
  }
@@ -28444,29 +28428,15 @@ const NestedForm = ({ schema, flow, parent, inputWrapper, maybeCustomHttpClient,
28444
28428
  }, [schemaAndFlow.schema]);
28445
28429
  const computedSandF = schemaAndFlow.flow.reduce((acc, entry) => {
28446
28430
  const step = (typeof entry === "string") ? schemaAndFlow.schema[entry] : schemaAndFlow.schema[entry.label];
28447
- const visibleStep = option(step)
28448
- .map(s => s.visible)
28449
- .map(visible => {
28450
- switch (typeof visible) {
28451
- case 'object':
28452
- const value = watch(visible.ref);
28453
- return option(visible.test).map(test => test(value, index)).getOrElse(value);
28454
- case 'boolean':
28455
- return visible;
28456
- default:
28457
- return true;
28458
- }
28459
- })
28460
- .getOrElse(true);
28461
- return [...acc, { step, visibleStep, entry }];
28431
+ return [...acc, { step, entry }];
28462
28432
  }, []);
28463
- const bordered = computedSandF.filter(x => x.visibleStep).length >= 1 && step.label !== null;
28433
+ const bordered = computedSandF.length >= 1 && step.label !== null;
28464
28434
  return (React__default.createElement("div", { className: classNames({ ['mrf-nestedform__border']: bordered, ['mrf-border__error']: !!errorDisplayed }), style: { position: 'relative' } },
28465
28435
  !!step.collapsable && schemaAndFlow.flow.length > 1 && collapsed &&
28466
28436
  React__default.createElement(ChevronDown, { size: 30, className: 'mrf-cursor_pointer', style: { position: 'absolute', top: -35, right: 0, zIndex: 100 }, strokeWidth: "2", onClick: () => setCollapsed(!collapsed) }),
28467
28437
  !!step.collapsable && schemaAndFlow.flow.length > 1 && !collapsed &&
28468
28438
  React__default.createElement(ChevronUp, { size: 30, className: 'mrf-cursor_pointer', style: { position: 'absolute', top: -35, right: 0, zIndex: 100 }, strokeWidth: "2", onClick: () => setCollapsed(!collapsed) }),
28469
- computedSandF.map(({ step, visibleStep, entry }, idx) => {
28439
+ computedSandF.map(({ step, entry }, idx) => {
28470
28440
  if (!step && typeof entry === 'string') {
28471
28441
  console.error(`no step found for the entry "${entry}" in the given schema. Your form might not work properly. Please fix it`);
28472
28442
  return null;
@@ -28479,30 +28449,12 @@ const NestedForm = ({ schema, flow, parent, inputWrapper, maybeCustomHttpClient,
28479
28449
  console.error(`no step found for the entry "${en}" in the given schema. Your form might not work properly. Please fix it`);
28480
28450
  return null;
28481
28451
  }
28482
- const visibleStep = option(stp)
28483
- .map(s => s.visible)
28484
- .map(visible => {
28485
- let value;
28486
- switch (typeof visible) {
28487
- case 'object':
28488
- value = getValues(visible.ref);
28489
- return option(visible.test).map(test => test(value, index)).getOrElse(value);
28490
- case 'boolean':
28491
- return visible;
28492
- default:
28493
- return true;
28494
- }
28495
- })
28496
- .getOrElse(true);
28497
- if (!visibleStep) {
28498
- return null;
28499
- }
28500
- return (React__default.createElement(BasicWrapper, { key: `collapse-${en}-${entryIdx}`, entry: en, functionalProperty: functionalProperty, label: (step === null || step === void 0 ? void 0 : step.label) === null ? null : (step === null || step === void 0 ? void 0 : step.label) || entry, help: stp === null || stp === void 0 ? void 0 : stp.help, render: inputWrapper },
28452
+ return (React__default.createElement(BasicWrapper, { key: `collapse-${en}-${entryIdx}`, entry: en, functionalProperty: functionalProperty, step: stp, render: inputWrapper },
28501
28453
  React__default.createElement(Step, { entry: en, step: stp, schema: schema, inputWrapper: inputWrapper, httpClient: maybeCustomHttpClient, defaultValue: stp === null || stp === void 0 ? void 0 : stp.defaultValue, functionalProperty: functionalProperty })));
28502
28454
  }));
28503
28455
  // TODO return collapse, then entry will always be a string in below return
28504
28456
  }
28505
- return (React__default.createElement(BasicWrapper, { key: `${entry}.${idx}`, className: classNames({ ['mrf-display__none']: (collapsed && !step.visibleOnCollapse) || !visibleStep }), entry: `${parent}.${entry}`, functionalProperty: functionalProperty, label: (step === null || step === void 0 ? void 0 : step.label) === null ? null : (step === null || step === void 0 ? void 0 : step.label) || entry, help: step.help, render: inputWrapper },
28457
+ return (React__default.createElement(BasicWrapper, { key: `${entry}.${idx}`, className: classNames({ ['mrf-display__none']: (collapsed && !step.visibleOnCollapse) }), entry: `${parent}.${entry}`, functionalProperty: functionalProperty, step: step, render: inputWrapper },
28506
28458
  React__default.createElement(Step, { key: `step.${entry}.${idx}`, entry: `${parent}.${entry}`, realEntry: entry, step: schemaAndFlow.schema[entry], parent: parent, schema: schemaAndFlow.schema, inputWrapper: inputWrapper, httpClient: maybeCustomHttpClient, defaultValue: value && value[entry], functionalProperty: functionalProperty })));
28507
28459
  })));
28508
28460
  };
package/lib/index.d.ts ADDED
@@ -0,0 +1,358 @@
1
+ /// <reference types="react" />
2
+ import * as yup_lib_types from 'yup/lib/types';
3
+ import * as yup from 'yup';
4
+ import { AnySchema, StringSchema, NumberSchema, ArraySchema, TestFunction } from 'yup';
5
+ import Reference from 'yup/lib/Reference';
6
+ import * as yup_lib_object from 'yup/lib/object';
7
+ import React from 'react';
8
+
9
+ declare const type: {
10
+ readonly string: "string";
11
+ readonly number: "number";
12
+ readonly bool: "bool";
13
+ readonly date: "date";
14
+ readonly object: "object";
15
+ readonly file: "file";
16
+ readonly json: "json";
17
+ };
18
+ declare let typeValues: ("string" | "number" | "object" | "json" | "bool" | "date" | "file")[];
19
+ declare type Type = typeof typeValues[number];
20
+
21
+ declare const format: {
22
+ readonly array: "array";
23
+ readonly select: "select";
24
+ readonly code: "code";
25
+ readonly markdown: "markdown";
26
+ readonly text: "textarea";
27
+ readonly textarea: "textarea";
28
+ readonly email: "email";
29
+ readonly password: "password";
30
+ readonly hidden: "hidden";
31
+ readonly form: "form";
32
+ readonly buttonsSelect: "buttons";
33
+ readonly singleLineCode: "singleLineCode";
34
+ };
35
+ declare let formatValues: ("markdown" | "email" | "array" | "select" | "code" | "textarea" | "password" | "hidden" | "form" | "buttons" | "singleLineCode")[];
36
+ declare type Format = typeof formatValues[number];
37
+
38
+ declare type Constraint = (resolver: any, key: string, dependencies: any[]) => AnySchema;
39
+ declare type NumberRef = Reference<number> | number;
40
+ declare const required: (message?: string) => (r: AnySchema) => any;
41
+ declare const url: (message?: string) => (r: StringSchema) => yup.StringSchema<string | undefined, yup_lib_types.AnyObject, string | undefined>;
42
+ declare const email: (message?: string) => (r: StringSchema) => yup.StringSchema<string | undefined, yup_lib_types.AnyObject, string | undefined>;
43
+ declare const uuid: (message?: string) => (r: StringSchema) => yup.StringSchema<string | undefined, yup_lib_types.AnyObject, string | undefined>;
44
+ declare const matches: (regexp?: RegExp, message?: string) => (r: StringSchema) => yup.StringSchema<string | undefined, yup_lib_types.AnyObject, string | undefined>;
45
+ declare const min: (ref: NumberRef, message?: string) => (r: StringSchema | NumberSchema) => yup.StringSchema<string | undefined, yup_lib_types.AnyObject, string | undefined> | yup.NumberSchema<number | undefined, yup_lib_types.AnyObject, number | undefined>;
46
+ declare const max: (ref: NumberRef, message?: string) => (r: StringSchema | NumberSchema) => yup.StringSchema<string | undefined, yup_lib_types.AnyObject, string | undefined> | yup.NumberSchema<number | undefined, yup_lib_types.AnyObject, number | undefined>;
47
+ declare const positive: (message?: string) => (r: NumberSchema) => yup.NumberSchema<number | undefined, yup_lib_types.AnyObject, number | undefined>;
48
+ declare const negative: (message?: string) => (r: NumberSchema) => yup.NumberSchema<number | undefined, yup_lib_types.AnyObject, number | undefined>;
49
+ declare const integer: (message?: string) => (r: NumberSchema) => yup.NumberSchema<number | undefined, yup_lib_types.AnyObject, number | undefined>;
50
+ declare const lessThan: (ref: NumberRef, message?: string) => (r: NumberSchema, key: any, dependencies: Array<[any, Reference]>) => yup.NumberSchema<number | undefined, yup_lib_types.AnyObject, number | undefined>;
51
+ declare const moreThan: (ref: NumberRef, message?: string) => (r: NumberSchema, key: any, dependencies: Array<[any, Reference]>) => yup.NumberSchema<number | undefined, yup_lib_types.AnyObject, number | undefined>;
52
+ declare const length: (ref: NumberRef, message?: string) => (r: ArraySchema<any>) => yup.ArraySchema<any, yup_lib_types.AnyObject, any[] | undefined, any[] | undefined>;
53
+ declare const supportedFormat: (arrayOfValues: string[], message?: string) => (r: AnySchema) => yup.AnySchema<any, any, any>;
54
+ declare const unsupportedFormat: (arrayOfValues: string[], message?: string) => (r: AnySchema) => yup.AnySchema<any, any, any>;
55
+ declare const maxSize: (ref: Reference, message?: string) => (r: AnySchema) => yup.AnySchema<any, any, any>;
56
+ declare const test: (name: string, message: string | undefined, test: TestFunction<any, object>) => (r: AnySchema) => yup.AnySchema<any, any, any>;
57
+ declare const when: (ref: string, test: TestFunction<any, object>, then?: Constraint[], otherwise?: Constraint[]) => (r: AnySchema, key: string, dependencies: any[]) => yup.AnySchema<any, any, any>;
58
+ declare const oneOf: (arrayOfValues: any[], message?: string) => (r: AnySchema) => yup.AnySchema<any, any, any>;
59
+ declare const blacklist: (arrayOfValues: any[], message?: string) => (r: AnySchema) => yup.AnySchema<any, any, any>;
60
+ declare const ref: (ref: string) => Reference<unknown>;
61
+ declare const jsonConstraints: Record<string, any>;
62
+ declare type TConstraintType = keyof typeof jsonConstraints;
63
+
64
+ type constraints_d_Constraint = Constraint;
65
+ declare const constraints_d_required: typeof required;
66
+ declare const constraints_d_url: typeof url;
67
+ declare const constraints_d_email: typeof email;
68
+ declare const constraints_d_uuid: typeof uuid;
69
+ declare const constraints_d_matches: typeof matches;
70
+ declare const constraints_d_min: typeof min;
71
+ declare const constraints_d_max: typeof max;
72
+ declare const constraints_d_positive: typeof positive;
73
+ declare const constraints_d_negative: typeof negative;
74
+ declare const constraints_d_integer: typeof integer;
75
+ declare const constraints_d_lessThan: typeof lessThan;
76
+ declare const constraints_d_moreThan: typeof moreThan;
77
+ declare const constraints_d_length: typeof length;
78
+ declare const constraints_d_supportedFormat: typeof supportedFormat;
79
+ declare const constraints_d_unsupportedFormat: typeof unsupportedFormat;
80
+ declare const constraints_d_maxSize: typeof maxSize;
81
+ declare const constraints_d_test: typeof test;
82
+ declare const constraints_d_when: typeof when;
83
+ declare const constraints_d_oneOf: typeof oneOf;
84
+ declare const constraints_d_blacklist: typeof blacklist;
85
+ declare const constraints_d_ref: typeof ref;
86
+ declare const constraints_d_jsonConstraints: typeof jsonConstraints;
87
+ type constraints_d_TConstraintType = TConstraintType;
88
+ declare namespace constraints_d {
89
+ export {
90
+ constraints_d_Constraint as Constraint,
91
+ constraints_d_required as required,
92
+ constraints_d_url as url,
93
+ constraints_d_email as email,
94
+ constraints_d_uuid as uuid,
95
+ constraints_d_matches as matches,
96
+ constraints_d_min as min,
97
+ constraints_d_max as max,
98
+ constraints_d_positive as positive,
99
+ constraints_d_negative as negative,
100
+ constraints_d_integer as integer,
101
+ constraints_d_lessThan as lessThan,
102
+ constraints_d_moreThan as moreThan,
103
+ constraints_d_length as length,
104
+ constraints_d_supportedFormat as supportedFormat,
105
+ constraints_d_unsupportedFormat as unsupportedFormat,
106
+ constraints_d_maxSize as maxSize,
107
+ constraints_d_test as test,
108
+ constraints_d_when as when,
109
+ constraints_d_oneOf as oneOf,
110
+ constraints_d_blacklist as blacklist,
111
+ constraints_d_ref as ref,
112
+ constraints_d_jsonConstraints as jsonConstraints,
113
+ constraints_d_TConstraintType as TConstraintType,
114
+ };
115
+ }
116
+
117
+ interface PropType {
118
+ onChange?: (v: boolean) => void;
119
+ value?: boolean;
120
+ readOnly?: boolean;
121
+ }
122
+ declare const BooleanInput: React.ForwardRefExoticComponent<PropType & React.RefAttributes<HTMLInputElement>>;
123
+
124
+ declare const Collapse: (props: {
125
+ initCollapsed?: boolean;
126
+ collapsed?: boolean;
127
+ errored: boolean;
128
+ label?: React.ReactNode;
129
+ inline?: any;
130
+ children: React.ReactNode;
131
+ lineEnd?: boolean;
132
+ }) => JSX.Element;
133
+
134
+ declare type SelectOption = {
135
+ label: string;
136
+ value: any;
137
+ };
138
+ declare const SelectInput: <T extends {
139
+ [x: string]: any;
140
+ }>(props: {
141
+ possibleValues: T[];
142
+ transformer?: {
143
+ label: string;
144
+ value: string;
145
+ } | ((v: T) => SelectOption) | undefined;
146
+ value?: any;
147
+ defaultValue?: any;
148
+ isMulti?: boolean | undefined;
149
+ optionsFrom: string | Promise<T[]> | ((param: {
150
+ rawValues: object;
151
+ value: any;
152
+ }) => string | Promise<T[]>);
153
+ fetchCondition?: (() => boolean) | undefined;
154
+ id?: string | undefined;
155
+ httpClient?: ((url: string, method: string) => Promise<Response>) | undefined;
156
+ onChange?: ((options: any[] | any) => void) | undefined;
157
+ onCreateOption?: ((option: string) => T) | undefined;
158
+ buttons: boolean;
159
+ disabled?: boolean | undefined;
160
+ createOption?: boolean | undefined;
161
+ label?: string | undefined;
162
+ placeholder?: React.ReactNode;
163
+ className: string;
164
+ }) => JSX.Element;
165
+
166
+ declare type InternalState = {
167
+ [x: string]: {
168
+ key: string;
169
+ value: any;
170
+ };
171
+ };
172
+ declare const ObjectInput: (props: {
173
+ value?: object | undefined;
174
+ onChange?: ((value: InternalState) => void) | undefined;
175
+ defaultKeyValue?: {
176
+ key: string;
177
+ value: string;
178
+ } | undefined;
179
+ className: string;
180
+ disabled?: boolean | undefined;
181
+ placeholderKey?: string | undefined;
182
+ placeholderValue?: string | undefined;
183
+ }) => JSX.Element;
184
+
185
+ declare type LanguageMode = "javascript" | "css" | "json" | "html" | "markdown";
186
+
187
+ declare function CodeInput({ onChange, value, mode, tabSize, readOnly, showLinesNumber, highlightLine, themeStyle, setRef }: {
188
+ onChange?: (v: string) => void;
189
+ value?: string;
190
+ mode?: LanguageMode;
191
+ tabSize?: number;
192
+ readOnly?: boolean;
193
+ showLinesNumber?: boolean;
194
+ highlightLine?: boolean;
195
+ themeStyle?: {
196
+ height: string;
197
+ minHeight: string;
198
+ maxHeight: string;
199
+ width: string;
200
+ minWidth: string;
201
+ maxWidth: string;
202
+ };
203
+ setRef?: (editor: any) => void;
204
+ }): JSX.Element;
205
+
206
+ declare const MarkdownInput: (props: {
207
+ value?: string | undefined;
208
+ preview?: boolean | undefined;
209
+ className: string;
210
+ readOnly?: boolean | undefined;
211
+ onChange?: ((value: string) => void) | undefined;
212
+ }) => JSX.Element;
213
+
214
+ declare function SingleLineCode({ onChange, value, mode, tabSize, readOnly, showLinesNumber, highlightLine, themeStyle }: {
215
+ onChange?: (newValue: string) => void;
216
+ value?: string;
217
+ mode?: LanguageMode;
218
+ tabSize?: number;
219
+ readOnly?: boolean;
220
+ showLinesNumber?: boolean;
221
+ highlightLine?: boolean;
222
+ themeStyle?: {
223
+ height: string;
224
+ minHeight: string;
225
+ maxHeight: string;
226
+ width: string;
227
+ minWidth: string;
228
+ maxWidth: string;
229
+ };
230
+ }): JSX.Element;
231
+
232
+ interface OptionActionItem {
233
+ display?: boolean;
234
+ label?: string;
235
+ }
236
+ interface OptionActions {
237
+ reset?: OptionActionItem;
238
+ submit?: OptionActionItem;
239
+ }
240
+ declare type HttpClient = (url: string, method: string) => Promise<Response>;
241
+ interface Option {
242
+ httpClient?: HttpClient;
243
+ watch?: boolean | ((param: any) => void);
244
+ autosubmit?: boolean;
245
+ actions?: OptionActions;
246
+ showErrorsOnStart?: boolean;
247
+ }
248
+ interface Schema {
249
+ [key: string]: SchemaEntry;
250
+ }
251
+ declare type SchemaRenderType = ({ rawValues, value, onChange, error, setValue, parent }: {
252
+ rawValues?: any;
253
+ value?: any;
254
+ onChange?: (param: object) => void;
255
+ error?: boolean;
256
+ parent?: string;
257
+ setValue?: (data: any) => void;
258
+ }) => JSX.Element;
259
+ interface ConditionnalSchemaElement {
260
+ default?: boolean;
261
+ condition?: ({ rawValues, ref }: {
262
+ rawValues: {
263
+ [x: string]: any;
264
+ };
265
+ ref: any;
266
+ }) => boolean | any;
267
+ schema: Schema;
268
+ flow: Array<FlowObject | string>;
269
+ }
270
+ interface ConditionnalSchema {
271
+ ref: string;
272
+ switch: ConditionnalSchemaElement[];
273
+ }
274
+ interface SchemaEntry {
275
+ schema?: Schema;
276
+ type: Type;
277
+ format?: Format;
278
+ array?: boolean;
279
+ createOption?: boolean;
280
+ onCreateOption?: (option: string) => any;
281
+ isMulti?: boolean;
282
+ defaultKeyValue?: object;
283
+ visible?: boolean | {
284
+ ref: string;
285
+ test: (b: any, idx?: number) => boolean;
286
+ };
287
+ disabled?: boolean | ((prop: {
288
+ rawValues: {
289
+ [x: string]: any;
290
+ };
291
+ value: any;
292
+ }) => boolean);
293
+ label?: React.ReactNode | ((prop: {
294
+ rawValues: {
295
+ [x: string]: any;
296
+ };
297
+ value: any;
298
+ }) => React.ReactNode);
299
+ placeholder?: string;
300
+ defaultValue?: any;
301
+ help?: string;
302
+ className?: string;
303
+ style?: object;
304
+ onChange?: (param: object) => void;
305
+ render: SchemaRenderType;
306
+ itemRender?: SchemaRenderType;
307
+ props: object;
308
+ options: Array<any | {
309
+ label: string;
310
+ value: any;
311
+ }>;
312
+ optionsFrom: string;
313
+ transformer: ((v: any) => SelectOption) | {
314
+ label: string;
315
+ value: string;
316
+ };
317
+ conditionalSchema: ConditionnalSchema;
318
+ constraints: Array<Constraint | {
319
+ type: TConstraintType;
320
+ message?: string;
321
+ }>;
322
+ flow: Array<string | FlowObject>;
323
+ onAfterChange?: (obj: {
324
+ entry: string;
325
+ value: object;
326
+ rawValues: object;
327
+ previousValue?: object;
328
+ getValue: (entry: string) => any;
329
+ setValue: (entry: string, value: any) => void;
330
+ onChange: (v: any) => void;
331
+ }) => void;
332
+ visibleOnCollapse?: boolean;
333
+ addableDefaultValue: any;
334
+ collapsed?: boolean;
335
+ collapsable?: boolean;
336
+ }
337
+ interface FlowObject {
338
+ label: string;
339
+ flow: Flow;
340
+ collapse: boolean;
341
+ }
342
+ declare type Flow = Array<string | FlowObject>;
343
+ declare const validate: (flow: string[], schema: Schema, value: object) => Promise<yup_lib_object.AssertsShape<yup_lib_object.Assign<yup_lib_object.ObjectShape, yup_lib_object.ObjectShape>>>;
344
+ declare const Form: React.ForwardRefExoticComponent<{
345
+ schema: Schema;
346
+ flow: Array<string | FlowObject>;
347
+ value?: object | undefined;
348
+ inputWrapper?: ((props: object) => JSX.Element) | undefined;
349
+ onSubmit: (obj: object) => void;
350
+ onError?: (() => void) | undefined;
351
+ footer?: ((props: object) => JSX.Element) | undefined;
352
+ style?: object | undefined;
353
+ className?: string | undefined;
354
+ options?: Option | undefined;
355
+ nostyle: boolean;
356
+ } & React.RefAttributes<unknown>>;
357
+
358
+ export { BooleanInput, CodeInput, Collapse, ConditionnalSchema, Flow, Form, Format, MarkdownInput, ObjectInput, Schema, SchemaEntry, SelectInput, SelectOption, SingleLineCode, Type, constraints_d as constraints, format, type, validate };
package/lib/index.js CHANGED
@@ -27993,27 +27993,46 @@ const usePrevious = (value) => {
27993
27993
  // Return previous value (happens before update in useEffect above)
27994
27994
  return ref.current;
27995
27995
  };
27996
- const BasicWrapper = ({ entry, className, help, children, render, functionalProperty, label }) => {
27996
+ const BasicWrapper = ({ entry, children, render, functionalProperty, step }) => {
27997
+ const { formState, watch } = reactHookForm.useFormContext();
27998
+ console.debug({ entry, step });
27997
27999
  if (typeof entry === 'object') {
27998
28000
  return children;
27999
28001
  }
28000
- const computedLabel = functionalProperty(entry, label === null ? null : label || entry);
28002
+ const visibleStep = option(step)
28003
+ .map(s => s.visible)
28004
+ .map(visible => {
28005
+ let value;
28006
+ switch (typeof visible) {
28007
+ case 'object':
28008
+ value = watch(visible.ref);
28009
+ return option(visible.test).map(test => test(value)).getOrElse(value);
28010
+ case 'boolean':
28011
+ return visible;
28012
+ default:
28013
+ return true;
28014
+ }
28015
+ })
28016
+ .getOrElse(true);
28017
+ if (!visibleStep) {
28018
+ return null;
28019
+ }
28020
+ const computedLabel = functionalProperty(entry, (step === null || step === void 0 ? void 0 : step.label) === null ? null : (step === null || step === void 0 ? void 0 : step.label) || entry);
28001
28021
  const id = uuid$1.v4();
28002
- const { formState } = reactHookForm.useFormContext();
28003
28022
  // FIXME not sure it works as intended with more two or more parts
28004
28023
  const error = entry.split('.').reduce((acc, curr) => acc && acc[curr], formState.errors);
28005
28024
  const isDirty = entry.split('.').reduce((acc, curr) => acc && acc[curr], formState.dirtyFields);
28006
28025
  const isTouched = entry.split('.').reduce((acc, curr) => acc && acc[curr], formState.touchedFields);
28007
28026
  const errorDisplayed = formState.isSubmitted || isDirty || isTouched;
28008
28027
  if (render) {
28009
- return render({ entry, label: computedLabel, error, help, children });
28028
+ return render({ entry, label: computedLabel, error, help: step === null || step === void 0 ? void 0 : step.help, children });
28010
28029
  }
28011
28030
  return (React__default["default"].createElement("div", { className: 'mrf-mt_10', style: { position: 'relative' } },
28012
28031
  computedLabel && React__default["default"].createElement("label", { className: 'mrf-flex mrf-ai_center mrf-mb_5', htmlFor: entry },
28013
28032
  React__default["default"].createElement("span", null, computedLabel),
28014
- help && React__default["default"].createElement(React__default["default"].Fragment, null,
28033
+ (step === null || step === void 0 ? void 0 : step.help) && React__default["default"].createElement(React__default["default"].Fragment, null,
28015
28034
  React__default["default"].createElement(ReactToolTip__default["default"], { html: true, place: 'bottom', id: id }),
28016
- React__default["default"].createElement("span", { className: 'mrf-flex mrf-ai_center', "data-html": true, "data-tip": help, "data-for": id },
28035
+ React__default["default"].createElement("span", { className: 'mrf-flex mrf-ai_center', "data-html": true, "data-tip": step === null || step === void 0 ? void 0 : step.help, "data-for": id },
28017
28036
  React__default["default"].createElement(reactFeather.HelpCircle, { style: { color: 'gray', width: 17, marginLeft: '.5rem', cursor: 'help' } })))),
28018
28037
  children,
28019
28038
  error && React__default["default"].createElement("div", { className: classNames__default["default"]('mrf-feedback', { ['mrf-txt_red']: !!errorDisplayed }) }, error.message)));
@@ -28202,6 +28221,7 @@ const Form = React__default["default"].forwardRef(function Form({ schema, flow,
28202
28221
  trigger,
28203
28222
  methods: Object.assign(Object.assign({}, methods), { data: () => cleanOutputArray(getValues(), schema) })
28204
28223
  }));
28224
+ console.debug({ formFlow });
28205
28225
  return (React__default["default"].createElement(reactHookForm.FormProvider, Object.assign({}, methods),
28206
28226
  React__default["default"].createElement(Watcher, { options: options, control: methods.control, schema: schema, onSubmit: onSubmit, handleSubmit: handleSubmit }),
28207
28227
  React__default["default"].createElement("form", { className: className || `mrf-pr_15 mrf-w_100`, onSubmit: handleSubmit(data => {
@@ -28214,25 +28234,7 @@ const Form = React__default["default"].forwardRef(function Form({ schema, flow,
28214
28234
  console.error(`no step found for the entry "${entry}" in the given schema. Your form might not work properly. Please fix it`);
28215
28235
  return null;
28216
28236
  }
28217
- const visibleStep = option(step)
28218
- .map((s) => s.visible)
28219
- .map((visible) => {
28220
- let value;
28221
- switch (typeof visible) {
28222
- case 'object':
28223
- value = getValues(visible.ref);
28224
- return option(visible.test).map(test => test(value, idx)).getOrElse(value);
28225
- case 'boolean':
28226
- return visible;
28227
- default:
28228
- return true;
28229
- }
28230
- })
28231
- .getOrElse(true);
28232
- if (!visibleStep) {
28233
- return null;
28234
- }
28235
- return (React__default["default"].createElement(BasicWrapper, { key: `${entry}-${idx}`, entry: entry, functionalProperty: functionalProperty, label: step === null || step === void 0 ? void 0 : step.label, help: step === null || step === void 0 ? void 0 : step.help, render: inputWrapper },
28237
+ return (React__default["default"].createElement(BasicWrapper, { key: `${entry}-${idx}`, entry: entry, functionalProperty: functionalProperty, render: inputWrapper, step: step },
28236
28238
  React__default["default"].createElement(Step, { key: idx, entry: entry, step: step, schema: schema, inputWrapper: inputWrapper, httpClient: maybeCustomHttpClient, functionalProperty: functionalProperty })));
28237
28239
  }),
28238
28240
  React__default["default"].createElement(Footer, { render: footer, reset: () => reset(defaultValues), valid: handleSubmit(data => onSubmit(cleanOutputArray(data, schema)), onError), actions: options.actions }))));
@@ -28259,25 +28261,7 @@ const Step = (props) => {
28259
28261
  console.error(`no step found for the entry "${en}" in the given schema. Your form might not work properly. Please fix it`);
28260
28262
  return null;
28261
28263
  }
28262
- const visibleStep = option(stp)
28263
- .map(s => s.visible)
28264
- .map(visible => {
28265
- let value;
28266
- switch (typeof visible) {
28267
- case 'object':
28268
- value = getValues(visible.ref);
28269
- return option(visible.test).map(test => test(value, index)).getOrElse(value);
28270
- case 'boolean':
28271
- return visible;
28272
- default:
28273
- return true;
28274
- }
28275
- })
28276
- .getOrElse(true);
28277
- if (!visibleStep) {
28278
- return null;
28279
- }
28280
- return (React__default["default"].createElement(BasicWrapper, { key: `collapse-${en}-${entryIdx}`, entry: en, functionalProperty: functionalProperty, label: stp === null || stp === void 0 ? void 0 : stp.label, help: stp === null || stp === void 0 ? void 0 : stp.help, render: inputWrapper },
28264
+ return (React__default["default"].createElement(BasicWrapper, { key: `collapse-${en}-${entryIdx}`, entry: en, functionalProperty: functionalProperty, step: stp, render: inputWrapper },
28281
28265
  React__default["default"].createElement(Step, { entry: en, step: stp, schema: schema, inputWrapper: inputWrapper, httpClient: httpClient, defaultValue: stp === null || stp === void 0 ? void 0 : stp.defaultValue, functionalProperty: functionalProperty })));
28282
28266
  })));
28283
28267
  }
@@ -28479,29 +28463,15 @@ const NestedForm = ({ schema, flow, parent, inputWrapper, maybeCustomHttpClient,
28479
28463
  }, [schemaAndFlow.schema]);
28480
28464
  const computedSandF = schemaAndFlow.flow.reduce((acc, entry) => {
28481
28465
  const step = (typeof entry === "string") ? schemaAndFlow.schema[entry] : schemaAndFlow.schema[entry.label];
28482
- const visibleStep = option(step)
28483
- .map(s => s.visible)
28484
- .map(visible => {
28485
- switch (typeof visible) {
28486
- case 'object':
28487
- const value = watch(visible.ref);
28488
- return option(visible.test).map(test => test(value, index)).getOrElse(value);
28489
- case 'boolean':
28490
- return visible;
28491
- default:
28492
- return true;
28493
- }
28494
- })
28495
- .getOrElse(true);
28496
- return [...acc, { step, visibleStep, entry }];
28466
+ return [...acc, { step, entry }];
28497
28467
  }, []);
28498
- const bordered = computedSandF.filter(x => x.visibleStep).length >= 1 && step.label !== null;
28468
+ const bordered = computedSandF.length >= 1 && step.label !== null;
28499
28469
  return (React__default["default"].createElement("div", { className: classNames__default["default"]({ ['mrf-nestedform__border']: bordered, ['mrf-border__error']: !!errorDisplayed }), style: { position: 'relative' } },
28500
28470
  !!step.collapsable && schemaAndFlow.flow.length > 1 && collapsed &&
28501
28471
  React__default["default"].createElement(reactFeather.ChevronDown, { size: 30, className: 'mrf-cursor_pointer', style: { position: 'absolute', top: -35, right: 0, zIndex: 100 }, strokeWidth: "2", onClick: () => setCollapsed(!collapsed) }),
28502
28472
  !!step.collapsable && schemaAndFlow.flow.length > 1 && !collapsed &&
28503
28473
  React__default["default"].createElement(reactFeather.ChevronUp, { size: 30, className: 'mrf-cursor_pointer', style: { position: 'absolute', top: -35, right: 0, zIndex: 100 }, strokeWidth: "2", onClick: () => setCollapsed(!collapsed) }),
28504
- computedSandF.map(({ step, visibleStep, entry }, idx) => {
28474
+ computedSandF.map(({ step, entry }, idx) => {
28505
28475
  if (!step && typeof entry === 'string') {
28506
28476
  console.error(`no step found for the entry "${entry}" in the given schema. Your form might not work properly. Please fix it`);
28507
28477
  return null;
@@ -28514,30 +28484,12 @@ const NestedForm = ({ schema, flow, parent, inputWrapper, maybeCustomHttpClient,
28514
28484
  console.error(`no step found for the entry "${en}" in the given schema. Your form might not work properly. Please fix it`);
28515
28485
  return null;
28516
28486
  }
28517
- const visibleStep = option(stp)
28518
- .map(s => s.visible)
28519
- .map(visible => {
28520
- let value;
28521
- switch (typeof visible) {
28522
- case 'object':
28523
- value = getValues(visible.ref);
28524
- return option(visible.test).map(test => test(value, index)).getOrElse(value);
28525
- case 'boolean':
28526
- return visible;
28527
- default:
28528
- return true;
28529
- }
28530
- })
28531
- .getOrElse(true);
28532
- if (!visibleStep) {
28533
- return null;
28534
- }
28535
- return (React__default["default"].createElement(BasicWrapper, { key: `collapse-${en}-${entryIdx}`, entry: en, functionalProperty: functionalProperty, label: (step === null || step === void 0 ? void 0 : step.label) === null ? null : (step === null || step === void 0 ? void 0 : step.label) || entry, help: stp === null || stp === void 0 ? void 0 : stp.help, render: inputWrapper },
28487
+ return (React__default["default"].createElement(BasicWrapper, { key: `collapse-${en}-${entryIdx}`, entry: en, functionalProperty: functionalProperty, step: stp, render: inputWrapper },
28536
28488
  React__default["default"].createElement(Step, { entry: en, step: stp, schema: schema, inputWrapper: inputWrapper, httpClient: maybeCustomHttpClient, defaultValue: stp === null || stp === void 0 ? void 0 : stp.defaultValue, functionalProperty: functionalProperty })));
28537
28489
  }));
28538
28490
  // TODO return collapse, then entry will always be a string in below return
28539
28491
  }
28540
- return (React__default["default"].createElement(BasicWrapper, { key: `${entry}.${idx}`, className: classNames__default["default"]({ ['mrf-display__none']: (collapsed && !step.visibleOnCollapse) || !visibleStep }), entry: `${parent}.${entry}`, functionalProperty: functionalProperty, label: (step === null || step === void 0 ? void 0 : step.label) === null ? null : (step === null || step === void 0 ? void 0 : step.label) || entry, help: step.help, render: inputWrapper },
28492
+ return (React__default["default"].createElement(BasicWrapper, { key: `${entry}.${idx}`, className: classNames__default["default"]({ ['mrf-display__none']: (collapsed && !step.visibleOnCollapse) }), entry: `${parent}.${entry}`, functionalProperty: functionalProperty, step: step, render: inputWrapper },
28541
28493
  React__default["default"].createElement(Step, { key: `step.${entry}.${idx}`, entry: `${parent}.${entry}`, realEntry: entry, step: schemaAndFlow.schema[entry], parent: parent, schema: schemaAndFlow.schema, inputWrapper: inputWrapper, httpClient: maybeCustomHttpClient, defaultValue: value && value[entry], functionalProperty: functionalProperty })));
28542
28494
  })));
28543
28495
  };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@maif/react-forms",
3
3
  "description": "Build react safe forms as fast as possible",
4
- "version": "1.1.0",
4
+ "version": "1.1.1",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/esm/index.js",
7
- "types": "lib/index.js",
7
+ "types": "lib/index.d.ts",
8
8
  "source": "src/index.ts",
9
9
  "author": "MAIF team",
10
10
  "keywords": [
@@ -32,8 +32,8 @@
32
32
  "README.md"
33
33
  ],
34
34
  "scripts": {
35
- "build": "rollup -c",
36
- "dev": "rollup -c -w",
35
+ "build": "tsc --emitDeclarationOnly && rollup -c",
36
+ "dev": "tsc --emitDeclarationOnly && rollup -c -w",
37
37
  "test": "jest",
38
38
  "tdd": "jest --watch"
39
39
  },
@@ -68,6 +68,13 @@
68
68
  "@rollup/plugin-json": "^4.1.0",
69
69
  "@rollup/plugin-node-resolve": "^13.1.3",
70
70
  "@rollup/plugin-typescript": "^8.3.2",
71
+ "@testing-library/jest-dom": "^5.11.4",
72
+ "@testing-library/react": "^11.1.0",
73
+ "@testing-library/user-event": "^12.1.10",
74
+ "@types/jest": "^26.0.24",
75
+ "@types/node": "^16.3.0",
76
+ "@types/react": "^17.0.14",
77
+ "@types/react-dom": "^17.0.9",
71
78
  "@types/object-hash": "^2.2.1",
72
79
  "@types/showdown": "^2.0.0",
73
80
  "@types/uuid": "^8.3.4",
@@ -87,6 +94,7 @@
87
94
  "rollup-plugin-commonjs": "^10.1.0",
88
95
  "rollup-plugin-copy": "^3.4.0",
89
96
  "rollup-plugin-delete": "^2.0.0",
97
+ "rollup-plugin-dts": "^4.2.2",
90
98
  "rollup-plugin-peer-deps-external": "^2.2.4",
91
99
  "rollup-plugin-scss": "^3.0.0",
92
100
  "rollup-plugin-terser": "^7.0.2",
@@ -107,13 +115,6 @@
107
115
  "@codemirror/theme-one-dark": "^0.19.1",
108
116
  "@fortawesome/fontawesome-free": "^5.15.3",
109
117
  "@hookform/resolvers": "2.4.0",
110
- "@testing-library/jest-dom": "^5.11.4",
111
- "@testing-library/react": "^11.1.0",
112
- "@testing-library/user-event": "^12.1.10",
113
- "@types/jest": "^26.0.24",
114
- "@types/node": "^16.3.0",
115
- "@types/react": "^17.0.14",
116
- "@types/react-dom": "^17.0.9",
117
118
  "classnames": "2.3.0",
118
119
  "fast-deep-equal": "^3.1.3",
119
120
  "highlight.js": "^11.5.1",