From c811047e0afade43ae615b1c51c55c87c067e261 Mon Sep 17 00:00:00 2001 From: Guilherme Monte Date: Sun, 5 Feb 2023 18:31:06 -0300 Subject: [PATCH] feat: added recommended colors array --- index.d.ts | 1 + src/__tests__/input-color.js | 6 +++++- src/color-picker.js | 36 +++++++++++++++++++++++++++------ src/color-square.js | 38 +++++++++++++++++++++++++++++++++++ src/input-color.js | 14 ++++++++++--- src/utils.js | 4 ++++ stories/InputColor.stories.js | 6 +++++- 7 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 src/color-square.js diff --git a/index.d.ts b/index.d.ts index c2da7a7..14af187 100644 --- a/index.d.ts +++ b/index.d.ts @@ -17,6 +17,7 @@ export interface InputColorProps { placement?: string; onChange?(color: Color): void; disabled?: boolean; + recommendedColors?: string[]; } export default function InputColor(props: InputColorProps); diff --git a/src/__tests__/input-color.js b/src/__tests__/input-color.js index 5372270..3692558 100644 --- a/src/__tests__/input-color.js +++ b/src/__tests__/input-color.js @@ -1,7 +1,7 @@ import React from 'react'; import renderer from 'react-test-renderer'; import InputColor from '../'; -import { parseColor, hex2alpha, rgba2hex, alpha2hex } from '../utils'; +import { parseColor, hex2alpha, rgba2hex, alpha2hex, onlyUnique } from '../utils'; test('render', () => { expect(() => @@ -51,3 +51,7 @@ test('alpha2hex', () => { test('rgba2hex', () => { expect(rgba2hex(255, 255, 255, 50)).toEqual(`#ffffff80`); }); + +test('onlyUnique', () => { + expect(onlyUnique(['a', 'b', 'c', 'b'])).toEqual(['a', 'b', 'c']); +}); diff --git a/src/color-picker.js b/src/color-picker.js index 64d2c29..75f83d2 100644 --- a/src/color-picker.js +++ b/src/color-picker.js @@ -10,11 +10,12 @@ import { rgba, hsv2rgb, } from '@swiftcarrot/color-fns'; -import { rgba2hex } from './utils'; +import { rgba2hex, parseColor, onlyUnique } from './utils'; +import ColorSquare from './color-square' const KEY_ENTER = 13; -const ColorPicker = ({ color, onChange, disabled }) => { +const ColorPicker = ({ color, onChange, disabled, recommendedColors }) => { const { r, g, b, a, h, s, v } = color; function changeColor(color) { @@ -70,6 +71,20 @@ const ColorPicker = ({ color, onChange, disabled }) => { return (
+ + {!!recommendedColors.length && ( +
+ {onlyUnique(recommendedColors).map((recommendedColor) => ( + !disabled && onChange(parseColor(recommendedColor))} + disabled={disabled} + /> + ))} +
+ )} +
@@ -153,9 +168,7 @@ const ColorPicker = ({ color, onChange, disabled }) => { disabled={disabled} />
-
+
@@ -219,7 +232,8 @@ const ColorPicker = ({ color, onChange, disabled }) => { ColorPicker.defaultProps = { initialValue: '#5e72e4', - disabled: false + disabled: false, + recommendedColors: [] }; const styles = { @@ -278,6 +292,16 @@ const styles = { marginTop: 4, }, }, + + recommendedColors: { + width: '100%', + display: 'flex', + flexWrap: 'wrap', + alignItems: 'center', + paddingBottom: 10, + marginBottom: 10, + borderBottom: '1px solid #e9e9e9' + } }; export default ColorPicker; diff --git a/src/color-square.js b/src/color-square.js new file mode 100644 index 0000000..626b60c --- /dev/null +++ b/src/color-square.js @@ -0,0 +1,38 @@ +/** @jsx jsx */ +import { jsx } from '@emotion/core'; + +const ColorSquare = ({color, onClick, disabled}) => { + return ( +
+
+
+ ); +}; + +ColorSquare.defaultProps = { + onClick: undefined, + disabled: false, + recommendedColors: [] +}; + +const styles = { + button: { + padding: 3, + border: '1px solid #bebebe', + borderRadius: 4, + }, + square: { + display: 'flex', + width: 30, + height: 30, + }, + content: { + flex: 1, + } +}; + +export default ColorSquare; diff --git a/src/input-color.js b/src/input-color.js index a9618e0..5efed9f 100644 --- a/src/input-color.js +++ b/src/input-color.js @@ -5,7 +5,7 @@ import Popover from '@xkit/popover'; import ColorPicker from './color-picker'; import { parseColor } from './utils'; -const InputColor = ({ initialValue, onChange, placement, disabled, ...props }) => { +const InputColor = ({ initialValue, onChange, placement, disabled, recommendedColors, ...props }) => { const [color, setColor] = useState(parseColor(initialValue)); useEffect(() => { @@ -22,7 +22,14 @@ const InputColor = ({ initialValue, onChange, placement, disabled, ...props }) = return ( } + body={( + + )} > self.indexOf(value) === index); +} \ No newline at end of file diff --git a/stories/InputColor.stories.js b/stories/InputColor.stories.js index f26f635..8f602d1 100644 --- a/stories/InputColor.stories.js +++ b/stories/InputColor.stories.js @@ -39,7 +39,11 @@ export const Demo = () => { onChange={(e) => setInitial(e.target.value)} />
- +
); };