Skip to content

Commit

Permalink
Propose Symbol.iterator implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Dec 10, 2024
1 parent 1963b14 commit 53fb446
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
23 changes: 14 additions & 9 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 @@ -573,11 +573,13 @@ export default () => {
? kebabToCamelCase( entry.suffix )
: 'item';

if ( typeof list?.map !== 'function' ) {
return [];
if ( ! list || ! ( Symbol.iterator in list ) ) {
return;
}

return list.map( ( item ) => {
const result: VNode< any >[] = [];

for ( const item of list ) {
const itemContext = proxifyContext(
proxifyState( namespace, {} ),
inheritedValue.client[ namespace ]
Expand All @@ -602,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

0 comments on commit 53fb446

Please sign in to comment.