FieldConfig: implement color picker (#24833)

This commit is contained in:
Ryan McKinley 2020-05-29 10:09:46 -07:00 committed by GitHub
parent b703f21622
commit 57a9e422b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 4 deletions

View File

@ -109,5 +109,7 @@ export const booleanOverrideProcessor = (
}; };
export interface ColorFieldConfigSettings { export interface ColorFieldConfigSettings {
enableNamedColors?: boolean; allowUndefined?: boolean;
textWhenUndefined?: string; // Pick Color
disableNamedColors?: boolean;
} }

View File

@ -1,11 +1,80 @@
import React from 'react'; import React from 'react';
import { FieldConfigEditorProps, ColorFieldConfigSettings } from '@grafana/data'; import {
/* import { ColorPicker } from '../ColorPicker/ColorPicker'; */ FieldConfigEditorProps,
ColorFieldConfigSettings,
GrafanaTheme,
getColorFromHexRgbOrName,
} from '@grafana/data';
import { ColorPicker } from '../ColorPicker/ColorPicker';
import { getTheme, stylesFactory } from '../../themes';
import { Icon } from '../Icon/Icon';
import { css } from 'emotion';
import { ColorPickerTrigger } from '../ColorPicker/ColorPickerTrigger';
export const ColorValueEditor: React.FC<FieldConfigEditorProps<string, ColorFieldConfigSettings>> = ({ export const ColorValueEditor: React.FC<FieldConfigEditorProps<string, ColorFieldConfigSettings>> = ({
value, value,
onChange, onChange,
item, item,
}) => { }) => {
return <div>todo</div>; const { settings } = item;
const theme = getTheme();
const styles = getStyles(theme);
const color = value || (item.defaultValue as string) || theme.colors.panelBg;
return (
<ColorPicker color={color} onChange={onChange} enableNamedColors={!settings.disableNamedColors}>
{({ ref, showColorPicker, hideColorPicker }) => {
return (
<div className={styles.spot} onBlur={hideColorPicker}>
<div className={styles.colorPicker}>
<ColorPickerTrigger
ref={ref}
onClick={showColorPicker}
onMouseLeave={hideColorPicker}
color={getColorFromHexRgbOrName(color, theme.type)}
/>
</div>
<div className={styles.colorText} onClick={showColorPicker}>
{value ?? settings.textWhenUndefined ?? 'Pick Color'}
</div>
{value && settings.allowUndefined && (
<Icon className={styles.trashIcon} name="trash-alt" onClick={() => onChange(undefined)} />
)}
</div>
);
}}
</ColorPicker>
);
}; };
const getStyles = stylesFactory((theme: GrafanaTheme) => {
return {
spot: css`
color: ${theme.colors.text};
background: ${theme.colors.formInputBg};
padding: 3px;
border: 1px solid ${theme.colors.formInputBorder};
display: flex;
flex-direction: row;
align-items: center;
&:hover {
border: 1px solid ${theme.colors.formInputBorderHover};
}
`,
colorPicker: css`
padding: 0 ${theme.spacing.sm};
`,
colorText: css`
cursor: pointer;
flex-grow: 1;
`,
trashIcon: css`
cursor: pointer;
color: ${theme.colors.textWeak};
&:hover {
color: ${theme.colors.text};
}
`,
};
});