@@ -3,16 +3,21 @@ import randomColor from 'randomcolor';
3
3
4
4
import { makeStyles } from '@material-ui/core/styles' ;
5
5
6
+ import { number , useTranslate } from 'react-admin' ;
7
+ import { Field } from 'react-final-form' ;
8
+ import { TextField } from '@material-ui/core' ;
6
9
import ColorPicker from '../../../../../../../components/react-admin/ColorPicker' ;
10
+ import Condition from '../../../../../../../components/react-admin/Condition' ;
7
11
8
12
import styles from './styles' ;
9
13
10
14
const useStyles = makeStyles ( styles ) ;
11
15
12
16
const DEFAULT_MAX_CLASSES = 15 ;
13
17
14
- const ColorListField = ( { value, onChange = ( ) => { } , maxClasses = DEFAULT_MAX_CLASSES } ) => {
18
+ const ColorListField = ( { path , value, onChange = ( ) => { } , maxClasses = DEFAULT_MAX_CLASSES } ) => {
15
19
const classes = useStyles ( ) ;
20
+ const translate = useTranslate ( ) ;
16
21
17
22
const handleColorChange = index => newValue => {
18
23
const newColorList = [ ...value ] ;
@@ -35,13 +40,54 @@ const ColorListField = ({ value, onChange = () => {}, maxClasses = DEFAULT_MAX_C
35
40
return (
36
41
< div className = { classes . colorList } >
37
42
{ ( value || [ ] ) . map ( ( color , index ) => (
38
- < ColorPicker
39
- // eslint-disable-next-line react/no-array-index-key
40
- key = { index }
41
- value = { color }
42
- onChange = { handleColorChange ( index ) }
43
- />
43
+ // eslint-disable-next-line react/no-array-index-key
44
+ < React . Fragment key = { index } >
45
+ < Condition when = { `${ path } .method` } is = "manual" >
46
+ < Field
47
+ name = { `${ path } .boundaries[${ index } ]` }
48
+ validate = { number ( ) }
49
+ parse = { v => Number ( v ) }
50
+ initialValue = ""
51
+ >
52
+ { ( { meta, input : { value : boundValue , onChange : onBoundChange } } ) => (
53
+ < TextField
54
+ label = { translate ( 'datalayer.form.styles.boundary' ) }
55
+ value = { boundValue }
56
+ onChange = { onBoundChange }
57
+ error = { meta . error && meta . touched }
58
+ helperText = { ( meta . error && meta . touched ? meta . error : '' ) }
59
+ />
60
+ ) }
61
+ </ Field >
62
+ </ Condition >
63
+ < ColorPicker
64
+ value = { color }
65
+ onChange = { handleColorChange ( index ) }
66
+ />
67
+ < Condition
68
+ when = { `${ path } .method` }
69
+ is = { v => ( v === 'manual' && index === ( value . length - 1 ) ) }
70
+ >
71
+ < Field
72
+ name = { `${ path } .boundaries[${ value . length } ]` }
73
+ validate = { number ( ) }
74
+ initialValue = ""
75
+ parse = { v => Number ( v ) }
76
+ >
77
+ { ( { meta, input : { value : boundValue , onChange : onBoundChange } } ) => (
78
+ < TextField
79
+ label = { translate ( 'datalayer.form.styles.boundary' ) }
80
+ value = { boundValue }
81
+ onChange = { onBoundChange }
82
+ error = { meta . error && meta . touched }
83
+ helperText = { meta . error && meta . touched ? meta . error : '' }
84
+ />
85
+ ) }
86
+ </ Field >
87
+ </ Condition >
88
+ </ React . Fragment >
44
89
) ) }
90
+
45
91
< button type = "button" className = "action" onClick = { removeColor ( value . length - 1 ) } >
46
92
-
47
93
</ button >
0 commit comments