import * as React from 'react'; import { Text } from 'office-ui-fabric-react/lib/Text'; import { TextField } from 'office-ui-fabric-react/lib/TextField'; import { Stack } from 'office-ui-fabric-react/lib/Stack'; import { ColorPicker } from 'office-ui-fabric-react/lib/ColorPicker'; import { Callout } from 'office-ui-fabric-react/lib/Callout'; import { mergeStyles } from '@uifabric/merge-styles'; import { IColor, getColorFromString } from 'office-ui-fabric-react/lib/Color'; const colorLabelClassName = mergeStyles({ fontSize: 16, fontWeight: 800, marginLeft: 20, }); const colorBoxClassName = mergeStyles({ width: 20, height: 20, display: 'inline-block', position: 'absolute', left: 5, top: 5, border: '1px solid black', flexShrink: 0, }); const textBoxClassName = mergeStyles({ width: 100, }); const colorPanelClassName = mergeStyles({ position: 'relative' /* This is necessary to make position: absolute; work in the other style. */, }); export interface IThemeDesignerColorPickerProps { color: IColor; onColorChange: (color: IColor | undefined) => void; label: string; } export interface IThemeDesignerColorPickerState { isColorPickerVisible: boolean; editingColorStr?: string; } export class ThemeDesignerColorPicker extends React.Component< IThemeDesignerColorPickerProps, IThemeDesignerColorPickerState > { private _colorPickerRef = React.createRef(); constructor(props: IThemeDesignerColorPickerProps) { super(props); this.state = { isColorPickerVisible: false, }; this._updateColorPickerVisible = this._updateColorPickerVisible.bind(this); this._onTextFieldValueChange = this._onTextFieldValueChange.bind(this); this._onCalloutDismiss = this._onCalloutDismiss.bind(this); this._onColorPickerChange = this._onColorPickerChange.bind(this); } public render() { const { editingColorStr = this.props.color.str } = this.state; return (
{this.props.label}
{this.state.isColorPickerVisible && ( )}
); } private _updateColorPickerVisible() { this.setState({ isColorPickerVisible: true }); } private _onTextFieldValueChange(ev: any, newValue: string | undefined) { const newColor = getColorFromString(newValue!); if (newColor) { this.props.onColorChange(newColor); this.setState({ editingColorStr: undefined }); } else { this.setState({ editingColorStr: newValue }); } } private _onCalloutDismiss() { this.setState({ isColorPickerVisible: false }); } private _onColorPickerChange(ev: any, color: IColor) { this.props.onColorChange(color); } }