/
Styled.js
118 lines (106 loc) · 2.78 KB
/
Styled.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// @flow
// @glam
import React, { Component } from 'react';
import { withValue } from 'react-value';
import chroma from 'chroma-js';
import Select from '../../src';
import { Code, Link, H1 } from '../components';
import { colourOptions } from '../data';
const dot = (color = '#ccc') => ({
alignItems: 'center',
display: 'flex ',
':before': {
backgroundColor: color,
borderRadius: 10,
content: ' ',
display: 'block',
marginRight: 8,
height: 10,
width: 10,
},
});
const colourStyles = {
control: styles => ({ ...styles, backgroundColor: 'white' }),
option: (styles, { data, isDisabled, isFocused, isSelected }) => {
const color = chroma(data.color);
return {
...styles,
backgroundColor: isDisabled
? null
: isSelected ? data.color : isFocused ? color.alpha(0.1).css() : null,
color: isDisabled
? '#ccc'
: isSelected
? chroma.contrast(color, 'white') > 2 ? 'white' : 'black'
: data.color,
cursor: isDisabled ? 'not-allowed' : 'default',
};
},
};
const colourStylesSingle = {
...colourStyles,
input: styles => ({ ...styles, ...dot() }),
placeholder: styles => ({ ...styles, ...dot() }),
singleValue: (styles, { data }) => ({ ...styles, ...dot(data.color) }),
};
const colourStylesMulti = {
...colourStyles,
multiValue: (styles, { data }) => {
const color = chroma(data.color);
return {
...styles,
backgroundColor: color.alpha(0.1).css(),
};
},
multiValueLabel: (styles, { data }) => ({
...styles,
color: data.color,
}),
multiValueRemove: (styles, { data }) => ({
...styles,
color: data.color,
':hover': {
backgroundColor: data.color,
color: 'white',
},
}),
};
const SelectWithValue = withValue(Select);
type State = {};
export default class StyledApp extends Component<*, State> {
state = {};
render() {
return (
<div>
<H1>Custom Styles</H1>
<p>
Style individual components with custom css using the{' '}
<Code>styles</Code> prop.{' '}
<Link
href="https://github.com/JedWatson/react-select/blob/v2/examples/pages/Styled.js"
target="_blank"
>
Source
</Link>
</p>
<h2>Examples</h2>
<h4>Single Select</h4>
<SelectWithValue
autoFocus
defaultValue={colourOptions[2]}
label="Single select"
options={colourOptions}
styles={colourStylesSingle}
/>
<h4>Multi Select</h4>
<SelectWithValue
closeMenuOnSelect={false}
defaultValue={[colourOptions[0], colourOptions[1]]}
isMulti
options={colourOptions}
styles={colourStylesMulti}
/>
</div>
);
}
}