11import React , { PropTypes } from 'react'
22import './ColorSelector.scss'
33import { SketchPicker } from 'react-color'
4- import _ from 'lodash'
54import { HOC as hoc } from 'formsy-react'
65import { PROJECT_MAX_COLORS } from '../../config/constants'
76import { Icons } from 'appirio-tech-react-components'
@@ -13,15 +12,19 @@ class ColorSelector extends React.Component {
1312
1413 this . state = {
1514 isPickerVisible : false ,
16- newColor : '#fff' ,
17- colors : _ . uniq ( [ ...props . defaultColors , ...props . value ] )
15+ newColor : '#fff'
1816 }
1917 }
2018
2119 render ( ) {
22- const { getValue, name, onChange, setValue} = this . props
23- const value = getValue ( ) || [ ]
24- const { isPickerVisible, colors, newColor} = this . state
20+ const { getValue, name, onChange, setValue, defaultColors } = this . props
21+ const value = getValue ( ) || defaultColors
22+ const { isPickerVisible, newColor} = this . state
23+
24+ const updateNewColorPalette = ( palette ) => {
25+ setValue ( palette )
26+ onChange ( name , palette )
27+ }
2528
2629 const onColorToggle = ( color ) => {
2730 const index = value . indexOf ( color )
@@ -34,8 +37,7 @@ class ColorSelector extends React.Component {
3437 newValue = tmp
3538 }
3639
37- setValue ( newValue )
38- onChange ( name , newValue )
40+ updateNewColorPalette ( newValue )
3941 }
4042
4143 return (
@@ -53,8 +55,8 @@ class ColorSelector extends React.Component {
5355 </ span >
5456 </ a >
5557 ) }
56-
57- { colors . length < PROJECT_MAX_COLORS &&
58+
59+ { value . length < PROJECT_MAX_COLORS &&
5860 < a
5961 href = "javascript:"
6062 onClick = { ( ) => this . setState ( { isPickerVisible : true } ) }
@@ -72,14 +74,11 @@ class ColorSelector extends React.Component {
7274 < button type = "button" className = "tc-btn tc-btn-primary tc-btn-md"
7375 onClick = { ( ) => {
7476 this . setState ( { isPickerVisible : false } )
75- if ( colors . indexOf ( newColor ) === - 1 ) {
76- this . setState ( { colors : [ ... colors , newColor ] , newColor : '#fff' } )
77- } else {
78- this . setState ( { newColor : '#fff' } )
77+ const index = value . indexOf ( this . state . newColor )
78+ if ( index === - 1 ) {
79+ const newValue = [ ... value , newColor ]
80+ updateNewColorPalette ( newValue )
7981 }
80- const newValue = [ ...value , newColor ]
81- setValue ( newValue )
82- onChange ( name , newValue )
8382 } }
8483 > Add</ button >
8584 < button
0 commit comments