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

Table Block: Add toolbar button to add a caption #47984

Merged
merged 5 commits into from
Jul 4, 2024
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
35 changes: 11 additions & 24 deletions packages/block-library/src/table/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
useBlockProps,
__experimentalUseColorProps as useColorProps,
__experimentalUseBorderProps as useBorderProps,
__experimentalGetElementClassName,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import {
Expand All @@ -41,7 +40,6 @@ import {
tableRowDelete,
table,
} from '@wordpress/icons';
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';

/**
* Internal dependencies
Expand All @@ -57,6 +55,7 @@ import {
toggleSection,
isEmptyTableSection,
} from './state';
import { Caption } from '../utils/caption';

const ALIGNMENT_CONTROLS = [
{
Expand Down Expand Up @@ -98,7 +97,7 @@ function TableEdit( {
insertBlocksAfter,
isSelected,
} ) {
const { hasFixedLayout, caption, head, foot } = attributes;
const { hasFixedLayout, head, foot } = attributes;
const [ initialRowCount, setInitialRowCount ] = useState( 2 );
const [ initialColumnCount, setInitialColumnCount ] = useState( 2 );
const [ selectedCell, setSelectedCell ] = useState();
Expand Down Expand Up @@ -523,27 +522,7 @@ function TableEdit( {
{ renderedSections }
</table>
) }
{ ! isEmpty && (
<RichText
identifier="caption"
tagName="figcaption"
className={ __experimentalGetElementClassName( 'caption' ) }
aria-label={ __( 'Table caption text' ) }
placeholder={ __( 'Add caption' ) }
value={ caption }
onChange={ ( value ) =>
setAttributes( { caption: value } )
}
// Deselect the selected table cell when the caption is focused.
onFocus={ () => setSelectedCell() }
__unstableOnSplitAtEnd={ () =>
insertBlocksAfter(
createBlock( getDefaultBlockName() )
)
}
/>
) }
{ isEmpty && (
{ isEmpty ? (
<Placeholder
label={ __( 'Table' ) }
icon={ <BlockIcon icon={ icon } showColors /> }
Expand Down Expand Up @@ -582,6 +561,14 @@ function TableEdit( {
</Button>
</form>
</Placeholder>
) : (
<Caption
attributes={ attributes }
setAttributes={ setAttributes }
isSelected={ isSelected }
insertBlocksAfter={ insertBlocksAfter }
label={ __( 'Table caption text' ) }
/>
) }
</figure>
);
Expand Down
9 changes: 5 additions & 4 deletions test/e2e/specs/editor/blocks/table.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,11 @@ test.describe( 'Table', () => {
.locator( 'role=button[name="Create Table"i]' )
.click();

// Click the first cell and add some text.
await editor.canvas
.locator( 'role=document[name="Block: Table"i] >> figcaption' )
.click();
await editor.clickBlockToolbarButton( 'Add caption' );
const caption = editor.canvas.locator(
'role=textbox[name="Table caption text"i]'
);
await expect( caption ).toBeFocused();
await page.keyboard.type( 'Caption!' );
expect( await editor.getEditedPostContent() ).toMatchSnapshot();
} );
Expand Down
Loading