Repository navigation
feat(twake-mui): Add ColorPickerCustom and ColorPicker, extend ColorList #225
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
Crash--
wants to merge
6
commits into
main
Choose a base branch
from
feat/color-list-custom-input
base: main
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.
+645
−15
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
13ee18b
feat(twake-mui): Add extra colors to ColorList
Crash-- ab0ee22
feat(twake-mui): Widen the touch area of ColorList
Crash-- b91419f
feat(twake-mui): Read the colors of ColorList as hexadecimal codes
Crash-- 1b987bd
feat(twake-mui): Add the ColorPickerCustom component
Crash-- b5d3a40
feat(twake-mui): Add a ColorPicker assembling ColorList and ColorPick…
Crash-- 0f403fa
docs(twake-mui): Add a story for the extra colors of ColorList
Crash-- 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
34 changes: 34 additions & 0 deletions
34
packages/twake-mui/src/components/ColorPicker/ColorPicker.stories.tsx
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,34 @@ | ||
| import { Paper } from '@mui/material' | ||
| import type { Meta, StoryObj } from '@storybook/react-vite' | ||
| import React, { useState } from 'react' | ||
|
|
||
| import ColorPicker, { ColorPickerProps } from './index' | ||
|
|
||
| const meta: Meta<typeof ColorPicker> = { | ||
| title: 'ColorPicker', | ||
| component: ColorPicker, | ||
| parameters: { layout: 'padded' }, | ||
| tags: ['autodocs', 'argos'], | ||
| args: { color: '#273891' } | ||
| } | ||
|
|
||
| export default meta | ||
| type Story = StoryObj<typeof meta> | ||
|
|
||
| const SelectableColorPicker: React.FC<ColorPickerProps> = props => { | ||
| const [color, setColor] = useState(props.color) | ||
| return <ColorPicker {...props} color={color} onChange={setColor} /> | ||
| } | ||
|
|
||
| export const Default: Story = { | ||
| render: args => ( | ||
| <Paper className="u-p-1 u-dib"> | ||
| <SelectableColorPicker {...args} /> | ||
| </Paper> | ||
| ) | ||
| } | ||
|
|
||
| export const OtherColor: Story = { | ||
| args: { color: '#12AB34' }, | ||
| render: Default.render | ||
| } | ||
74 changes: 74 additions & 0 deletions
74
packages/twake-mui/src/components/ColorPicker/ColorPicker.test.tsx
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,74 @@ | ||
| // @vitest-environment jsdom | ||
| import { ThemeProvider } from '@mui/material' | ||
| import { cleanup, fireEvent, render, screen } from '@testing-library/react' | ||
| import React, { useState } from 'react' | ||
| import { I18n } from 'twake-i18n' | ||
| import { afterEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import ColorPicker from './index' | ||
| import { theme } from '../../lib/theme' | ||
|
|
||
| afterEach(cleanup) | ||
|
|
||
| const Picker: React.FC<{ onChange: (color: string) => void }> = ({ | ||
| onChange | ||
| }) => { | ||
| const [color, setColor] = useState('#273891') | ||
| return ( | ||
| <ColorPicker | ||
| color={color} | ||
| onChange={picked => { | ||
| setColor(picked) | ||
| onChange(picked) | ||
| }} | ||
| /> | ||
| ) | ||
| } | ||
|
|
||
| const setup = (onChange: (color: string) => void): void => { | ||
| render( | ||
| <ThemeProvider theme={theme}> | ||
| <I18n lang="en" dictRequire={() => ({})}> | ||
| <Picker onChange={onChange} /> | ||
| </I18n> | ||
| </ThemeProvider> | ||
| ) | ||
| } | ||
|
|
||
| const customButton = (): HTMLElement => | ||
| screen.getByRole('button', { name: 'Custom color' }) | ||
|
|
||
| describe('ColorPicker', () => { | ||
| it('picks a color of the list', () => { | ||
| const onChange = vi.fn() | ||
| setup(onChange) | ||
|
|
||
| fireEvent.click(screen.getByRole('button', { name: '#7E57E3' })) | ||
| expect(onChange).toHaveBeenCalledWith('#7E57E3') | ||
| }) | ||
|
|
||
| it('opens the custom picker on its field, saves its color and comes back to its button', () => { | ||
| const onChange = vi.fn() | ||
| setup(onChange) | ||
|
|
||
| fireEvent.click(customButton()) | ||
| const field = screen.getByRole('textbox', { name: 'Hex' }) | ||
| expect(document.activeElement).toBe(field) | ||
| fireEvent.change(field, { target: { value: '#12ab34' } }) | ||
| fireEvent.click(screen.getByRole('button', { name: 'Save' })) | ||
| expect(onChange).toHaveBeenCalledWith('#12AB34') | ||
| expect(document.activeElement).toBe(customButton()) | ||
| expect(customButton().getAttribute('aria-pressed')).toBe('true') | ||
| }) | ||
|
|
||
| it('comes back to its button on cancel, the color unchanged', () => { | ||
| const onChange = vi.fn() | ||
| setup(onChange) | ||
|
|
||
| fireEvent.click(customButton()) | ||
| fireEvent.click(screen.getByRole('button', { name: 'Cancel' })) | ||
| expect(onChange).not.toHaveBeenCalled() | ||
| expect(document.activeElement).toBe(customButton()) | ||
| expect(customButton().getAttribute('aria-pressed')).toBe('false') | ||
| }) | ||
| }) |
Oops, something went wrong.
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.
Keep the story’s color control effective.
If a user changes the Storybook
colorcontrol after the story mounts,useState(props.color)retains the earlier value. The wrapper then overrides the new arg, so the control cannot demonstrate another selected color. Remount the wrapper when the arg changes, as theColorListstory does, or synchronize its state with the arg.