Skip to content

Commit be0a19a

Browse files
Guilherme Montegmonte
Guilherme Monte
authored andcommitted
feat: added recommended colors array
1 parent 5a7ae27 commit be0a19a

File tree

6 files changed

+89
-10
lines changed

6 files changed

+89
-10
lines changed

index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface InputColorProps {
1717
placement?: string;
1818
onChange?(color: Color): void;
1919
disabled?: boolean;
20+
recommendedColors?: string[];
2021
}
2122

2223
export default function InputColor(props: InputColorProps);

src/color-picker.js

+30-6
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import {
1010
rgba,
1111
hsv2rgb,
1212
} from '@swiftcarrot/color-fns';
13-
import { rgba2hex } from './utils';
13+
import { rgba2hex, parseColor, onlyUnique } from './utils';
14+
import ColorSquare from './color-square'
1415

1516
const KEY_ENTER = 13;
1617

17-
const ColorPicker = ({ color, onChange, disabled }) => {
18+
const ColorPicker = ({ color, onChange, disabled, recommendedColors }) => {
1819
const { r, g, b, a, h, s, v } = color;
1920

2021
function changeColor(color) {
@@ -70,6 +71,20 @@ const ColorPicker = ({ color, onChange, disabled }) => {
7071

7172
return (
7273
<div css={styles.picker} onClick={handleClick}>
74+
75+
{!!recommendedColors.length && (
76+
<div css={styles.recommendedColors}>
77+
{recommendedColors.filter(onlyUnique).map((recommendedColor) => (
78+
<ColorSquare
79+
key={recommendedColor}
80+
color={recommendedColor}
81+
onClick={() => !disabled && onChange(parseColor(recommendedColor))}
82+
disabled={disabled}
83+
/>
84+
))}
85+
</div>
86+
)}
87+
7388
<div css={styles.selector} style={{ backgroundColor: hueBackground }}>
7489
<div css={styles.gradientWhite} />
7590
<div css={styles.gradientDark} />
@@ -153,9 +168,7 @@ const ColorPicker = ({ color, onChange, disabled }) => {
153168
disabled={disabled}
154169
/>
155170
</div>
156-
<div
157-
style={{ backgroundColor: rgbaBackground, width: 30, height: 30 }}
158-
/>
171+
<ColorSquare color={rgbaBackground} />
159172
</div>
160173

161174
<div css={styles.inputs}>
@@ -219,7 +232,8 @@ const ColorPicker = ({ color, onChange, disabled }) => {
219232

220233
ColorPicker.defaultProps = {
221234
initialValue: '#5e72e4',
222-
disabled: false
235+
disabled: false,
236+
recommendedColors: []
223237
};
224238

225239
const styles = {
@@ -278,6 +292,16 @@ const styles = {
278292
marginTop: 4,
279293
},
280294
},
295+
296+
recommendedColors: {
297+
width: '100%',
298+
display: 'flex',
299+
flexWrap: 'wrap',
300+
alignItems: 'center',
301+
paddingBottom: 10,
302+
marginBottom: 10,
303+
borderBottom: '1px solid #e9e9e9'
304+
}
281305
};
282306

283307
export default ColorPicker;

src/color-square.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/** @jsx jsx */
2+
import { jsx } from '@emotion/core';
3+
4+
const ColorSquare = ({color, onClick, disabled}) => {
5+
return (
6+
<div
7+
onClick={onClick}
8+
css={{...styles.square, ...(onClick && styles.button)}}
9+
style={{ cursor: (onClick && !disabled) ? 'pointer' : 'normal' }}
10+
>
11+
<div css={styles.content} style={{ backgroundColor: color }} />
12+
</div>
13+
);
14+
};
15+
16+
ColorSquare.defaultProps = {
17+
onClick: undefined,
18+
disabled: false,
19+
recommendedColors: []
20+
};
21+
22+
const styles = {
23+
button: {
24+
padding: 3,
25+
border: '1px solid #bebebe',
26+
borderRadius: 4,
27+
},
28+
square: {
29+
display: 'flex',
30+
width: 30,
31+
height: 30,
32+
},
33+
content: {
34+
flex: 1,
35+
}
36+
};
37+
38+
export default ColorSquare;

src/input-color.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Popover from '@xkit/popover';
55
import ColorPicker from './color-picker';
66
import { parseColor } from './utils';
77

8-
const InputColor = ({ initialValue, onChange, placement, disabled, ...props }) => {
8+
const InputColor = ({ initialValue, onChange, placement, disabled, recommendedColors, ...props }) => {
99
const [color, setColor] = useState(parseColor(initialValue));
1010

1111
useEffect(() => {
@@ -22,7 +22,14 @@ const InputColor = ({ initialValue, onChange, placement, disabled, ...props }) =
2222
return (
2323
<Popover
2424
placement={placement}
25-
body={<ColorPicker color={color} onChange={changeColor} disabled={ disabled } />}
25+
body={(
26+
<ColorPicker
27+
color={color}
28+
onChange={changeColor}
29+
disabled={disabled}
30+
recommendedColors={recommendedColors}
31+
/>
32+
)}
2633
>
2734
<span
2835
{...props}
@@ -55,7 +62,8 @@ const InputColor = ({ initialValue, onChange, placement, disabled, ...props }) =
5562

5663
InputColor.defaultProps = {
5764
placement: 'bottom',
58-
disabled: false
65+
disabled: false,
66+
recommendedColors: []
5967
};
6068

6169
export default InputColor;

src/utils.js

+4
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ export {
3131
rgba,
3232
hsv2rgb,
3333
} from '@swiftcarrot/color-fns';
34+
35+
export function onlyUnique(value, index, self) {
36+
return self.indexOf(value) === index;
37+
}

stories/InputColor.stories.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ export const Demo = () => {
3939
onChange={(e) => setInitial(e.target.value)}
4040
/>
4141
<br />
42-
<InputColor initialValue={initial} onChange={handleChange} />
42+
<InputColor
43+
initialValue={initial}
44+
onChange={handleChange}
45+
recommendedColors={['#000', '#babaca', '#3a1596', '#5e72e4', '#Fa2c4a', '#b24acf']}
46+
/>
4347
</div>
4448
);
4549
};

0 commit comments

Comments
 (0)