|
| 1 | +import React, {Component} from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import tinyColor from 'tinycolor2'; |
| 4 | +import ModalBox from '../containers/ModalBox'; |
| 5 | + |
| 6 | +const tooLightFactor = 0.8; |
| 7 | + |
| 8 | +function tooLight(color) { |
| 9 | + const hslColor = tinyColor(color).toHsl(); |
| 10 | + return hslColor.l > tooLightFactor; |
| 11 | +} |
| 12 | + |
| 13 | +export default class SymbolSelector extends Component { |
| 14 | + constructor(props) { |
| 15 | + super(props); |
| 16 | + this.state = { |
| 17 | + isOpen: false, |
| 18 | + }; |
| 19 | + this.togglePanel = this.togglePanel.bind(this); |
| 20 | + } |
| 21 | + |
| 22 | + shouldComponentUpdate(nextProps, nextState) { |
| 23 | + const {markerColor, borderColor} = this.props; |
| 24 | + const { |
| 25 | + markerColor: nextMarkerColor, |
| 26 | + borderColor: nextBorderColor, |
| 27 | + } = nextProps; |
| 28 | + |
| 29 | + return ( |
| 30 | + this.props.value !== nextProps.value || |
| 31 | + this.state.isOpen !== nextState.isOpen || |
| 32 | + markerColor !== nextMarkerColor || |
| 33 | + borderColor !== nextBorderColor |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + togglePanel() { |
| 38 | + this.setState({isOpen: !this.state.isOpen}); |
| 39 | + } |
| 40 | + |
| 41 | + renderActiveOption() { |
| 42 | + const {markerColor, borderColor, symbolOptions, value} = this.props; |
| 43 | + const currentSymbol = symbolOptions.find(symbol => symbol.value === value); |
| 44 | + if (!currentSymbol) { |
| 45 | + return ( |
| 46 | + <span |
| 47 | + style={{ |
| 48 | + paddingTop: '5px', |
| 49 | + paddingLeft: '15px', |
| 50 | + }} |
| 51 | + > |
| 52 | + {'-'} |
| 53 | + </span> |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + const symbolStyle = { |
| 58 | + stroke: currentSymbol.fill === 'none' ? markerColor : borderColor, |
| 59 | + strokeOpacity: '1', |
| 60 | + strokeWidth: '2px', |
| 61 | + fill: currentSymbol.fill === 'none' ? 'none' : markerColor, |
| 62 | + }; |
| 63 | + |
| 64 | + return ( |
| 65 | + <span> |
| 66 | + <svg width="18" height="18"> |
| 67 | + <g transform="translate(8,8)"> |
| 68 | + <path d={currentSymbol.label} style={symbolStyle} /> |
| 69 | + </g> |
| 70 | + </svg> |
| 71 | + </span> |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + renderOptions() { |
| 76 | + const {markerColor, borderColor, symbolOptions} = this.props; |
| 77 | + return symbolOptions.map(option => { |
| 78 | + const {fill, value, label} = option; |
| 79 | + |
| 80 | + const symbolStyle = { |
| 81 | + stroke: fill === 'none' ? markerColor : borderColor, |
| 82 | + strokeOpacity: '1', |
| 83 | + strokeWidth: '2px', |
| 84 | + fill: fill === 'none' ? 'none' : markerColor, |
| 85 | + }; |
| 86 | + return ( |
| 87 | + <div |
| 88 | + className="symbol-selector__item" |
| 89 | + key={value} |
| 90 | + onClick={this.props.onChange} |
| 91 | + > |
| 92 | + <svg |
| 93 | + width="28" |
| 94 | + height="28" |
| 95 | + className="symbol-selector__symbol" |
| 96 | + data-value={value} |
| 97 | + > |
| 98 | + <g transform="translate(14,14)"> |
| 99 | + <path d={label} style={symbolStyle} /> |
| 100 | + </g> |
| 101 | + </svg> |
| 102 | + </div> |
| 103 | + ); |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + render() { |
| 108 | + const {isOpen} = this.state; |
| 109 | + const {markerColor} = this.props; |
| 110 | + |
| 111 | + // TODO link these colors into theme |
| 112 | + const backgroundColor = tooLight(markerColor) ? '#bec8d9' : 'white'; |
| 113 | + |
| 114 | + return ( |
| 115 | + <div> |
| 116 | + <div className="symbol-selector__toggle" onClick={this.togglePanel}> |
| 117 | + <span className="symbol-selector__toggle_option"> |
| 118 | + {this.renderActiveOption()} |
| 119 | + </span> |
| 120 | + <span className="symbol-selector__toggle__caret"> |
| 121 | + <i className="icon-caret-down" /> |
| 122 | + </span> |
| 123 | + </div> |
| 124 | + {isOpen ? ( |
| 125 | + <ModalBox |
| 126 | + onClose={this.togglePanel} |
| 127 | + backgroundColor={backgroundColor} |
| 128 | + > |
| 129 | + {this.renderOptions()} |
| 130 | + </ModalBox> |
| 131 | + ) : null} |
| 132 | + </div> |
| 133 | + ); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +SymbolSelector.propTypes = { |
| 138 | + markerColor: PropTypes.string, |
| 139 | + borderColor: PropTypes.string, |
| 140 | + value: PropTypes.string, |
| 141 | + onChange: PropTypes.func, |
| 142 | + symbolOptions: PropTypes.array, |
| 143 | +}; |
0 commit comments