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

Moved useColorPicker to services #3067

Merged
merged 13 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from 10 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
- Added `useResizeObserver` hook ([#2991](https://github.com/elastic/eui/pull/2991))
- Added `showColumnSelector.allowHide` and `showColumnSelector.allowReorder` props to `EuiDataGrid` UI configuration ([#2993](https://github.com/elastic/eui/pull/2993))
- Added `EuiMark` component ([#3060](https://github.com/elastic/eui/pull/3060))
- Moved and renamed `useColorPicker and useColorStops` from src-docs/src/views/color_picker/utils to `useColorPickerState and useColorStops` src/services ([#3067](https://github.com/elastic/eui/pull/3067))
- Changed `tabs.name` prop shape in `EuiTabbedContent` to accept a `node`, which aligns it with `EuiTab` ([#3100](https://github.com/elastic/eui/pull/3100))


**Bug Fixes**

- Fixed `EuiFieldNumber` so values of type `number` are now allowed ([#3020](https://github.com/elastic/eui/pull/3020))
Expand Down
6 changes: 3 additions & 3 deletions src-docs/src/views/color_picker/alpha.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';

import { EuiColorPicker, EuiFormRow } from '../../../../src/components';
import { useColorPicker } from './utils';
import { useColorPickerState } from '../../../../src/services';

export const Alpha = () => {
const [color, setColor, errors] = useColorPicker('#D36086');
const [color2, setColor2, errors2] = useColorPicker('211, 96, 134');
const [color, setColor, errors] = useColorPickerState('#D36086');
const [color2, setColor2, errors2] = useColorPickerState('211, 96, 134');

const customSwatches = [
'#54B399',
Expand Down
4 changes: 2 additions & 2 deletions src-docs/src/views/color_picker/color_picker.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react';

import { EuiColorPicker, EuiFormRow } from '../../../../src/components';
import { useColorPicker } from './utils';
import { useColorPickerState } from '../../../../src/services';

export const ColorPicker = () => {
const [color, setColor, errors] = useColorPicker('#D36086');
const [color, setColor, errors] = useColorPickerState('#D36086');
return (
<EuiFormRow label="Pick a color" isInvalid={!!errors} error={errors}>
<EuiColorPicker onChange={setColor} color={color} isInvalid={!!errors} />
Expand Down
4 changes: 2 additions & 2 deletions src-docs/src/views/color_picker/color_stops.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import React, { useState } from 'react';

import { EuiColorStops, EuiFormRow } from '../../../../src/components';

import { useColorStop } from './utils';
import { useColorStopsState } from '../../../../src/services';

export const ColorStops = () => {
const [colorStops, setColorStops, addColor] = useColorStop(true);
const [colorStops, setColorStops, addColor] = useColorStopsState(true);

const [extendedColorStops, setExtendedColorStops] = useState([
{
Expand Down
9 changes: 6 additions & 3 deletions src-docs/src/views/color_picker/containers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ import {
EuiSpacer,
} from '../../../../src/components';

import { useColorPicker, useColorStop } from './utils';
import {
useColorPickerState,
useColorStopsState,
} from '../../../../src/services';

export default () => {
const [color, setColor] = useColorPicker('#FFF');
const [colorStops, setColorStops] = useColorStop();
const [color, setColor] = useColorPickerState('#FFF');
const [colorStops, setColorStops] = useColorStopsState();
const [isModalVisible, setIsModalVisible] = useState(false);
const [isPopoverOpen, setIsPopoverOpen] = useState(false);

Expand Down
4 changes: 2 additions & 2 deletions src-docs/src/views/color_picker/custom_button.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import {
EuiSpacer,
} from '../../../../src/components';

import { useColorPicker } from './utils';
import { useColorPickerState } from '../../../../src/services';

export const CustomButton = () => {
const [color, setColor, errors] = useColorPicker('');
const [color, setColor, errors] = useColorPickerState('');
const [selectedColor, setSelectedColor] = useState(color);
const handleColorChange = (text, { hex, isValid }) => {
setColor(text, { hex, isValid });
Expand Down
9 changes: 6 additions & 3 deletions src-docs/src/views/color_picker/custom_swatches.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import {
EuiSpacer,
} from '../../../../src/components';

import { useColorPicker, useColorStop } from './utils';
import {
useColorPickerState,
useColorStopsState,
} from '../../../../src/services';

export const CustomSwatches = () => {
const [color, setColor, errors] = useColorPicker();
const [colorStops, setColorStops] = useColorStop();
const [color, setColor, errors] = useColorPickerState();
const [colorStops, setColorStops] = useColorStopsState();

const customSwatches = ['#333', '#666', '#999', '#CCC'];

Expand Down
8 changes: 4 additions & 4 deletions src-docs/src/views/color_picker/formats.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react';

import { EuiColorPicker, EuiFormRow } from '../../../../src/components';
import { useColorPicker } from './utils';
import { useColorPickerState } from '../../../../src/services';

export const Formats = () => {
const [color, setColor, errors] = useColorPicker('#D36086');
const [color2, setColor2, errors2] = useColorPicker('#D36086');
const [color3, setColor3, errors3] = useColorPicker('211, 96, 134');
const [color, setColor, errors] = useColorPickerState('#D36086');
const [color2, setColor2, errors2] = useColorPickerState('#D36086');
const [color3, setColor3, errors3] = useColorPickerState('211, 96, 134');
return (
<>
<EuiFormRow label="Auto format" isInvalid={!!errors} error={errors}>
Expand Down
4 changes: 2 additions & 2 deletions src-docs/src/views/color_picker/inline.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react';

import { EuiColorPicker } from '../../../../src/components';
import { useColorPicker } from './utils';
import { useColorPickerState } from '../../../../src/services';

export const Inline = () => {
const [color, setColor, errors] = useColorPicker('#D36086');
const [color, setColor, errors] = useColorPickerState('#D36086');
return (
<EuiColorPicker
onChange={setColor}
Expand Down
9 changes: 6 additions & 3 deletions src-docs/src/views/color_picker/kitchen_sink.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import {
} from '../../../../src/components';
import { DisplayToggles } from '../form_controls/display_toggles';

import { useColorPicker, useColorStop } from './utils';
import {
useColorPickerState,
useColorStopsState,
} from '../../../../src/services';

export const KitchenSink = () => {
const [color, setColor] = useColorPicker('#D36086');
const [colorStops, setColorStops, addStop] = useColorStop(true);
const [color, setColor] = useColorPickerState('#D36086');
const [colorStops, setColorStops, addStop] = useColorStopsState(true);

return (
<React.Fragment>
Expand Down
9 changes: 6 additions & 3 deletions src-docs/src/views/color_picker/modes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import {
EuiSpacer,
} from '../../../../src/components';

import { useColorPicker, useColorStop } from './utils';
import {
useColorPickerState,
useColorStopsState,
} from '../../../../src/services';

export const Modes = () => {
const [color, setColor, errors] = useColorPicker('#D36086');
const [colorStops, setColorStops] = useColorStop();
const [color, setColor, errors] = useColorPickerState('#D36086');
const [colorStops, setColorStops] = useColorStopsState();

return (
<React.Fragment>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { useMemo, useState } from 'react';

interface colorStopsType {
stop: number;
color: string;
}

const generateRandomColor = () =>
// https://www.paulirish.com/2009/random-hex-color-code-snippets/
`#${Math.floor(Math.random() * 16777215).toString(16)}`;

export const useColorStop = (useRandomColor = false) => {
const [addColor, setAddColor] = useState(generateRandomColor());
const [colorStops, setColorStops] = useState([
export const useColorStopsState = (
initialColorStops: colorStopsType[] = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed this, sorry.

The function signature changed from (useRandomColor: boolean) to (initialColorStops: colorStopsType[], useRandomColor: boolean) so any example that was using useColorStopsState(true) now causes errors.
You'll need to update the files where this service is used to pass the correct params.

{
stop: 20,
color: '#54B399',
Expand All @@ -19,9 +23,13 @@ export const useColorStop = (useRandomColor = false) => {
stop: 65,
color: '#9170B8',
},
]);
],
useRandomColor: boolean = false
) => {
const [addColor, setAddColor] = useState(generateRandomColor());
const [colorStops, setColorStops] = useState(initialColorStops);

const updateColorStops = colorStops => {
const updateColorStops = (colorStops: colorStopsType[]) => {
setColorStops(colorStops);
if (useRandomColor) {
setAddColor(generateRandomColor());
Expand All @@ -30,10 +38,10 @@ export const useColorStop = (useRandomColor = false) => {
return [colorStops, updateColorStops, addColor];
};

export const useColorPicker = (initialColor = '') => {
export const useColorPickerState = (initialColor = '') => {
const [color, setColorValue] = useState(initialColor);
const [isValid, setIsValid] = useState(true);
const setColor = (text, { isValid }) => {
const setColor = (text: string, { isValid }: { isValid: boolean }) => {
setColorValue(text);
setIsValid(isValid);
};
Expand Down
1 change: 1 addition & 0 deletions src/services/color_picker/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useColorPickerState, useColorStopsState } from './color_picker';
2 changes: 2 additions & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export {
HSV,
} from './color';

export { useColorPickerState, useColorStopsState } from './color_picker';

export { copyToClipboard } from './copy_to_clipboard';

export {
Expand Down