-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Interactivity API: Support for the data-wp-key
directive
#53844
Merged
luisherranz
merged 9 commits into
add/interactivity-api-regions-based-client-side-navigation
from
interactivity-api-wp-key-directive
Aug 22, 2023
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
76a63a1
Add failing test
luisherranz c067612
Fix test using key
luisherranz b0017ce
Replace key with data-wp-key
luisherranz 6545b5e
Refactor test a bit
luisherranz 6c9dfb4
Add changelog
luisherranz aa08d7d
Add docs
luisherranz 9dd4ff1
Remove unnecessary paragraph
luisherranz 5ca3702
Fix lint error
luisherranz cae548c
Merge branch 'add/interactivity-api-regions-based-client-side-navigat…
luisherranz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
packages/e2e-tests/plugins/interactive-blocks/directive-key/block.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"apiVersion": 2, | ||
"name": "test/directive-key", | ||
"title": "E2E Interactivity tests - directive key", | ||
"category": "text", | ||
"icon": "heart", | ||
"description": "", | ||
"supports": { | ||
"interactivity": true | ||
}, | ||
"textdomain": "e2e-interactivity", | ||
"viewScript": "directive-key-view", | ||
"render": "file:./render.php" | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/e2e-tests/plugins/interactive-blocks/directive-key/render.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
/** | ||
* HTML for testing the directive `data-wp-key`. | ||
* | ||
* @package gutenberg-test-interactive-blocks | ||
*/ | ||
?> | ||
|
||
<div data-wp-interactive data-wp-navigation-id="some-id"> | ||
<ul> | ||
<li data-wp-key="id-2" data-testid="first-item">2</li> | ||
<li data-wp-key="id-3">3</li> | ||
</ul> | ||
<button data-testid="navigate" data-wp-on--click="actions.navigate"> | ||
Navigate | ||
</button> | ||
</div> |
23 changes: 23 additions & 0 deletions
23
packages/e2e-tests/plugins/interactive-blocks/directive-key/view.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
( ( { wp } ) => { | ||
const { store, navigate } = wp.interactivity; | ||
|
||
const html = ` | ||
<div data-wp-interactive data-wp-navigation-id="some-id"> | ||
<ul> | ||
<li data-wp-key="id-1">1</li> | ||
<li data-wp-key="id-2" data-testid="second-item">2</li> | ||
<li data-wp-key="id-3">3</li> | ||
</ul> | ||
</div>`; | ||
|
||
store( { | ||
actions: { | ||
navigate: () => { | ||
navigate( window.location, { | ||
force: true, | ||
html, | ||
} ); | ||
}, | ||
}, | ||
} ); | ||
} )( window ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { test, expect } from './fixtures'; | ||
|
||
test.describe( 'data-wp-key', () => { | ||
test.beforeAll( async ( { interactivityUtils: utils } ) => { | ||
await utils.activatePlugins(); | ||
await utils.addPostWithBlock( 'test/directive-key' ); | ||
} ); | ||
|
||
test.beforeEach( async ( { interactivityUtils: utils, page } ) => { | ||
await page.goto( utils.getLink( 'test/directive-key' ) ); | ||
} ); | ||
|
||
test.afterAll( async ( { interactivityUtils: utils } ) => { | ||
await utils.deactivatePlugins(); | ||
await utils.deleteAllPosts(); | ||
} ); | ||
|
||
test( 'should keep the elements when adding items to the start of the array', async ( { | ||
page, | ||
} ) => { | ||
// Add a number to the node so we can check later that it is still there. | ||
await page | ||
.getByTestId( 'first-item' ) | ||
.evaluate( ( n ) => ( ( n as any )._id = 123 ) ); | ||
await page.getByTestId( 'navigate' ).click(); | ||
const id = await page | ||
.getByTestId( 'second-item' ) | ||
.evaluate( ( n ) => ( n as any )._id ); | ||
expect( id ).toBe( 123 ); | ||
} ); | ||
} ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any example we can add here to understand better the value of
data-wp-key
? I mean, some example that actually rearranges elements, insert them or remove them where having a key is essential.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now, I think it's useful only when doing client-side navigation. So maybe we can add that in #53733.
Ok, that makes sense. If you don't mind, let's merge this as it is because it's merged against #53733, and let's add a better example of
data-wp-key
in the docs for that PR.