From 04bd0915e7278bb9091ad54fbec2f4eefe30ef9d Mon Sep 17 00:00:00 2001 From: Derek Sifford Date: Mon, 8 Jul 2019 17:00:15 -0400 Subject: [PATCH] [@wordpress/notices] add new definitions (#36700) * [@wordpress/notices] add new definitions * fix build failure --- types/wordpress__blocks/tsconfig.json | 1 + types/wordpress__components/notice/index.d.ts | 3 +- types/wordpress__components/tsconfig.json | 1 + types/wordpress__notices/index.d.ts | 61 +++++++++++++++++++ types/wordpress__notices/store/actions.d.ts | 50 +++++++++++++++ types/wordpress__notices/store/selectors.d.ts | 8 +++ types/wordpress__notices/tsconfig.json | 26 ++++++++ types/wordpress__notices/tslint.json | 1 + .../wordpress__notices-tests.ts | 39 ++++++++++++ types/wordpress__plugins/tsconfig.json | 1 + 10 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 types/wordpress__notices/index.d.ts create mode 100644 types/wordpress__notices/store/actions.d.ts create mode 100644 types/wordpress__notices/store/selectors.d.ts create mode 100644 types/wordpress__notices/tsconfig.json create mode 100644 types/wordpress__notices/tslint.json create mode 100644 types/wordpress__notices/wordpress__notices-tests.ts diff --git a/types/wordpress__blocks/tsconfig.json b/types/wordpress__blocks/tsconfig.json index 58cd466f9d..feb2e3eeac 100644 --- a/types/wordpress__blocks/tsconfig.json +++ b/types/wordpress__blocks/tsconfig.json @@ -17,6 +17,7 @@ "@wordpress/components": ["wordpress__components"], "@wordpress/data": ["wordpress__data"], "@wordpress/element": ["wordpress__element"], + "@wordpress/notices": ["wordpress__notices"], "@wordpress/rich-text": ["wordpress__rich-text"] } }, diff --git a/types/wordpress__components/notice/index.d.ts b/types/wordpress__components/notice/index.d.ts index 40ad7f45c3..7f2357267c 100644 --- a/types/wordpress__components/notice/index.d.ts +++ b/types/wordpress__components/notice/index.d.ts @@ -1,4 +1,5 @@ import { ComponentType, MouseEventHandler, ReactNode } from '@wordpress/element'; +import { Status } from '@wordpress/notices'; declare namespace Notice { interface Props { @@ -16,7 +17,7 @@ declare namespace Notice { * @defaultValue true */ isDismissible?: boolean; - status?: 'success' | 'warning' | 'error'; + status?: Status; /** * Function called when dismissing the notice. */ diff --git a/types/wordpress__components/tsconfig.json b/types/wordpress__components/tsconfig.json index 21ae23c376..855865c095 100644 --- a/types/wordpress__components/tsconfig.json +++ b/types/wordpress__components/tsconfig.json @@ -16,6 +16,7 @@ "@wordpress/components": ["wordpress__components"], "@wordpress/data": ["wordpress__data"], "@wordpress/element": ["wordpress__element"], + "@wordpress/notices": ["wordpress__notices"], "@wordpress/rich-text": ["wordpress__rich-text"] } }, diff --git a/types/wordpress__notices/index.d.ts b/types/wordpress__notices/index.d.ts new file mode 100644 index 0000000000..de2a3eea19 --- /dev/null +++ b/types/wordpress__notices/index.d.ts @@ -0,0 +1,61 @@ +// Type definitions for @wordpress/notices 1.5 +// Project: https://github.com/WordPress/gutenberg/tree/master/packages/notices/README.md +// Definitions by: Derek Sifford +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.5 + +import { dispatch, select } from '@wordpress/data'; +import { MouseEventHandler } from '@wordpress/element'; + +declare module '@wordpress/data' { + function dispatch(key: 'core/notices'): typeof import('./store/actions'); + function select(key: 'core/notices'): typeof import('./store/selectors'); +} + +export type Status = 'error' | 'info' | 'success' | 'warning'; + +export interface Notice { + id: string; + status: Status; + content: string; + isDismissible: boolean; + actions: readonly Action[]; +} + +export interface URLAction { + label: string; + url: string; +} + +export interface CallbackAction { + label: string; + callback(): void; +} + +export type Action = URLAction | CallbackAction; + +export interface Options { + /** + * User actions to be presented with notice. + */ + actions: readonly Action[]; + /** + * Context under which to group notice. + * @defaultValue `'global'` + */ + context: string; + /** + * Identifier for notice. Automatically assigned if not specified. + */ + id: string; + /** + * Whether the notice can be dismissed by user. + * @defaultValue `true` + */ + isDismissible: boolean; + /** + * Whether the notice content should be announced to screen readers. + * @defaultValue `true` + */ + speak: boolean; +} diff --git a/types/wordpress__notices/store/actions.d.ts b/types/wordpress__notices/store/actions.d.ts new file mode 100644 index 0000000000..fb83c59e42 --- /dev/null +++ b/types/wordpress__notices/store/actions.d.ts @@ -0,0 +1,50 @@ +import { Status, Options } from '../'; + +/** + * Yields action objects used in signalling that a notice is to be created. + * + * @param [status='info'] - Notice status. + * @param content - Notice message. + * @param [options={}] - Notice options. + */ +export function createNotice(status: Status | undefined, content: string, options?: Partial): void; + +/** + * Dispatches an action signalling that an error notice is to be created. + * + * @param content - Notice message. + * @param [options] - Optional notice options. + */ +export function createErrorNotice(content: string, options?: Partial): void; + +/** + * Dispatches an action signalling that an info notice is to be created. + * + * @param content - Notice message. + * @param [options] - Optional notice options. + */ +export function createInfoNotice(content: string, options?: Partial): void; + +/** + * Dispatches an action signalling that a success notice is to be created. + * + * @param content - Notice message. + * @param [options] - Optional notice options. + */ +export function createSuccessNotice(content: string, options?: Partial): void; + +/** + * Dispatches an action signalling that a warning notice is to be created. + * + * @param content - Notice message. + * @param [options] - Optional notice options. + */ +export function createWarningNotice(content: string, options?: Partial): void; + +/** + * Dispatches an action signalling that a notice is to be removed. + * + * @param id - Notice's unique identifier. + * @param [context] - Optional context (grouping) in which the notice is intended to appear. + */ +export function removeNotice(id: string, context?: string): void; diff --git a/types/wordpress__notices/store/selectors.d.ts b/types/wordpress__notices/store/selectors.d.ts new file mode 100644 index 0000000000..1635b9b06b --- /dev/null +++ b/types/wordpress__notices/store/selectors.d.ts @@ -0,0 +1,8 @@ +import { Notice } from '../'; + +/** + * Returns all notices as an array, optionally for a given context. + * + * @param [context='global'] - Optional grouping context. + */ +export function getNotices(context?: string): Notice[]; diff --git a/types/wordpress__notices/tsconfig.json b/types/wordpress__notices/tsconfig.json new file mode 100644 index 0000000000..51022f5dd8 --- /dev/null +++ b/types/wordpress__notices/tsconfig.json @@ -0,0 +1,26 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["dom", "es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "paths": { + "@wordpress/data": ["wordpress__data"], + "@wordpress/element": ["wordpress__element"], + "@wordpress/notices": ["wordpress__notices"] + } + }, + "files": [ + "index.d.ts", + "store/actions.d.ts", + "store/selectors.d.ts", + "wordpress__notices-tests.ts" + ] +} diff --git a/types/wordpress__notices/tslint.json b/types/wordpress__notices/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/wordpress__notices/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/wordpress__notices/wordpress__notices-tests.ts b/types/wordpress__notices/wordpress__notices-tests.ts new file mode 100644 index 0000000000..bf7e42b828 --- /dev/null +++ b/types/wordpress__notices/wordpress__notices-tests.ts @@ -0,0 +1,39 @@ +import { dispatch, select } from '@wordpress/data'; + +// +// store +// + +dispatch('core/notices').createNotice('warning', 'hello world'); +dispatch('core/notices').createNotice(undefined, 'hello world', {}); +dispatch('core/notices').createNotice('info', 'hello world', { + isDismissible: true, + actions: [ + { + label: 'foo', + url: 'https://foo.bar', + }, + { + label: 'bar', + callback: () => void 0, + }, + ], +}); + +dispatch('core/notices').createErrorNotice('hello world'); +dispatch('core/notices').createErrorNotice('hello world', {}); + +dispatch('core/notices').createInfoNotice('hello world'); +dispatch('core/notices').createInfoNotice('hello world', { id: 'foo' }); + +dispatch('core/notices').createSuccessNotice('hello world'); +dispatch('core/notices').createSuccessNotice('hello world', { isDismissible: false }); + +dispatch('core/notices').createWarningNotice('hello world'); +dispatch('core/notices').createWarningNotice('hello world', undefined); + +// $ExpectType Notice[] +select('core/notices').getNotices(); + +// $ExpectType Notice[] +select('core/notices').getNotices('foo'); diff --git a/types/wordpress__plugins/tsconfig.json b/types/wordpress__plugins/tsconfig.json index 78be0d2568..18e0c17345 100644 --- a/types/wordpress__plugins/tsconfig.json +++ b/types/wordpress__plugins/tsconfig.json @@ -16,6 +16,7 @@ "@wordpress/data": ["wordpress__data"], "@wordpress/components": ["wordpress__components"], "@wordpress/element": ["wordpress__element"], + "@wordpress/notices": ["wordpress__notices"], "@wordpress/plugins": ["wordpress__plugins"], "@wordpress/rich-text": ["wordpress__rich-text"] }