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

Interactivity API: Prevent the use of components in wp-text #57879

Merged
merged 6 commits into from Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -34,4 +34,18 @@
Toggle Context Text
</button>
</div>
<div>
<span
data-wp-text="state.component"
data-testid="show state component"
></span>
<span
data-wp-text="state.number"
data-testid="show state number"
></span>
<span
data-wp-text="state.boolean"
data-testid="show state boolean"
></span>
</div>
</div>
@@ -1,11 +1,14 @@
/**
* WordPress dependencies
*/
import { store, getContext } from '@wordpress/interactivity';
import { store, getContext, createElement } from '@wordpress/interactivity';

const { state } = store( 'directive-context', {
state: {
text: 'Text 1',
component: () => (createElement( 'div', {}, state.text )),
number: 1,
boolean: true
},
actions: {
toggleStateText() {
Expand Down
4 changes: 4 additions & 0 deletions packages/interactivity/CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Enhancements

- Allow only strings for `wp-text`. ([#57879](https://github.com/WordPress/gutenberg/pull/57879))
luisherranz marked this conversation as resolved.
Show resolved Hide resolved

### New Features

- Add the `data-wp-run` directive along with the `useInit` and `useWatch` hooks. ([57805](https://github.com/WordPress/gutenberg/pull/57805))
Expand Down
4 changes: 3 additions & 1 deletion packages/interactivity/src/directives.js
Expand Up @@ -330,7 +330,9 @@ export default () => {
// data-wp-text
directive( 'text', ( { directives: { text }, element, evaluate } ) => {
const entry = text.find( ( { suffix } ) => suffix === 'default' );
element.props.children = evaluate( entry );
const evaluatedEntry = evaluate( entry );
if ( typeof evaluatedEntry !== 'string' ) return null;
element.props.children = evaluatedEntry;
cbravobernal marked this conversation as resolved.
Show resolved Hide resolved
} );

// data-wp-slot
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/specs/interactivity/directives-text.spec.ts
Expand Up @@ -33,4 +33,13 @@ test.describe( 'data-wp-text', () => {
await page.getByTestId( 'toggle context text' ).click();
await expect( el ).toHaveText( 'Text 1' );
} );

test( 'work only with strings', async ( { page } ) => {
const elObject = page.getByTestId( 'show state component' );
await expect( elObject ).toBeHidden();
const elNumber = page.getByTestId( 'show state number' );
await expect( elNumber ).toBeHidden();
const elBool = page.getByTestId( 'show state boolean' );
await expect( elBool ).toBeHidden();
} );
} );