Skip to content

Commit 5f2efcd

Browse files
committed
add color hash helper function/story and fix component description
1 parent e99fa43 commit 5f2efcd

2 files changed

Lines changed: 74 additions & 5 deletions

File tree

src/components/ColorField/ColorField.stories.tsx

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Meta, StoryFn } from "@storybook/react";
33

44
import textFieldTest from "../TextField/stories/TextField.stories";
55

6-
import { ColorField } from "./ColorField";
6+
import { ColorField, ColorFieldProps } from "./ColorField";
77

88
export default {
99
title: "Forms/ColorField",
@@ -24,10 +24,46 @@ Default.args = {
2424

2525
export const NoPalettePresets = Template.bind({});
2626
NoPalettePresets.args = {
27+
...Default.args,
2728
colorWeightFilter: [],
2829
paletteGroupFilter: [],
2930
allowCustomColor: true,
30-
onChange: (e) => {
31-
alert(e.target.value);
32-
},
31+
};
32+
33+
interface TemplateColorHashProps
34+
extends Pick<ColorFieldProps, "onChange" | "allowCustomColor" | "colorWeightFilter" | "paletteGroupFilter"> {
35+
stringForColorHashValue: string;
36+
}
37+
38+
const TemplateColorHash: StoryFn<TemplateColorHashProps> = (args: TemplateColorHashProps) => (
39+
<ColorField
40+
allowCustomColor={args.allowCustomColor}
41+
colorWeightFilter={args.colorWeightFilter}
42+
paletteGroupFilter={args.paletteGroupFilter}
43+
value={ColorField.calculateColorHashValue(args.stringForColorHashValue, {
44+
allowCustomColor: args.allowCustomColor,
45+
colorWeightFilter: args.colorWeightFilter,
46+
paletteGroupFilter: args.paletteGroupFilter,
47+
})}
48+
/>
49+
);
50+
51+
/**
52+
* Component provides a helper function to calculate a color hash from a text,
53+
* that can be used as `value` or `defaultValue`.
54+
*
55+
* ```
56+
* <ColorField value={ColorField.calculateColorHashValue("MyText")} />
57+
* ```
58+
*
59+
* You can add `options` to set the config for the color palette filters.
60+
* The same default values like on `ColorField` are used for them.
61+
*/
62+
export const ColorHashValue = TemplateColorHash.bind({});
63+
ColorHashValue.args = {
64+
...Default.args,
65+
allowCustomColor: true,
66+
colorWeightFilter: [300, 500, 700],
67+
paletteGroupFilter: ["layout", "extra"],
68+
stringForColorHashValue: "My text that will used to create a color hash as initial value.",
3369
};

src/components/ColorField/ColorField.tsx

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export interface ColorFieldProps extends Omit<TextFieldProps, "invisibleCharacte
2929
}
3030

3131
/**
32-
* Text input field.
32+
* Color input field that provides resets from the configured color palette.
33+
* Use `colorWeightFilter` and `paletteGroupFilter` to filter them.
3334
*/
3435
export const ColorField = ({
3536
className = "",
@@ -164,4 +165,36 @@ export const ColorField = ({
164165
);
165166
};
166167

168+
type calculateColorHashValueProps = Pick<
169+
ColorFieldProps,
170+
"allowCustomColor" | "colorWeightFilter" | "paletteGroupFilter"
171+
>;
172+
173+
/**
174+
* Simple helper function that provide simple access to color hash calculation.
175+
* Using the same default values for the color palette filter.
176+
*/
177+
ColorField.calculateColorHashValue = (
178+
text: string,
179+
options: calculateColorHashValueProps = {
180+
allowCustomColor: false,
181+
colorWeightFilter: [100, 300, 700, 900],
182+
paletteGroupFilter: ["layout"],
183+
}
184+
) => {
185+
const hash = utils.textToColorHash({
186+
text,
187+
options: {
188+
returnValidColorsDirectly: options.allowCustomColor as boolean,
189+
enabledColors: utils.getEnabledColorsFromPalette({
190+
includePaletteGroup: options.paletteGroupFilter,
191+
includeColorWeight: options.colorWeightFilter,
192+
minimalColorDistance: 0,
193+
}),
194+
},
195+
});
196+
197+
return hash ? hash : undefined;
198+
};
199+
167200
export default ColorField;

0 commit comments

Comments
 (0)