@maif/react-forms 1.0.54 → 1.0.57
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/README.md +41 -9
- package/lib/esm/index.js +182 -418
- package/lib/index.css +360 -0
- package/lib/index.js +182 -417
- package/package.json +4 -6
package/README.md
CHANGED
|
@@ -18,12 +18,14 @@ yarn add @maif/react-forms
|
|
|
18
18
|
```
|
|
19
19
|
|
|
20
20
|
# Usage
|
|
21
|
-
You must define a form flow (this is just a javascript array which
|
|
21
|
+
You must define a form flow (this is just a javascript array which represents the rendering order of the form fields) and a schema (that defines all fields of your form with types or constraints for example)
|
|
22
|
+
You can import the default stylesheet or use your own style by using default classname or passing a classname in your schema.
|
|
22
23
|
|
|
23
24
|
## example
|
|
24
25
|
|
|
25
26
|
```javascript
|
|
26
|
-
import { Form, type, format, constraints } from 'react-forms'
|
|
27
|
+
import { Form, type, format, constraints } from '@maif/react-forms'
|
|
28
|
+
import '@maif/react-forms/lib/index.css'
|
|
27
29
|
|
|
28
30
|
export const Example = () => {
|
|
29
31
|
const schema = {
|
|
@@ -32,6 +34,7 @@ export const Example = () => {
|
|
|
32
34
|
label: 'age',
|
|
33
35
|
placeholder: 'Your age',
|
|
34
36
|
help: "Just your age",
|
|
37
|
+
className: "input-number",
|
|
35
38
|
constraints: [
|
|
36
39
|
constraints.required("your age is required"),
|
|
37
40
|
constraints.min(18, 'You must be an adult'),
|
|
@@ -64,7 +67,7 @@ export const Example = () => {
|
|
|
64
67
|
## schema properties
|
|
65
68
|
|
|
66
69
|
- **type** (required): the type of value. It provided by the imported object `type`. It could be just string like `string`, `number`, `bool`, `object`, `date` or `file`
|
|
67
|
-
- **format**: Over the type you can display a special format for the field. It provided by the imported object `format` or just a string
|
|
70
|
+
- **format**: Over the type you can display a special format for the field. It is provided by the imported object `format` or just a string
|
|
68
71
|
- `select`: display a [react-select](https://react-select.com/home) field with provided options
|
|
69
72
|
- `buttonsSelect`: display a buttons group, drawn with the same options than the format `select`
|
|
70
73
|
- `code`: if the type is `string`, display a code input (draw with [react-ace](https://github.com/securingsincity/react-ace)) (add a props.setRef to get ace editor ref)
|
|
@@ -138,7 +141,7 @@ export const Example = () => {
|
|
|
138
141
|
switch: [
|
|
139
142
|
{
|
|
140
143
|
default: true,
|
|
141
|
-
condition: ({
|
|
144
|
+
condition: ({rawValues, ref}) => ref === "mammal",
|
|
142
145
|
schema: {
|
|
143
146
|
continent: {
|
|
144
147
|
type: type.string,
|
|
@@ -226,6 +229,7 @@ By default all fields of the form are nullable (they can accept `null` or `undef
|
|
|
226
229
|
- [`constraints.test(name: string, message?:string, test: (val: any) => boolean | Promise<boolean>)`](#constraintstestname-string-messagestring-test-val-any--boolean--promiseboolean)
|
|
227
230
|
- [`constraints.when(ref: string, test: (val: any) => boolean, then: any = [], otherwise: any = [])`](#constraintswhenref-string-test-val-any--boolean-then-any---otherwise-any--)
|
|
228
231
|
- [`constraints.oneOf(arrayOfValues: any[], message?:string)`](#constraintsoneofarrayofvalues-any-messagestring)
|
|
232
|
+
- [`constraints.ref(ref: any)`](#constraintsrefrefany)
|
|
229
233
|
- [string](#string)
|
|
230
234
|
- [string or number](#string-or-number)
|
|
231
235
|
- [number](#number)
|
|
@@ -279,6 +283,7 @@ the following methods works for all type types of value.
|
|
|
279
283
|
|
|
280
284
|
#### `constraints.oneOf(arrayOfValues: any[], message?:string)`
|
|
281
285
|
Whitelist a set of values and display an error under field if the provided value is not contains in this set.
|
|
286
|
+
Values can also be a [`reference`](#constraintsrefrefany) of value
|
|
282
287
|
|
|
283
288
|
```javascript
|
|
284
289
|
constraints.oneOf(['foo', 'bar'], 'not foo or bar :(')
|
|
@@ -287,6 +292,33 @@ the following methods works for all type types of value.
|
|
|
287
292
|
{type: 'oneOf', arrayOfValues: ['foo', 'bar'], message: 'not foo or bar :('}
|
|
288
293
|
```
|
|
289
294
|
|
|
295
|
+
#### `constraints.ref(ref:any)`
|
|
296
|
+
Some constraints accepts reference as property. This methods create a reference to another field.
|
|
297
|
+
Refs are evaluated in the proper order so that the ref value is resolved before the field using the ref (be careful of circular dependencies!).
|
|
298
|
+
|
|
299
|
+
```javascript
|
|
300
|
+
const schema = {
|
|
301
|
+
dad_age: { type: type.number},
|
|
302
|
+
son_age: {
|
|
303
|
+
type: type.number,
|
|
304
|
+
constraints: [
|
|
305
|
+
constraints.lessThan(constraints.ref('dad_age'), 'too old')
|
|
306
|
+
]
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
```
|
|
310
|
+
```javascript
|
|
311
|
+
const schema = {
|
|
312
|
+
dad_age: { type: 'number'},
|
|
313
|
+
son_age: {
|
|
314
|
+
type: 'number',
|
|
315
|
+
constraints: [
|
|
316
|
+
{type: 'lessThan', ref: { ref: 'dad_age' }, message: 'too old !'}
|
|
317
|
+
]
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
```
|
|
321
|
+
|
|
290
322
|
### string
|
|
291
323
|
the following methods works for string values. All methods for [mixed](#mixed) are available.
|
|
292
324
|
|
|
@@ -324,13 +356,13 @@ the following methods works for string or number values. All methods for [mixed]
|
|
|
324
356
|
|
|
325
357
|
|
|
326
358
|
#### `constraints.min(ref: number | Reference<number>, message: string)`
|
|
327
|
-
Set the minimum value allowed and display an error if the provided value (for a number) or the length of the string is bigger.
|
|
359
|
+
Set the minimum value allowed and display an error if the provided value (for a number) or the length of the string is bigger. Provided value can be a number or a [`reference`](#constraintsrefrefany) to a number
|
|
328
360
|
```javascript
|
|
329
361
|
{type: 'min', ref: 1, message: 'too small'}
|
|
330
362
|
```
|
|
331
363
|
|
|
332
364
|
#### `constraints.max(ref: number | Reference<number>, message: string)`
|
|
333
|
-
Set the maximun value allowed and display an error if provided value (for a number) or the length of the string is smaller.
|
|
365
|
+
Set the maximun value allowed and display an error if provided value (for a number) or the length of the string is smaller. value can be a number or a [`reference`](#constraintsrefrefany) to a number
|
|
334
366
|
```javascript
|
|
335
367
|
{type: 'max', ref: 5, message: 'too high'}
|
|
336
368
|
```
|
|
@@ -356,13 +388,13 @@ The value must be aa integer number and display an error if it's not.
|
|
|
356
388
|
```
|
|
357
389
|
|
|
358
390
|
#### `constraints.lessThan(ref: number | Reference<number>, message: string)`
|
|
359
|
-
Set the maximun value allowed and display an error if provided value (for a number) or the length of the string is smaller.
|
|
391
|
+
Set the maximun value allowed and display an error if provided value (for a number) or the length of the string is smaller. value can be a number or a [`reference`](#constraintsrefrefany) to a number
|
|
360
392
|
```javascript
|
|
361
393
|
{type: 'lessThan', ref: 5, message: 'less please'}
|
|
362
394
|
```
|
|
363
395
|
|
|
364
396
|
#### `constraints.moreThan(ref: number | Reference<number>, message: string)`
|
|
365
|
-
Set the minimum value allowed and display an error if provided value (for a number) or the length of the string is bigger.
|
|
397
|
+
Set the minimum value allowed and display an error if provided value (for a number) or the length of the string is bigger. value can be a number or a [`reference`](#constraintsrefrefany) to a number
|
|
366
398
|
```javascript
|
|
367
399
|
{type: 'moreThan', ref: 5, message: 'more please'}
|
|
368
400
|
```
|
|
@@ -371,7 +403,7 @@ Set the minimum value allowed and display an error if provided value (for a numb
|
|
|
371
403
|
the following methods works for basic types if the format is define to `array`. All methods for [mixed](#mixed) are available.
|
|
372
404
|
|
|
373
405
|
#### `constraints.length(value: number | Reference<number>, message?:string)`
|
|
374
|
-
Set the length of the array and display an error if it's different.
|
|
406
|
+
Set the length of the array and display an error if it's different. value can be a number or a [`reference`](#constraintsrefrefany) to a number
|
|
375
407
|
```javascript
|
|
376
408
|
{type: 'length', ref: 5, message: 'this array must have 5 elements'}
|
|
377
409
|
```
|