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

Implement the nested block behavior in list block v2 #39487

Merged
merged 3 commits into from
Mar 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
9 changes: 9 additions & 0 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,15 @@ Create a bulleted or numbered list. ([Source](https://github.com/WordPress/guten
- **Supports:** __unstablePasteTextInline, anchor, color (background, gradients, link, text), typography (fontSize, lineHeight), ~~className~~
- **Attributes:** ordered, placeholder, reversed, start, type, values

## List item

Create a list item. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/list-item))

- **Name:** core/list-item
- **Category:** text
- **Supports:** ~~className~~
- **Attributes:** content, placeholder

## Login/out

Show login & logout links. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/loginout))
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import * as image from './image';
import * as latestComments from './latest-comments';
import * as latestPosts from './latest-posts';
import * as list from './list';
import * as listItem from './list-item';
import * as logInOut from './loginout';
import * as mediaText from './media-text';
import * as missing from './missing';
Expand Down Expand Up @@ -135,6 +136,7 @@ export const __experimentalGetCoreBlocks = () => [
heading,
gallery,
list,
listItem,
quote,

// Register all remaining core blocks.
Expand Down
26 changes: 26 additions & 0 deletions packages/block-library/src/list-item/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "core/list-item",
"title": "List item",
"category": "text",
"parent": [ "core/list" ],
"description": "Create a list item.",
"textdomain": "default",
"attributes": {
"placeholder": {
"type": "string"
},
"content": {
"type": "string",
"source": "html",
"selector": "li",
Copy link
Contributor

Choose a reason for hiding this comment

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

Indentation here is off.

"default": "",
"__experimentalRole": "content"
}
},
"supports": {
"className": false,
"__experimentalSelector": "li"
}
}
32 changes: 32 additions & 0 deletions packages/block-library/src/list-item/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* WordPress dependencies
*/
import {
RichText,
useBlockProps,
useInnerBlocksProps,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';

export default function ListItemEdit( { attributes, setAttributes } ) {
const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps( blockProps, {
allowedBlocks: [ 'core/list' ],
} );
Comment on lines +13 to +15
Copy link
Member

@ZebulanStanphill ZebulanStanphill Mar 16, 2022

Choose a reason for hiding this comment

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

Nitpick: { allowedBlocks: [ 'core/list' ] } could be moved to a constant outside of the component to avoid recreating it on every render.


return (
<li { ...innerBlocksProps }>
<RichText
identifier="content"
tagName="div"
onChange={ ( nextContent ) =>
setAttributes( { content: nextContent } )
}
value={ attributes.content }
aria-label={ __( 'List text' ) }
placeholder={ attributes.placeholder || __( 'List' ) }
/>
{ innerBlocksProps.children }
</li>
);
}
21 changes: 21 additions & 0 deletions packages/block-library/src/list-item/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* WordPress dependencies
*/
import { list as icon } from '@wordpress/icons';

/**
* Internal dependencies
*/
import metadata from './block.json';
import edit from './edit';
import save from './save';

const { name } = metadata;

export { metadata, name };

export const settings = {
icon,
edit,
save,
};
13 changes: 13 additions & 0 deletions packages/block-library/src/list-item/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* WordPress dependencies
*/
import { InnerBlocks, RichText, useBlockProps } from '@wordpress/block-editor';

export default function save( { attributes } ) {
return (
<li { ...useBlockProps.save() }>
<RichText.Content value={ attributes.content } />
<InnerBlocks.Content />
</li>
);
}
15 changes: 14 additions & 1 deletion packages/block-library/src/list/v2/edit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
/**
* WordPress dependencies
*/
import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor';

const TEMPLATE = [ [ 'core/list-item' ] ];

function Edit() {
return <div>List block v2</div>;
const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps( blockProps, {
allowedBlocks: [ 'core/list-item' ],
template: TEMPLATE,
} );

return <ul { ...innerBlocksProps } />;
}

export default Edit;
15 changes: 11 additions & 4 deletions packages/block-library/src/list/v2/save.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
function Save() {
return <div>List block v2</div>;
}
/**
* WordPress dependencies
*/
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';

export default Save;
export default function save() {
return (
<ul { ...useBlockProps.save() }>
<InnerBlocks.Content />
Copy link
Member

Choose a reason for hiding this comment

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

Could we add some checks to return null if there are no inner blocks? It is very easy to create an empty list item for the user is very hard to remove. Or maybe we can automatically remove the list block if it becomes empty?

Copy link
Member

Choose a reason for hiding this comment

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

we should add a shortcut later that removes it on backspace

</ul>
);
}
1 change: 1 addition & 0 deletions test/integration/fixtures/blocks/core__list-item.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:list-item --><li></li><!-- /wp:list-item -->
10 changes: 10 additions & 0 deletions test/integration/fixtures/blocks/core__list-item.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"name": "core/list-item",
"isValid": true,
"attributes": {
"content": ""
},
"innerBlocks": []
}
]
9 changes: 9 additions & 0 deletions test/integration/fixtures/blocks/core__list-item.parsed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"blockName": "core/list-item",
"attrs": {},
"innerBlocks": [],
"innerHTML": "<li></li>",
"innerContent": [ "<li></li>" ]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:list-item -->
<li></li>
<!-- /wp:list-item -->