|
| 1 | +// @flow |
| 2 | +import React, { PureComponent } from 'react' |
| 3 | +import Select from 'react-select' |
| 4 | + |
| 5 | +import SelectorOption from './SelectorOption' |
| 6 | + |
| 7 | +import '../../assets/react-select.css' |
| 8 | + |
| 9 | +import theme from '../../styles/theme' |
| 10 | +import typo from '../../styles/typo' |
| 11 | + |
| 12 | +const cmz = require('cmz') |
| 13 | + |
| 14 | +const cx = { |
| 15 | + select: cmz( |
| 16 | + typo.baseText, |
| 17 | + ` |
| 18 | + & { |
| 19 | + font-size: 18px |
| 20 | + text-rendering: optimizeLegibility |
| 21 | + -webkit-font-smoothing: antialiased |
| 22 | + color: ${theme.typoParagraph} |
| 23 | + border: 1px solid transparent |
| 24 | + } |
| 25 | +
|
| 26 | + & .Select-control { |
| 27 | + display: flex |
| 28 | + border: 1px solid ${theme.lineSilver2} |
| 29 | + height: auto |
| 30 | + padding: 14px 16px |
| 31 | + border-radius: 0 |
| 32 | + cursor: pointer |
| 33 | + } |
| 34 | +
|
| 35 | + &.is-focused .Select-control { |
| 36 | + border: 1px solid ${theme.lineRed} !important |
| 37 | + box-shadow: 0 0 3px ${theme.lineRed} !important |
| 38 | + } |
| 39 | +
|
| 40 | + &.is-disabled:hover { |
| 41 | + border: 1px solid transparent |
| 42 | + box-shadow: none |
| 43 | + } |
| 44 | +
|
| 45 | + & .Select-multi-value-wrapper { |
| 46 | + white-space: nowrap |
| 47 | + flex: 1 |
| 48 | + overflow-x: auto |
| 49 | + overflow-y: hidden |
| 50 | + display: flex |
| 51 | + align-items: center |
| 52 | + height: 18px |
| 53 | + } |
| 54 | +
|
| 55 | + & .Select-placeholder, |
| 56 | + & .Select-control .Select-value { |
| 57 | + padding: 21px 18px !important |
| 58 | + line-height: 1 !important |
| 59 | + white-space: nowrap !important |
| 60 | + overflow: hidden !important |
| 61 | + text-overflow: ellipsis !important |
| 62 | + } |
| 63 | +
|
| 64 | + & .Select-input { |
| 65 | + padding: 0 3px !important |
| 66 | + position: absolute !important |
| 67 | + top: 11px !important |
| 68 | + } |
| 69 | +
|
| 70 | + & .Select-input:focus { |
| 71 | + background: transparent !important |
| 72 | + } |
| 73 | +
|
| 74 | + & .Select-control > *:last-child { |
| 75 | + padding: 0 |
| 76 | + margin-right: -5px |
| 77 | + } |
| 78 | +
|
| 79 | + & .Select-menu-outer { |
| 80 | + top: calc(100% + 5px) |
| 81 | + max-height: 270px |
| 82 | + background: ${theme.baseBrighter} |
| 83 | + border: 1px solid rgba(0, 0, 0, 0.15) |
| 84 | + box-shadow: 0 5px 12px rgba(0, 0, 0, 0.15) |
| 85 | + border-radius: 0 |
| 86 | + } |
| 87 | +
|
| 88 | + & .Select-menu { |
| 89 | + max-height: 270px |
| 90 | + } |
| 91 | +
|
| 92 | + & .Select-option { |
| 93 | + padding: 0 |
| 94 | + border-bottom: 1px solid ${theme.lineSilver2} |
| 95 | + } |
| 96 | + ` |
| 97 | + ) |
| 98 | +} |
| 99 | + |
| 100 | +type Option = { |
| 101 | + value: string | number, |
| 102 | + label: string |
| 103 | +} |
| 104 | + |
| 105 | +type Props = { |
| 106 | + placeholder?: string, |
| 107 | + options?: Array<Option> | null, |
| 108 | + disabled?: boolean, |
| 109 | + clearable?: boolean, |
| 110 | + searchable?: boolean, |
| 111 | + value?: any, |
| 112 | + onChange?: any |
| 113 | +} |
| 114 | + |
| 115 | +type State = { |
| 116 | + selectedOption: Option | null |
| 117 | +} |
| 118 | + |
| 119 | +class CustomSelector extends PureComponent<Props, State> { |
| 120 | + static defaultProps = { |
| 121 | + placeholder: '', |
| 122 | + options: null, |
| 123 | + disabled: false, |
| 124 | + clearable: false, |
| 125 | + searchable: false, |
| 126 | + value: null, |
| 127 | + onChange: null |
| 128 | + } |
| 129 | + |
| 130 | + state: State = { |
| 131 | + selectedOption: null |
| 132 | + } |
| 133 | + |
| 134 | + handleSelection = (selectedOption: Option) => { |
| 135 | + this.setState({ selectedOption }) |
| 136 | + } |
| 137 | + |
| 138 | + render () { |
| 139 | + const { placeholder, options, disabled, clearable, searchable, value, onChange } = this.props |
| 140 | + const { selectedOption } = this.state |
| 141 | + |
| 142 | + return ( |
| 143 | + <div> |
| 144 | + <Select |
| 145 | + {...this.props} |
| 146 | + name={'customSelector'} |
| 147 | + className={cx.select.toString()} |
| 148 | + optionRenderer={SelectorOption} |
| 149 | + placeholder={placeholder || 'Select...'} |
| 150 | + options={options} |
| 151 | + disabled={disabled} |
| 152 | + clearable={clearable} |
| 153 | + searchable={searchable} |
| 154 | + value={value || selectedOption} |
| 155 | + onChange={onChange || this.handleSelection} |
| 156 | + /> |
| 157 | + </div> |
| 158 | + ) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +export default CustomSelector |
0 commit comments