From be0a19a6b6fc3a5fc9b3bda46356ad3f4943cdcd 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/color-picker.js | 36 +++++++++++++++++++++++++++------ src/color-square.js | 38 +++++++++++++++++++++++++++++++++++ src/input-color.js | 14 ++++++++++--- src/utils.js | 4 ++++ stories/InputColor.stories.js | 6 +++++- 6 files changed, 89 insertions(+), 10 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/color-picker.js b/src/color-picker.js index 64d2c29..178ad38 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 && ( +
+ {recommendedColors.filter(onlyUnique).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={( + + )} > { onChange={(e) => setInitial(e.target.value)} />
- +
); };