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 each directive errors and allow any iterable #67798

Merged
merged 6 commits into from
Dec 16, 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
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,54 @@
data-wp-text="context.callbackRunCount"
></data>
</div>

<hr>

<div
data-wp-interactive="directive-each"
data-testid="each-with-unset"
>
<template data-wp-each="state.eachUnset"><p data-wp-text="context.item"></p></template>
</div>
<div
data-wp-interactive="directive-each"
data-testid="each-with-null"
>
<template data-wp-each="state.eachNull"><p data-wp-text="context.item"></p></template>
</div>
<div
data-wp-interactive="directive-each"
data-testid="each-with-undefined"
>
<template data-wp-each="state.eachUndefined"><p data-wp-text="context.item"></p></template>
</div>
<div
data-wp-interactive="directive-each"
data-testid="each-with-array"
>
<template data-wp-each="state.eachArray"><p data-wp-text="context.item"></p></template>
</div>
<div
data-wp-interactive="directive-each"
data-testid="each-with-set"
>
<template data-wp-each="state.eachSet"><p data-wp-text="context.item"></p></template>
</div>
<div
data-wp-interactive="directive-each"
data-testid="each-with-string"
>
<template data-wp-each="state.eachString"><p data-wp-text="context.item"></p></template>
</div>
<div
data-wp-interactive="directive-each"
data-testid="each-with-generator"
>
<template data-wp-each="state.eachGenerator"><p data-wp-text="context.item"></p></template>
</div>
<div
data-wp-interactive="directive-each"
data-testid="each-with-iterator"
>
<template data-wp-each="state.eachIterator"><p data-wp-text="context.item"></p></template>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@ const { state } = store( 'directive-each' );
store( 'directive-each', {
state: {
letters: [ 'A', 'B', 'C' ],
eachUndefined: undefined,
eachNull: null,
eachArray: [ 'an', 'array' ],
eachSet: new Set( [ 'a', 'set' ] ),
eachString: 'str',
*eachGenerator() {
yield 'a';
yield 'generator';
},
eachIterator: {
[ Symbol.iterator ]() {
const vals = [ 'implements', 'iterator' ];
let i = 0;
return {
next() {
return i < vals.length
? { value: vals[ i++ ], done: false }
: { done: true };
},
};
},
},
Comment on lines +25 to +37
Copy link
Member Author

Choose a reason for hiding this comment

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

I'll admit, this is a bit esoteric, but it doesn't work in the iAPI but it seems like it could. I suspect its iterator is lost in the proxies.

},
} );

Expand Down
8 changes: 8 additions & 0 deletions packages/interactivity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## Unreleased

### Enhancements

- Allow more iterables to be used in each directives ([#67798](https://github.com/WordPress/gutenberg/pull/67798)).

### Bug Fixes

- Fix an error when the value used in an each directive is not iterable ([#67798](https://github.com/WordPress/gutenberg/pull/67798)).

## 6.14.0 (2024-12-11)

## 6.13.0 (2024-11-27)
Expand Down
27 changes: 19 additions & 8 deletions packages/interactivity/src/directives.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* External dependencies
*/
import { h as createElement, type RefObject } from 'preact';
import { h as createElement, type VNode, type RefObject } from 'preact';
import { useContext, useMemo, useRef } from 'preact/hooks';

/**
Expand Down Expand Up @@ -567,11 +567,19 @@ export default () => {
const [ entry ] = each;
const { namespace } = entry;

const list = evaluate( entry );
const iterable = evaluate( entry );

if ( typeof iterable?.[ Symbol.iterator ] !== 'function' ) {
return;
}

const itemProp = isNonDefaultDirectiveSuffix( entry )
? kebabToCamelCase( entry.suffix )
: 'item';
return list.map( ( item ) => {

const result: VNode< any >[] = [];

for ( const item of iterable ) {
const itemContext = proxifyContext(
proxifyState( namespace, {} ),
inheritedValue.client[ namespace ]
Expand All @@ -596,12 +604,15 @@ export default () => {
? getEvaluate( { scope } )( eachKey[ 0 ] )
: item;

return createElement(
Provider,
{ value: mergedContext, key },
element.props.content
result.push(
createElement(
Provider,
{ value: mergedContext, key },
element.props.content
)
);
} );
}
return result;
},
{ priority: 20 }
);
Expand Down
2 changes: 1 addition & 1 deletion packages/interactivity/src/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ interface DirectiveArgs {
}

export interface DirectiveCallback {
( args: DirectiveArgs ): VNode< any > | null | void;
( args: DirectiveArgs ): VNode< any > | VNode< any >[] | null | void;
}

interface DirectiveOptions {
Expand Down
35 changes: 34 additions & 1 deletion test/e2e/specs/interactivity/directive-each.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test.describe( 'data-wp-each', () => {
await utils.deleteAllPosts();
} );

test( 'should use `item` as the defaul item name in the context', async ( {
test( 'should use `item` as the default item name in the context', async ( {
page,
} ) => {
const elements = page.getByTestId( 'letters' ).getByTestId( 'item' );
Expand Down Expand Up @@ -500,4 +500,37 @@ test.describe( 'data-wp-each', () => {
await expect( element ).toHaveText( 'beta' );
await expect( callbackRunCount ).toHaveText( '1' );
} );

for ( const testId of [
'each-with-unset',
'each-with-null',
'each-with-undefined',
] ) {
test( `does not error with non-iterable values: ${ testId }`, async ( {
page,
} ) => {
await expect( page.getByTestId( testId ) ).toBeEmpty();
} );
}

for ( const [ testId, values ] of [
[ 'each-with-array', [ 'an', 'array' ] ],
[ 'each-with-set', [ 'a', 'set' ] ],
[ 'each-with-string', [ 's', 't', 'r' ] ],
[ 'each-with-generator', [ 'a', 'generator' ] ],

// TODO: Is there a problem with proxies here?
// [ 'each-with-iterator', [ 'implements', 'iterator' ] ],
Comment on lines +522 to +523
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it still the case that there is a problem with proxies? I see that tests are passing in the CI.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I realized you meant there's a problem only for the each-with-iterator case which is commented out.

Copy link
Member Author

@sirreal sirreal Dec 16, 2024

Choose a reason for hiding this comment

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

That's right, it's commented out because it would not pass.

I didn't spend much time debugging the problem so I'm not sure what the issue is. It's some thing that apparently should work but does not, I'm not sure of the cause and don't really plan to spend time debugging it, maybe @DAreRodz know if this would create issues with store proxies.

] as const ) {
test( `support different each iterable values: ${ testId }`, async ( {
page,
} ) => {
const element = page.getByTestId( testId );
for ( const value of values ) {
await expect(
element.getByText( value, { exact: true } )
).toBeVisible();
}
} );
}
} );
Loading