Skip to content
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

ColorPalette: make sure "key" is unique when iterating over color entries with same value #43096

Merged
merged 7 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- `BaseControl`: Add `box-sizing` reset style ([#42889](https://github.com/WordPress/gutenberg/pull/42889)).
- `ToggleGroupControl`, `RangeControl`, `FontSizePicker`: Add `__nextHasNoMarginBottom` prop for opting into the new margin-free styles ([#43062](https://github.com/WordPress/gutenberg/pull/43062)).
- `BoxControl`: Export `applyValueToSides` util function. ([#42733](https://github.com/WordPress/gutenberg/pull/42733/)).
- `ColorPalette`: use index while iterating over color entries to avoid React "duplicated key" warning ([#43096](https://github.com/WordPress/gutenberg/pull/43096)).
- `AnglePickerControl`: Add `__nextHasNoMarginBottom` prop for opting into the new margin-free styles ([#43160](https://github.com/WordPress/gutenberg/pull/43160/)).

### Internal
Expand Down
12 changes: 6 additions & 6 deletions packages/components/src/color-palette/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
/**
* External dependencies
*/
import { map } from 'lodash';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 Bye bye

import { colord, extend } from 'colord';
import namesPlugin from 'colord/plugins/names';
import a11yPlugin from 'colord/plugins/a11y';
Expand Down Expand Up @@ -35,15 +34,16 @@ function SinglePalette( {
actions,
} ) {
const colorOptions = useMemo( () => {
return map( colors, ( { color, name } ) => {
return colors.map( ( { color, name }, index ) => {
const colordColor = colord( color );
const isSelected = value === color;

return (
<CircularOptionPicker.Option
key={ color }
isSelected={ value === color }
key={ `${ color }-${ index }` }
isSelected={ isSelected }
selectedIconProps={
value === color
isSelected
? {
fill:
colordColor.contrast() >
Expand All @@ -60,7 +60,7 @@ function SinglePalette( {
}
style={ { backgroundColor: color, color } }
onClick={
value === color ? clearColor : () => onChange( color )
isSelected ? clearColor : () => onChange( color )
}
aria-label={
name
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/color-palette/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ MultipleOrigins.args = {
],
},
{
name: 'Primary colors',
name: 'Secondary colors',
colors: [
{ name: 'Orange', color: '#f60' },
{ name: 'Green', color: '#0f0' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ exports[`ColorPalette should render a dynamic toolbar of colors 1`] = `
<Option
aria-label="Color: red"
isSelected={true}
key="#f00"
key="#f00-0"
onClick={[Function]}
selectedIconProps={
Object {
Expand Down Expand Up @@ -1074,7 +1074,7 @@ exports[`ColorPalette should render a dynamic toolbar of colors 1`] = `
<Option
aria-label="Color: white"
isSelected={false}
key="#fff"
key="#fff-1"
onClick={[Function]}
selectedIconProps={Object {}}
style={
Expand Down Expand Up @@ -1134,7 +1134,7 @@ exports[`ColorPalette should render a dynamic toolbar of colors 1`] = `
<Option
aria-label="Color: blue"
isSelected={false}
key="#00f"
key="#00f-2"
onClick={[Function]}
selectedIconProps={Object {}}
style={
Expand Down