Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added recommendedColors prop #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface InputColorProps {
placement?: string;
onChange?(color: Color): void;
disabled?: boolean;
recommendedColors?: string[];
}

export default function InputColor(props: InputColorProps);
6 changes: 5 additions & 1 deletion src/__tests__/input-color.js
Original file line number Diff line number Diff line change
@@ -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(() =>
Expand Down Expand Up @@ -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']);
});
36 changes: 30 additions & 6 deletions src/color-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -70,6 +71,20 @@ const ColorPicker = ({ color, onChange, disabled }) => {

return (
<div css={styles.picker} onClick={handleClick}>

{!!recommendedColors.length && (
<div css={styles.recommendedColors}>
{onlyUnique(recommendedColors).map((recommendedColor) => (
<ColorSquare
key={recommendedColor}
color={recommendedColor}
onClick={() => !disabled && onChange(parseColor(recommendedColor))}
disabled={disabled}
/>
))}
</div>
)}

<div css={styles.selector} style={{ backgroundColor: hueBackground }}>
<div css={styles.gradientWhite} />
<div css={styles.gradientDark} />
Expand Down Expand Up @@ -153,9 +168,7 @@ const ColorPicker = ({ color, onChange, disabled }) => {
disabled={disabled}
/>
</div>
<div
style={{ backgroundColor: rgbaBackground, width: 30, height: 30 }}
/>
<ColorSquare color={rgbaBackground} />
</div>

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

ColorPicker.defaultProps = {
initialValue: '#5e72e4',
disabled: false
disabled: false,
recommendedColors: []
};

const styles = {
Expand Down Expand Up @@ -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;
38 changes: 38 additions & 0 deletions src/color-square.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/** @jsx jsx */
import { jsx } from '@emotion/core';

const ColorSquare = ({color, onClick, disabled}) => {
return (
<div
onClick={onClick}
css={{...styles.square, ...(onClick && styles.button)}}
style={{ cursor: (onClick && !disabled) ? 'pointer' : 'normal' }}
>
<div css={styles.content} style={{ backgroundColor: color }} />
</div>
);
};

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;
14 changes: 11 additions & 3 deletions src/input-color.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -22,7 +22,14 @@ const InputColor = ({ initialValue, onChange, placement, disabled, ...props }) =
return (
<Popover
placement={placement}
body={<ColorPicker color={color} onChange={changeColor} disabled={ disabled } />}
body={(
<ColorPicker
color={color}
onChange={changeColor}
disabled={disabled}
recommendedColors={recommendedColors}
/>
)}
>
<span
{...props}
Expand Down Expand Up @@ -55,7 +62,8 @@ const InputColor = ({ initialValue, onChange, placement, disabled, ...props }) =

InputColor.defaultProps = {
placement: 'bottom',
disabled: false
disabled: false,
recommendedColors: []
};

export default InputColor;
4 changes: 4 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ export {
rgba,
hsv2rgb,
} from '@swiftcarrot/color-fns';

export function onlyUnique(arr) {
return arr.filter((value, index, self) => self.indexOf(value) === index);
}
6 changes: 5 additions & 1 deletion stories/InputColor.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ export const Demo = () => {
onChange={(e) => setInitial(e.target.value)}
/>
<br />
<InputColor initialValue={initial} onChange={handleChange} />
<InputColor
initialValue={initial}
onChange={handleChange}
recommendedColors={['#000', '#babaca', '#3a1596', '#5e72e4', '#Fa2c4a', '#b24acf']}
/>
</div>
);
};
Expand Down