Skip to content

Commit

Permalink
Use GroupItemOptions for ToggleNode, see #798
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Oct 11, 2022
1 parent 3877640 commit ee7c87f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions js/BooleanToggleNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export default class BooleanToggleNode extends ToggleNode<boolean> {
falseNode: Node,
providedOptions?: BooleanToggleNodeOptions ) {
super( booleanProperty, [
{ value: true, node: trueNode },
{ value: false, node: falseNode }
{ value: true, createNode: tandem => trueNode },
{ value: false, createNode: tandem => falseNode }
], providedOptions );
}
}
Expand Down
10 changes: 6 additions & 4 deletions js/ToggleNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import { Node, NodeOptions } from '../../scenery/js/imports.js';
import Tandem from '../../tandem/js/Tandem.js';
import sun from './sun.js';
import TReadOnlyProperty from '../../axon/js/TReadOnlyProperty.js';
import GroupItemOptions, { getGroupItemNodes } from './GroupItemOptions.js';

export type ToggleNodeElement<T> = {
value: T; // a value
node: Node; // the Node associated with the value, to be shown by the ToggleNode
};
} & GroupItemOptions;

type SelfOptions = {

Expand Down Expand Up @@ -52,12 +52,14 @@ export default class ToggleNode<T> extends Node {
tandem: Tandem.OPTIONAL
}, providedOptions );

const nodes = getGroupItemNodes( elements, options.tandem.createTandem( 'elements' ) );

const valueListener = ( value: T ) => {
let matchCount = 0;
for ( let i = 0; i < elements.length; i++ ) {
const element = elements[ i ];
const visible = element.value === value;
element.node.visible = visible;
nodes[ i ].visible = visible;
if ( visible ) {
matchCount++;
}
Expand All @@ -66,7 +68,7 @@ export default class ToggleNode<T> extends Node {
};
valueProperty.link( valueListener );

options.children = _.map( elements, 'node' );
options.children = nodes;

options.alignChildren( options.children );

Expand Down

0 comments on commit ee7c87f

Please sign in to comment.