Skip to content

Commit

Permalink
Add wp-each directive in the new store API
Browse files Browse the repository at this point in the history
  • Loading branch information
SantosGuillamot authored and DAreRodz committed Nov 14, 2023
1 parent c1bbf9f commit 51bf723
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packages/interactivity/src/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,48 @@ export default () => {
),
{ priority: 4 }
);

// data-wp-each
directive(
'each',
( {
directives: { each, 'each-key': key },
context: inheritedContext,
element,
evaluate,
} ) => {
const { Provider } = inheritedContext;
const inheritedValue = useContext( inheritedContext );

const entry = each
.filter( ( n ) => n !== 'default' )
.find( ( n ) => n );

const list = evaluate( entry );

if ( ! list.length ) return null;

return list.map( ( item ) => {
const currentValue = useRef( deepSignal( {} ) );

currentValue.current = useMemo( () => {
const newValue = deepSignal( {
[ entry.namespace ]: { [ entry.suffix ]: item },
} );
mergeDeepSignals( newValue, inheritedValue );
mergeDeepSignals( currentValue.current, newValue, true );
return currentValue.current;
}, [ inheritedValue, entry ] );

return (
<Provider
value={ currentValue.current }
key={ item[ key[ 0 ].value ] }
>
{ element.props.children[ 1 ] }
</Provider>
);
} );
}
);
};

0 comments on commit 51bf723

Please sign in to comment.