Skip to content

Commit

Permalink
Make RectangularRadioButton.options.accessibleName default based on a…
Browse files Browse the repository at this point in the history
… tandem name, if it is supplied, see phetsims/buoyancy#109
  • Loading branch information
samreid committed May 28, 2024
1 parent 88eb7d0 commit 1210f0a
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion js/Tandem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
import arrayRemove from '../../phet-core/js/arrayRemove.js';
import merge from '../../phet-core/js/merge.js';
import optionize from '../../phet-core/js/optionize.js';
import PhetioObject from './PhetioObject.js';
import PhetioObject, { PhetioObjectOptions } from './PhetioObject.js';
import TandemConstants, { PhetioID } from './TandemConstants.js';
import tandemNamespace from './tandemNamespace.js';
import PickOptional from '../../phet-core/js/types/PickOptional.js';

// constants
// Tandem can't depend on joist, so cannot use packageJSON module
Expand Down Expand Up @@ -597,6 +598,32 @@ class Tandem {
* Use this as the parent tandem for Properties that are related to sim-specific preferences.
*/
public static readonly PREFERENCES = Tandem.GLOBAL_MODEL.createTandem( 'preferences' );

/**
* Convenience method to get an accessibleName from providedOptions, see tandemNameToAccessibleName
*/
public static toAccessibleName( providedOptions: PickOptional<PhetioObjectOptions, 'tandem'> | undefined, suffix: string ): string | null {
if ( providedOptions && providedOptions.tandem && providedOptions.tandem.supplied ) {
return Tandem.tandemNameToAccessibleName( providedOptions.tandem.name, suffix );
}
return null;
}

/**
* Tandem names can be used to create accessible names for screen readers. This method will convert a tandem name to
* a human-readable name. For example, 'resetAllButton' would become 'Reset All'.
*/
public static tandemNameToAccessibleName( tandemName: string, suffix: string ): string | null {
assert && assert( tandemName.toLowerCase().endsWith( suffix.toLowerCase() ), `suffix should be at the end of the tandem name: ${tandemName}` );

// trim the suffix
const withoutSuffix = tandemName.slice( 0, -suffix.length );

const whitespaceName = withoutSuffix.replace( /([A-Z])/g, ' $1' ).trim();

// capitalize the first letter of each word, no matter how many words
return whitespaceName.replace( /\b\w/g, c => c.toUpperCase() );
}
}

Tandem.addLaunchListener( () => {
Expand Down

0 comments on commit 1210f0a

Please sign in to comment.