Skip to content

Commit

Permalink
fix: Display heading level dropdown icons and labels (#52004)
Browse files Browse the repository at this point in the history
* fix: Heading level dropdown displays active selection

A recent refactor for web was only partially applied to the sibling
native files. This finishes the application so that the active selection
is displayed, passed through via the `value` prop.

* fix: Heading level dropdown leverages Icon

Without using Icon, the SVG elements rendered without a width, causing
layout issues and invisible icons.
  • Loading branch information
dcalhoun authored Jun 28, 2023
1 parent 2ecd216 commit 6771ba4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
headingLevel6,
paragraph,
} from '@wordpress/icons';
import { Icon } from '@wordpress/components';

/** @typedef {import('@wordpress/element').WPComponent} WPComponent */

Expand Down Expand Up @@ -39,5 +40,9 @@ const LEVEL_TO_PATH = {
* @return {?WPComponent} The icon.
*/
export default function HeadingLevelIcon( { level } ) {
return LEVEL_TO_PATH[ level ] ?? null;
if ( LEVEL_TO_PATH[ level ] ) {
return <Icon icon={ LEVEL_TO_PATH[ level ] } />;
}

return null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ const HEADING_LEVELS = [ 1, 2, 3, 4, 5, 6 ];
*
* @return {WPComponent} The toolbar.
*/
export default function HeadingLevelDropdown( { selectedLevel, onChange } ) {
export default function HeadingLevelDropdown( {
options = HEADING_LEVELS,
value,
onChange,
} ) {
const createLevelControl = (
targetLevel,
currentLevel,
Expand All @@ -53,9 +57,9 @@ export default function HeadingLevelDropdown( { selectedLevel, onChange } ) {

return (
<DropdownMenu
icon={ <HeadingLevelIcon level={ selectedLevel } /> }
controls={ HEADING_LEVELS.map( ( index ) =>
createLevelControl( index, selectedLevel, onChange )
icon={ <HeadingLevelIcon level={ value } /> }
controls={ options.map( ( index ) =>
createLevelControl( index, value, onChange )
) }
label={ __( 'Change level' ) }
/>
Expand Down
18 changes: 18 additions & 0 deletions packages/block-library/src/heading/test/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,22 @@ describe( 'Heading block', () => {
// Assert
expect( getEditorHtml() ).toMatchSnapshot();
} );

it( 'change level dropdown displays active selection', async () => {
// Arrange
const screen = await initializeEditor();
await addBlock( screen, 'Heading' );
const headingBlock = await getBlock( screen, 'Heading' );

// Act
fireEvent.press( headingBlock );
fireEvent.press( screen.getByLabelText( 'Change level' ) );

// Assert
expect(
within( screen.getByLabelText( 'Heading 2' ) ).getByTestId(
'bottom-sheet-cell-selected-icon'
)
).toBeVisible();
} );
} );
1 change: 1 addition & 0 deletions packages/components/src/mobile/bottom-sheet/cell.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ class BottomSheetCell extends Component {
<Icon
icon={ check }
fill={ platformStyles.isSelected.color }
testID="bottom-sheet-cell-selected-icon"
/>
) }
{ showValue && getValueComponent() }
Expand Down

0 comments on commit 6771ba4

Please sign in to comment.