Skip to content

Commit

Permalink
If, for whatever reason, the returned index does not match a color/gr…
Browse files Browse the repository at this point in the history
…adient with the same incoming value, then just open up the list view editing panel.
  • Loading branch information
ramonjd committed Nov 17, 2022
1 parent 57b6dd6 commit a901974
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions packages/components/src/palette-edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { kebabCase } from 'lodash';
/**
* WordPress dependencies
*/
import { useState, useRef, useEffect } from '@wordpress/element';
import { useState, useRef, useEffect, useCallback } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { lineSolid, moreVertical, plus } from '@wordpress/icons';
import {
Expand Down Expand Up @@ -330,6 +330,19 @@ export default function PaletteEdit( {
const elementsLength = elements.length;
const hasElements = elementsLength > 0;
const debounceOnChange = useDebounce( onChange, 100 );
const onSelectPaletteItem = useCallback(
( value, newEditingElementIndex ) => {
const selectedElement = elements[ newEditingElementIndex ];
const key = isGradient ? 'gradient' : 'color';
// Ensures that the index returned matches a known element value.
if ( !! selectedElement && selectedElement[ key ] === value ) {
setEditingElement( newEditingElementIndex );
} else {
setIsEditing( true );
}
},
[ isGradient ]
);

return (
<PaletteEditStyles>
Expand Down Expand Up @@ -495,18 +508,14 @@ export default function PaletteEdit( {
<GradientPicker
__nextHasNoMargin
gradients={ gradients }
onChange={ ( value, index ) =>
setEditingElement( index )
}
onChange={ onSelectPaletteItem }
clearable={ false }
disableCustomGradients={ true }
/>
) : (
<ColorPalette
colors={ colors }
onChange={ ( value, index ) =>
setEditingElement( index )
}
onChange={ onSelectPaletteItem }
clearable={ false }
disableCustomColors={ true }
/>
Expand Down

0 comments on commit a901974

Please sign in to comment.