From 137b3bd5192e5a21dafb5bc4a9524e82318f61bc Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Fri, 16 Nov 2018 13:04:33 -0500 Subject: [PATCH] Notices: Add speak option --- packages/notices/CHANGELOG.md | 1 + packages/notices/src/store/actions.js | 25 ++++++++++++++++------ packages/notices/src/store/test/actions.js | 22 +++++++++++++++++++ 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/packages/notices/CHANGELOG.md b/packages/notices/CHANGELOG.md index 0ae2ecd47d2eec..6a38ce167d73af 100644 --- a/packages/notices/CHANGELOG.md +++ b/packages/notices/CHANGELOG.md @@ -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 diff --git a/packages/notices/src/store/actions.js b/packages/notices/src/store/actions.js index 5bbe001b4d4ca8..21c8c160668b0e 100644 --- a/packages/notices/src/store/actions.js +++ b/packages/notices/src/store/actions.js @@ -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. @@ -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} 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 ), @@ -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', diff --git a/packages/notices/src/store/test/actions.js b/packages/notices/src/store/test/actions.js index 4f5515362682bd..39d21fd83883b0 100644 --- a/packages/notices/src/store/test/actions.js +++ b/packages/notices/src/store/test/actions.js @@ -113,6 +113,28 @@ describe( 'actions', () => { }, } ); } ); + + it( 'yields action when speak disabled', () => { + const result = createNotice( { + id, + content: '', + __unstableHTML: 'my message', + isDismissible: false, + }, { speak: false } ); + + expect( result.next().value ).toEqual( { + type: 'CREATE_NOTICE', + context: DEFAULT_CONTEXT, + notice: { + id, + status: DEFAULT_STATUS, + content: '', + __unstableHTML: 'my message', + isDismissible: false, + actions: [], + }, + } ); + } ); } ); describe( 'createSuccessNotice', () => {