|
| 1 | +import React, { CSSProperties } from "react"; |
| 2 | +import classNames from "classnames"; |
| 3 | + |
| 4 | +import { utils } from "../../common"; |
| 5 | +import { ColorWeight, PaletteGroup } from "../../common/utils/colorHash"; |
| 6 | +import { CLASSPREFIX as eccgui } from "../../configuration/constants"; |
| 7 | +import { ContextOverlay } from "../ContextOverlay"; |
| 8 | +import { FieldSet } from "../Form"; |
| 9 | +import { RadioButton } from "../RadioButton/RadioButton"; |
| 10 | +import { Spacing } from "../Separation/Spacing"; |
| 11 | +import { Tag, TagList } from "../Tag"; |
| 12 | +import { TextField, TextFieldProps } from "../TextField"; |
| 13 | +import { Tooltip } from "../Tooltip/Tooltip"; |
| 14 | +import { WhiteSpaceContainer } from "../Typography"; |
| 15 | + |
| 16 | +export interface ColorFieldProps extends Omit<TextFieldProps, "invisibleCharacterWarning"> { |
| 17 | + /** |
| 18 | + * Any color can be selected, not only from the configured color palette. |
| 19 | + */ |
| 20 | + allowCustomColor?: boolean; |
| 21 | + /** |
| 22 | + * What color weights should be included in the set of allowed colors. |
| 23 | + */ |
| 24 | + colorWeightFilter?: ColorWeight[]; |
| 25 | + /** |
| 26 | + * What palette groups should be included in the set of allowed colors. |
| 27 | + */ |
| 28 | + paletteGroupFilter?: PaletteGroup[]; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Text input field. |
| 33 | + */ |
| 34 | +export const ColorField = ({ |
| 35 | + className = "", |
| 36 | + allowCustomColor = false, |
| 37 | + colorWeightFilter = [100, 300, 700, 900], |
| 38 | + paletteGroupFilter = ["layout"], |
| 39 | + defaultValue, |
| 40 | + value, |
| 41 | + onChange, |
| 42 | + fullWidth = false, |
| 43 | + ...otherTextFieldProps |
| 44 | +}: ColorFieldProps) => { |
| 45 | + const ref = React.useRef(null); |
| 46 | + const [colorValue, setColorValue] = React.useState<string>(defaultValue || value || "#000000"); |
| 47 | + |
| 48 | + let allowedPaletteColors, disableNativePicker, disabled; |
| 49 | + const updateConfig = () => { |
| 50 | + allowedPaletteColors = utils.getEnabledColorPropertiesFromPalette({ |
| 51 | + includePaletteGroup: paletteGroupFilter, |
| 52 | + includeColorWeight: colorWeightFilter, |
| 53 | + minimalColorDistance: 0, // we use all allowed colors, and do not check distances between them |
| 54 | + }); |
| 55 | + |
| 56 | + disableNativePicker = |
| 57 | + colorWeightFilter.length > 0 && paletteGroupFilter.length > 0 && allowedPaletteColors.length > 0; |
| 58 | + disabled = (!disableNativePicker && !allowCustomColor) || otherTextFieldProps.disabled; |
| 59 | + }; |
| 60 | + updateConfig(); |
| 61 | + React.useEffect(() => { |
| 62 | + updateConfig(); |
| 63 | + }, [allowCustomColor, colorWeightFilter, paletteGroupFilter, otherTextFieldProps]); |
| 64 | + |
| 65 | + React.useEffect(() => { |
| 66 | + setColorValue(defaultValue || value || "#000000"); |
| 67 | + }, [defaultValue, value]); |
| 68 | + |
| 69 | + const forwardOnChange = (forwardedEvent: React.ChangeEvent<HTMLInputElement>) => { |
| 70 | + setColorValue(forwardedEvent.target.value); |
| 71 | + if (onChange) { |
| 72 | + onChange(forwardedEvent); |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + const colorInput = ( |
| 77 | + <TextField |
| 78 | + inputRef={ref} |
| 79 | + className={classNames(`${eccgui}-colorfield`, className, { |
| 80 | + [`${eccgui}-colorfield--custom-picker`]: disableNativePicker, |
| 81 | + })} |
| 82 | + // we cannot use `color` type for the custom picker because we do not have control over it then |
| 83 | + type={!disableNativePicker ? "color" : "text"} |
| 84 | + readOnly={disableNativePicker} |
| 85 | + disabled={disabled} |
| 86 | + value={colorValue} |
| 87 | + fullWidth={fullWidth} |
| 88 | + {...otherTextFieldProps} |
| 89 | + onChange={ |
| 90 | + !disableNativePicker |
| 91 | + ? (e: React.ChangeEvent<HTMLInputElement>) => { |
| 92 | + forwardOnChange(e); |
| 93 | + } |
| 94 | + : undefined |
| 95 | + } |
| 96 | + style={{ ...otherTextFieldProps.style, [`--eccgui-colorfield-background`]: colorValue } as CSSProperties} |
| 97 | + /> |
| 98 | + ); |
| 99 | + |
| 100 | + return disableNativePicker && !disabled ? ( |
| 101 | + <ContextOverlay |
| 102 | + fill={fullWidth} |
| 103 | + content={ |
| 104 | + <WhiteSpaceContainer |
| 105 | + paddingTop={"small"} |
| 106 | + paddingRight={"small"} |
| 107 | + paddingBottom={"small"} |
| 108 | + paddingLeft={"small"} |
| 109 | + className={`${eccgui}-colorfield__picker`} |
| 110 | + > |
| 111 | + {allowCustomColor && ( |
| 112 | + <> |
| 113 | + <TextField |
| 114 | + type={"color"} |
| 115 | + value={colorValue} |
| 116 | + onChange={(e: React.ChangeEvent<HTMLInputElement>) => { |
| 117 | + forwardOnChange(e); |
| 118 | + }} |
| 119 | + /> |
| 120 | + <Spacing size={"small"} /> |
| 121 | + </> |
| 122 | + )} |
| 123 | + <FieldSet> |
| 124 | + <TagList |
| 125 | + className={`${eccgui}-colorfield__palette ${eccgui}-colorfield__palette--${ |
| 126 | + colorWeightFilter.length >= 3 ? colorWeightFilter.length * 2 : "8" |
| 127 | + }col`} |
| 128 | + > |
| 129 | + {allowedPaletteColors!.map((color: [string, string], idx: number) => [ |
| 130 | + <RadioButton |
| 131 | + className={`${eccgui}-colorfield__palette__color`} |
| 132 | + hideIndicator |
| 133 | + value={color[1]} |
| 134 | + onChange={(e: React.ChangeEvent<HTMLInputElement>) => { |
| 135 | + forwardOnChange(e); |
| 136 | + }} |
| 137 | + > |
| 138 | + <Tooltip key={idx} content={color[0].replace(`${eccgui}-color-palette-`, "")}> |
| 139 | + <Tag |
| 140 | + large |
| 141 | + style={{ [`--eccgui-colorfield-palette-color`]: color[1] } as CSSProperties} |
| 142 | + > |
| 143 | + {color[1]} |
| 144 | + </Tag> |
| 145 | + </Tooltip> |
| 146 | + </RadioButton>, |
| 147 | + // Looks like we cannot force some type of line break in the tag list via CSS only |
| 148 | + (idx + 1) % (colorWeightFilter.length >= 3 ? colorWeightFilter.length * 2 : 8) === |
| 149 | + 0 && ( |
| 150 | + <> |
| 151 | + <br className={`${eccgui}-colorfield__palette-linebreak`} /> |
| 152 | + </> |
| 153 | + ), |
| 154 | + ])} |
| 155 | + </TagList> |
| 156 | + </FieldSet> |
| 157 | + </WhiteSpaceContainer> |
| 158 | + } |
| 159 | + > |
| 160 | + {colorInput} |
| 161 | + </ContextOverlay> |
| 162 | + ) : ( |
| 163 | + colorInput |
| 164 | + ); |
| 165 | +}; |
| 166 | + |
| 167 | +export default ColorField; |
0 commit comments