-
Notifications
You must be signed in to change notification settings - Fork 3
Provide ColorField input (CMEM-7327) #374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
haschek
wants to merge
12
commits into
develop
Choose a base branch
from
feature/colorWheelInput-CMEM-7327
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
db14876
add option to hide radio button indicator
haschek bb814f4
add helper function to get palette CSS colors including its custom pr…
haschek 4f54ebf
add ColorField component
haschek 41d6c76
Merge remote-tracking branch 'origin/develop' into feature/colorWheel…
haschek 190e1fa
Merge branch 'develop' into feature/colorWheelInput-CMEM-7327
haschek e99fa43
Merge remote-tracking branch 'origin/develop' into feature/colorWheel…
haschek 5f2efcd
add color hash helper function/story and fix component description
haschek 8a6a1ac
fix ColorField export
haschek aa9454b
align property names with color hash helper functions
haschek b0df565
fix markdown list
haschek 9cf88ab
fix properties use din story
haschek b7d5717
add Jest tests for ColorField
haschek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import React from "react"; | ||
| import { Meta, StoryFn } from "@storybook/react"; | ||
|
|
||
| import textFieldTest from "../TextField/stories/TextField.stories"; | ||
|
|
||
| import { ColorField, ColorFieldProps } from "./ColorField"; | ||
|
|
||
| export default { | ||
| title: "Forms/ColorField", | ||
| component: ColorField, | ||
| argTypes: { | ||
| ...textFieldTest.argTypes, | ||
| }, | ||
| } as Meta<typeof ColorField>; | ||
|
|
||
| const Template: StoryFn<typeof ColorField> = (args) => <ColorField {...args}></ColorField>; | ||
|
|
||
| export const Default = Template.bind({}); | ||
| Default.args = { | ||
| onChange: (e) => { | ||
| alert(e.target.value); | ||
| }, | ||
| }; | ||
|
|
||
| export const NoPalettePresets = Template.bind({}); | ||
| NoPalettePresets.args = { | ||
| ...Default.args, | ||
| includeColorWeight: [], | ||
| includePaletteGroup: [], | ||
| allowCustomColor: true, | ||
| }; | ||
|
|
||
| interface TemplateColorHashProps | ||
| extends Pick<ColorFieldProps, "onChange" | "allowCustomColor" | "includeColorWeight" | "includePaletteGroup"> { | ||
| stringForColorHashValue: string; | ||
| } | ||
|
|
||
| const TemplateColorHash: StoryFn<TemplateColorHashProps> = (args: TemplateColorHashProps) => ( | ||
| <ColorField | ||
| allowCustomColor={args.allowCustomColor} | ||
| includeColorWeight={args.includeColorWeight} | ||
| includePaletteGroup={args.includePaletteGroup} | ||
| value={ColorField.calculateColorHashValue(args.stringForColorHashValue, { | ||
| allowCustomColor: args.allowCustomColor, | ||
| includeColorWeight: args.includeColorWeight, | ||
| includePaletteGroup: args.includePaletteGroup, | ||
| })} | ||
| /> | ||
| ); | ||
|
|
||
| /** | ||
| * Component provides a helper function to calculate a color hash from a text, | ||
| * that can be used as `value` or `defaultValue`. | ||
| * | ||
| * ``` | ||
| * <ColorField value={ColorField.calculateColorHashValue("MyText")} /> | ||
| * ``` | ||
| * | ||
| * You can add `options` to set the config for the color palette filters. | ||
| * The same default values like on `ColorField` are used for them. | ||
| */ | ||
| export const ColorHashValue = TemplateColorHash.bind({}); | ||
| ColorHashValue.args = { | ||
| ...Default.args, | ||
| allowCustomColor: true, | ||
| includeColorWeight: [300, 500, 700], | ||
| includePaletteGroup: ["layout", "extra"], | ||
| stringForColorHashValue: "My text that will used to create a color hash as initial value.", | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import React from "react"; | ||
| import { render } from "@testing-library/react"; | ||
| import userEvent from "@testing-library/user-event"; | ||
|
|
||
| import "@testing-library/jest-dom"; | ||
|
|
||
| import { CLASSPREFIX as eccgui } from "../../configuration/constants"; | ||
|
|
||
| import { ColorField } from "./ColorField"; | ||
|
|
||
| describe("ColorField", () => { | ||
| describe("rendering", () => { | ||
| it("renders without crashing, and correct component CSS class is applied", () => { | ||
| const { container } = render(<ColorField />); | ||
| expect(container).not.toBeEmptyDOMElement(); | ||
| expect(container.getElementsByClassName(`${eccgui}-colorfield`).length).toBe(1); | ||
| }); | ||
|
|
||
| it("renders a color input by default (no palette presets)", () => { | ||
| const { container } = render( | ||
| <ColorField includePaletteGroup={[]} includeColorWeight={[]} allowCustomColor={true} /> | ||
| ); | ||
| expect(container.querySelector("input[type='color']")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| // Jest cannot test this because it does not (cannot) load Styles where the palette isconfigured | ||
| /* | ||
| it("renders a readonly text input when palette colors are configured, and custom picker CSS class is applied", () => { | ||
| const { container } = render(<ColorField className="my-custom-class" />); | ||
| // With default palette settings, a text input with readOnly is shown | ||
| expect(container.querySelector("input[type='text']")).toBeInTheDocument(); | ||
| expect(container.querySelector("input[readonly]")).toBeInTheDocument(); | ||
| expect(container.querySelector(`.${eccgui}-colorfield--custom-picker`)).toBeInTheDocument(); | ||
| }); | ||
| */ | ||
|
|
||
| it("applies additional className", () => { | ||
| render( | ||
| <ColorField | ||
| className="my-custom-class" | ||
| includePaletteGroup={[]} | ||
| includeColorWeight={[]} | ||
| allowCustomColor={true} | ||
| /> | ||
| ); | ||
| expect(document.querySelector(".my-custom-class")).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("value handling", () => { | ||
| it("uses defaultValue as initial color", () => { | ||
| render( | ||
| <ColorField | ||
| defaultValue="#ff0000" | ||
| includePaletteGroup={[]} | ||
| includeColorWeight={[]} | ||
| allowCustomColor={true} | ||
| /> | ||
| ); | ||
| const input = document.querySelector("input") as HTMLInputElement; | ||
| expect(input.value).toBe("#ff0000"); | ||
| }); | ||
|
|
||
| it("uses value prop as initial color", () => { | ||
| render( | ||
| <ColorField value="#00ff00" includePaletteGroup={[]} includeColorWeight={[]} allowCustomColor={true} /> | ||
| ); | ||
| const input = document.querySelector("input") as HTMLInputElement; | ||
| expect(input.value).toBe("#00ff00"); | ||
| }); | ||
|
|
||
| it("falls back to #000000 when no value or defaultValue is provided", () => { | ||
| render(<ColorField includePaletteGroup={[]} includeColorWeight={[]} allowCustomColor={true} />); | ||
| const input = document.querySelector("input") as HTMLInputElement; | ||
| expect(input.value).toBe("#000000"); | ||
| }); | ||
|
|
||
| it("updates displayed value when value prop changes", () => { | ||
| const { rerender } = render( | ||
| <ColorField value="#ff0000" includePaletteGroup={[]} includeColorWeight={[]} allowCustomColor={true} /> | ||
| ); | ||
| let input = document.querySelector("input") as HTMLInputElement; | ||
| expect(input.value).toBe("#ff0000"); | ||
|
|
||
| rerender( | ||
| <ColorField value="#0000ff" includePaletteGroup={[]} includeColorWeight={[]} allowCustomColor={true} /> | ||
| ); | ||
| input = document.querySelector("input") as HTMLInputElement; | ||
| expect(input.value).toBe("#0000ff"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("disabled state", () => { | ||
| it("is disabled when disabled prop is true", () => { | ||
| render(<ColorField disabled includePaletteGroup={[]} includeColorWeight={[]} allowCustomColor={true} />); | ||
| const input = document.querySelector("input") as HTMLInputElement; | ||
| expect(input).toBeDisabled(); | ||
| }); | ||
|
|
||
| it("is disabled when no palette colors and allowCustomColor is false", () => { | ||
| render(<ColorField includePaletteGroup={[]} includeColorWeight={[]} allowCustomColor={false} />); | ||
| const input = document.querySelector("input") as HTMLInputElement; | ||
| expect(input).toBeDisabled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("onChange callback", () => { | ||
| it("calls onChange when native color input changes", async () => { | ||
| const user = userEvent.setup(); | ||
| const onChange = jest.fn(); | ||
| render( | ||
| <ColorField | ||
| onChange={onChange} | ||
| includePaletteGroup={[]} | ||
| includeColorWeight={[]} | ||
| allowCustomColor={true} | ||
| /> | ||
| ); | ||
| const input = document.querySelector("input[type='color']") as HTMLInputElement; | ||
| input.type = "text"; // for unknown reasons Jest seems not able to test it on color inputs | ||
| await user.type(input, "#123456"); | ||
| expect(onChange).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
string[][]is there perhaps a better type for this?