diff --git a/packages/grafana-ui/src/components/ColorPicker/ColorPicker.tsx b/packages/grafana-ui/src/components/ColorPicker/ColorPicker.tsx index f807b09bd93..a08fd27b23e 100644 --- a/packages/grafana-ui/src/components/ColorPicker/ColorPicker.tsx +++ b/packages/grafana-ui/src/components/ColorPicker/ColorPicker.tsx @@ -5,10 +5,23 @@ import { ColorPickerPopover } from './ColorPickerPopover'; import { Themeable, GrafanaTheme } from '../../types'; import { getColorFromHexRgbOrName } from '../../utils/colorsPalette'; import { SeriesColorPickerPopover } from './SeriesColorPickerPopover'; +import propDeprecationWarning from '../../utils/propDeprecationWarning'; +type ColorPickerChangeHandler = (color: string) => void; + +export const handleColorPickerPropsDeprecation = (componentName: string, props: ColorPickerProps) => { + const { onColorChange } = props; + if (onColorChange) { + propDeprecationWarning(componentName, 'onColorChange', 'onChange'); + } +}; export interface ColorPickerProps extends Themeable { color: string; - onChange: (color: string) => void; + onChange: ColorPickerChangeHandler; + /** + * @deprecated Use onChange instead + */ + onColorChange?: ColorPickerChangeHandler; enableNamedColors?: boolean; withArrow?: boolean; children?: JSX.Element; @@ -16,20 +29,18 @@ export interface ColorPickerProps extends Themeable { export const colorPickerFactory = ( popover: React.ComponentType, - displayName?: string, + displayName = 'ColorPicker', renderPopoverArrowFunction?: RenderPopperArrowFn ) => { return class ColorPicker extends Component { - static displayName = displayName || 'ColorPicker'; + static displayName = displayName; pickerTriggerRef = createRef(); handleColorChange = (color: string) => { - const { enableNamedColors, onChange } = this.props; + const { onColorChange, onChange } = this.props; + const changeHandler = (onColorChange || onChange) as ColorPickerChangeHandler; - if (enableNamedColors) { - return onChange(color); - } - return onChange(getColorFromHexRgbOrName(color)); + return changeHandler(color); }; render() { @@ -77,7 +88,7 @@ export const colorPickerFactory = (
diff --git a/packages/grafana-ui/src/components/ColorPicker/ColorPickerPopover.tsx b/packages/grafana-ui/src/components/ColorPicker/ColorPickerPopover.tsx index fe12082cb5e..bb82e5b389f 100644 --- a/packages/grafana-ui/src/components/ColorPicker/ColorPickerPopover.tsx +++ b/packages/grafana-ui/src/components/ColorPicker/ColorPickerPopover.tsx @@ -1,7 +1,7 @@ import React from 'react'; import NamedColorsPicker from './NamedColorsPalette'; -import { getColorName } from '../..//utils/colorsPalette'; -import { ColorPickerProps } from './ColorPicker'; +import { getColorName, getColorFromHexRgbOrName } from '../..//utils/colorsPalette'; +import { ColorPickerProps, handleColorPickerPropsDeprecation } from './ColorPicker'; import { GrafanaTheme, Themeable } from '../../types'; import { PopperContentProps } from '../Tooltip/PopperController'; import SpectrumPalette from './SpectrumPalette'; @@ -20,20 +20,27 @@ export class ColorPickerPopover extends React.Component { this.state = { activePicker: 'palette', }; + handleColorPickerPropsDeprecation('ColorPickerPopover', props); } - handleSpectrumColorSelect = (color: any) => { - this.props.onChange(color); + handleChange = (color: any) => { + const { onColorChange, onChange, enableNamedColors } = this.props; + const changeHandler = onColorChange || onChange; + + if (enableNamedColors) { + return changeHandler(color); + } + changeHandler(getColorFromHexRgbOrName(color)); }; renderPicker = () => { const { activePicker } = this.state; - const { color, onChange, theme } = this.props; + const { color, theme } = this.props; return activePicker === 'spectrum' ? ( - + ) : ( - + ); }; diff --git a/packages/grafana-ui/src/components/ColorPicker/SeriesColorPickerPopover.tsx b/packages/grafana-ui/src/components/ColorPicker/SeriesColorPickerPopover.tsx index f019dc1634b..559b3f4a550 100644 --- a/packages/grafana-ui/src/components/ColorPicker/SeriesColorPickerPopover.tsx +++ b/packages/grafana-ui/src/components/ColorPicker/SeriesColorPickerPopover.tsx @@ -1,24 +1,19 @@ import React, { FunctionComponent } from 'react'; -import { ColorPickerPopover } from './ColorPickerPopover'; -import { Themeable } from '../../types'; -import { ColorPickerProps } from './ColorPicker'; -import { PopperContentProps } from '../Tooltip/PopperController'; -export interface SeriesColorPickerPopoverProps extends ColorPickerProps, Themeable, PopperContentProps { +import { ColorPickerPopover } from './ColorPickerPopover'; +import { ColorPickerProps } from './ColorPicker'; +import { PopperContentProps } from '../Tooltip/PopperController'; + +export interface SeriesColorPickerPopoverProps extends ColorPickerProps, PopperContentProps { yaxis?: number; onToggleAxis?: () => void; } -export const SeriesColorPickerPopover: FunctionComponent = ({ - onChange, - color, - theme, - yaxis, - onToggleAxis, - updatePopperPosition -}) => { +export const SeriesColorPickerPopover: FunctionComponent = props => { + const { yaxis, onToggleAxis, color, ...colorPickerProps } = props; + return ( - +
{yaxis && }
); diff --git a/packages/grafana-ui/src/utils/propDeprecationWarning.ts b/packages/grafana-ui/src/utils/propDeprecationWarning.ts new file mode 100644 index 00000000000..a277395c090 --- /dev/null +++ b/packages/grafana-ui/src/utils/propDeprecationWarning.ts @@ -0,0 +1,6 @@ +const propDeprecationWarning = (componentName: string, propName: string, newPropName: string) => { + const message = `[Deprecation warning] ${componentName}: ${propName} is deprecated. Use ${newPropName} instead`; + console.warn(message); +}; + +export default propDeprecationWarning;