Skip to content

Commit

Permalink
Notices: Add speak option
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Nov 16, 2018
1 parent 4718101 commit 137b3bd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/notices/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### New Feature

- The `createNotice` can now optionally accept a WPNotice object as the sole argument.
- New option `speak` enables control as to whether the notice content is announced to screen readers (defaults to `true`)

### Bug Fixes

Expand Down
25 changes: 18 additions & 7 deletions packages/notices/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import { DEFAULT_CONTEXT, DEFAULT_STATUS } from './constants';
* @param {?string|WPNotice} statusOrNotice Notice status, or a
* notice object.
* Defaults to `info`.
* @param {string} content Notice message.
* @param {string|?Object} contentOrOptions Notice message, or
* options if the first
* argument is WPNotice.
* @param {?Object} options Notice options.
* @param {?string} options.context Context under which to
* group notice.
Expand All @@ -24,22 +26,29 @@ import { DEFAULT_CONTEXT, DEFAULT_STATUS } from './constants';
* @param {?boolean} options.isDismissible Whether the notice can
* be dismissed by user.
* Defaults to `true`.
* @param {?boolean} options.speak Whether the notice
* content should be
* announced to screen
* readers. Defaults to
* `true`.
* @param {?Array<WPNoticeAction>} options.actions User actions to be
* presented with notice.
*/
export function* createNotice( statusOrNotice = DEFAULT_STATUS, content, options = {} ) {
let status, __unstableHTML;
export function* createNotice( statusOrNotice = DEFAULT_STATUS, contentOrOptions, options = {} ) {
let status, content, __unstableHTML;

if ( isPlainObject( statusOrNotice ) ) {
// Support overloaded form `createNotice( notice: WPNotice )`.
options = statusOrNotice;
( { status = DEFAULT_STATUS, content, __unstableHTML } = options );
// Support overloaded form `createNotice( notice: WPNotice, options: ?Object )`.
options = { ...contentOrOptions, ...statusOrNotice };
( { status = DEFAULT_STATUS, content, __unstableHTML } = statusOrNotice );
} else {
// Else consider the first argument the status type string.
status = statusOrNotice;
content = contentOrOptions;
}

const {
speak = true,
isDismissible = true,
context = DEFAULT_CONTEXT,
id = uniqueId( context ),
Expand All @@ -51,7 +60,9 @@ export function* createNotice( statusOrNotice = DEFAULT_STATUS, content, options
// supported, cast to a string.
content = String( content );

yield { type: 'SPEAK', message: content };
if ( speak ) {
yield { type: 'SPEAK', message: content };
}

yield {
type: 'CREATE_NOTICE',
Expand Down
22 changes: 22 additions & 0 deletions packages/notices/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@ describe( 'actions', () => {
},
} );
} );

it( 'yields action when speak disabled', () => {
const result = createNotice( {
id,
content: '',
__unstableHTML: 'my <strong>message</strong>',
isDismissible: false,
}, { speak: false } );

expect( result.next().value ).toEqual( {
type: 'CREATE_NOTICE',
context: DEFAULT_CONTEXT,
notice: {
id,
status: DEFAULT_STATUS,
content: '',
__unstableHTML: 'my <strong>message</strong>',
isDismissible: false,
actions: [],
},
} );
} );
} );

describe( 'createSuccessNotice', () => {
Expand Down

0 comments on commit 137b3bd

Please sign in to comment.