From 8225d471f841d1cd65a4541331301037fc13c5e1 Mon Sep 17 00:00:00 2001 From: Dominik Porada Date: Fri, 14 May 2021 19:12:14 +0200 Subject: [PATCH] Add support for `placeholder` in `AlignmentToolbar` component --- .../components/alignment-control/README.md | 7 ++ .../test/__snapshots__/index.js.snap | 76 +++++++++++++++++++ .../alignment-control/test/index.js | 11 +++ .../src/components/alignment-control/ui.js | 8 ++ 4 files changed, 102 insertions(+) diff --git a/packages/block-editor/src/components/alignment-control/README.md b/packages/block-editor/src/components/alignment-control/README.md index 24aa31aef9dfa6..d521a23c23b2ba 100644 --- a/packages/block-editor/src/components/alignment-control/README.md +++ b/packages/block-editor/src/components/alignment-control/README.md @@ -44,6 +44,13 @@ _Note:_ In this example that we render `AlignmentControl` as a child of the `Blo The current value of the alignment setting. You may only choose from the `Options` listed above. +### `placeholder` +- **Type:** `String` +- **Default:** `left` for left-to-right languages, `right` for right-to-left languages +- **Options:**: `left`, `center`, `right` + +The placeholder alignment value that will be reflected by the toolbar icon when the value is `undefined`. If the content inside your block is aligned to center when no specific `value` is defined, you should set `placeholder` to `center`. + ### `onChange` - **Type:** `Function` diff --git a/packages/block-editor/src/components/alignment-control/test/__snapshots__/index.js.snap b/packages/block-editor/src/components/alignment-control/test/__snapshots__/index.js.snap index 4f549af0b2dc11..445f88c5552613 100644 --- a/packages/block-editor/src/components/alignment-control/test/__snapshots__/index.js.snap +++ b/packages/block-editor/src/components/alignment-control/test/__snapshots__/index.js.snap @@ -1,5 +1,81 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`AlignmentUI should allow a custom placeholder to be specified 1`] = ` + + + , + "isActive": false, + "onClick": [Function], + "role": "menuitemradio", + "title": "Align text left", + }, + Object { + "align": "center", + "icon": + + , + "isActive": false, + "onClick": [Function], + "role": "menuitemradio", + "title": "Align text center", + }, + Object { + "align": "right", + "icon": + + , + "isActive": false, + "onClick": [Function], + "role": "menuitemradio", + "title": "Align text right", + }, + ] + } + icon={ + + + + } + label="Align" + popoverProps={ + Object { + "isAlternate": true, + "position": "bottom right", + } + } + toggleProps={ + Object { + "describedBy": "Change text alignment", + } + } +/> +`; + exports[`AlignmentUI should allow custom alignment controls to be specified 1`] = ` { expect( onChangeSpy ).toHaveBeenCalledWith( 'center' ); } ); + test( 'should allow a custom placeholder to be specified', () => { + const wrapperPlaceholder = shallow( + + ); + expect( wrapperPlaceholder ).toMatchSnapshot(); + + const wrapperIllegalPlaceholder = () => + shallow( ); + expect( wrapperIllegalPlaceholder ).not.toThrowError(); + } ); + test( 'should allow custom alignment controls to be specified', () => { const wrapperCustomControls = shallow( control.align === value ); + const placeholderAlignment = find( + alignmentControls, + ( control ) => control.align === placeholder + ); + function setIcon() { if ( activeAlignment ) return activeAlignment.icon; + if ( placeholderAlignment ) return placeholderAlignment.icon; + return isRTL() ? alignRight : alignLeft; }