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)}
/>
-
+
);
};