|
24 | 24 | */
|
25 | 25 | import range from 'lodash/range';
|
26 | 26 | import React, { useMemo } from 'react';
|
27 |
| -import { ArrayControlProps, composePaths, createDefaultValue, findUISchema } from '@jsonforms/core'; |
| 27 | +import { ArrayControlProps, composePaths, createDefaultValue, findUISchema, Helpers, ControlElement } from '@jsonforms/core'; |
28 | 28 | import { JsonFormsDispatch } from '@jsonforms/react';
|
29 | 29 | import { VanillaRendererProps } from '../../index';
|
30 | 30 |
|
| 31 | +const { convertToValidClassName } = Helpers; |
| 32 | + |
31 | 33 | export const ArrayControl = ({
|
32 | 34 | classNames,
|
33 | 35 | data,
|
34 | 36 | label,
|
35 | 37 | path,
|
36 | 38 | schema,
|
| 39 | + errors, |
37 | 40 | addItem,
|
38 | 41 | uischema,
|
39 | 42 | uischemas,
|
| 43 | + getStyleAsClassName, |
40 | 44 | renderers,
|
41 | 45 | rootSchema
|
42 | 46 | }: ArrayControlProps & VanillaRendererProps) => {
|
| 47 | + |
| 48 | + const controlElement = uischema as ControlElement; |
43 | 49 | const childUiSchema = useMemo(
|
44 | 50 | () => findUISchema(uischemas, schema, uischema.scope, path, undefined, uischema, rootSchema),
|
45 | 51 | [uischemas, schema, uischema.scope, path, uischema, rootSchema]
|
46 | 52 | );
|
| 53 | + const isValid = errors.length === 0; |
| 54 | + const validationClass = getStyleAsClassName('array.control.validation'); |
| 55 | + const divClassNames = [validationClass] |
| 56 | + .concat(isValid ? '' : getStyleAsClassName('array.control.validation.error')) |
| 57 | + .join(' '); |
| 58 | + const buttonClass = getStyleAsClassName('array.control.button'); |
| 59 | + const labelClass = getStyleAsClassName('array.control.label'); |
| 60 | + const controlClass = [ |
| 61 | + getStyleAsClassName('array.control'), |
| 62 | + convertToValidClassName(controlElement.scope) |
| 63 | + ].join(' '); |
| 64 | + |
47 | 65 | return (
|
48 |
| - <div className={classNames.wrapper}> |
49 |
| - <fieldset className={classNames.fieldSet}> |
50 |
| - <legend> |
51 |
| - <button |
52 |
| - className={classNames.button} |
| 66 | + <div className={controlClass}> |
| 67 | + <header> |
| 68 | + <label className={labelClass}>{label}</label> |
| 69 | + <button |
| 70 | + className={buttonClass} |
53 | 71 | onClick={addItem(path, createDefaultValue(schema))}
|
54 |
| - > |
55 |
| - + |
56 |
| - </button> |
57 |
| - <label className={'array.label'}>{label}</label> |
58 |
| - </legend> |
59 |
| - <div className={classNames.children}> |
60 |
| - {data ? ( |
61 |
| - range(0, data.length).map(index => { |
62 |
| - const childPath = composePaths(path, `${index}`); |
63 |
| - |
64 |
| - return ( |
65 |
| - <JsonFormsDispatch |
66 |
| - schema={schema} |
67 |
| - uischema={childUiSchema || uischema} |
68 |
| - path={childPath} |
69 |
| - key={childPath} |
70 |
| - renderers={renderers} |
71 |
| - /> |
72 |
| - ); |
73 |
| - }) |
74 |
| - ) : ( |
75 |
| - <p>No data</p> |
76 |
| - )} |
77 |
| - </div> |
78 |
| - </fieldset> |
| 72 | + >Add to {label} |
| 73 | + </button> |
| 74 | + </header> |
| 75 | + <div className={divClassNames}> |
| 76 | + {errors} |
| 77 | + </div> |
| 78 | + <div className={classNames.children}> |
| 79 | + {data ? ( |
| 80 | + range(0, data.length).map(index => { |
| 81 | + const childPath = composePaths(path, `${index}`); |
| 82 | + return ( |
| 83 | + <JsonFormsDispatch |
| 84 | + schema={schema} |
| 85 | + uischema={childUiSchema || uischema} |
| 86 | + path={childPath} |
| 87 | + key={childPath} |
| 88 | + renderers={renderers} |
| 89 | + /> |
| 90 | + ); |
| 91 | + }) |
| 92 | + ) : ( |
| 93 | + <p>No data</p> |
| 94 | + )} |
| 95 | + </div> |
79 | 96 | </div>
|
80 | 97 | );
|
81 | 98 | };
|
0 commit comments