From f635e16c61f236b1feab4e75ac4b12f066b118ef Mon Sep 17 00:00:00 2001 From: Jeow Li Huan Date: Mon, 18 Mar 2019 19:16:00 +0800 Subject: [PATCH] [react-alert] Update react-alert to 5.2. --- types/react-alert/index.d.ts | 100 ++++++++++++++++++------ types/react-alert/react-alert-tests.tsx | 90 +++++++++++---------- 2 files changed, 126 insertions(+), 64 deletions(-) diff --git a/types/react-alert/index.d.ts b/types/react-alert/index.d.ts index 6c94da77b0..5334de3571 100644 --- a/types/react-alert/index.d.ts +++ b/types/react-alert/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for react-alert 4.0 +// Type definitions for react-alert 5.2 // Project: https://github.com/schiehll/react-alert // Definitions by: Yue Yang // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped @@ -8,16 +8,49 @@ import * as React from 'react'; export type AlertPosition = | 'top left' - | 'top right' | 'top center' + | 'top right' + | 'middle left' + | 'middle' + | 'middle right' | 'bottom left' - | 'bottom right' - | 'bottom center'; + | 'bottom center' + | 'bottom right'; + +export interface Positions { + TOP_LEFT: 'top left'; + TOP_CENTER: 'top center'; + TOP_RIGHT: 'top right'; + MIDDLE_LEFT: 'middle left'; + MIDDLE: 'middle'; + MIDDLE_RIGHT: 'middle right'; + BOTTOM_LEFT: 'bottom left'; + BOTTOM_CENTER: 'bottom center'; + BOTTOM_RIGHT: 'bottom right'; +} + +export const positions: Positions; export type AlertType = 'info' | 'success' | 'error'; + +export interface Types { + INFO: 'info'; + SUCCESS: 'success'; + ERROR: 'error'; +} + +export const types: Types; + export type AlertTransition = 'fade' | 'scale'; -export interface ProviderOptions { +export interface Transitions { + FADE: 'fade'; + SCALE: 'scale'; +} + +export const transitions: Transitions; + +export interface AlertProviderProps extends React.HTMLAttributes { /** * The margin of each alert * @@ -49,18 +82,33 @@ export interface ProviderOptions { */ transition?: AlertTransition; /** - * The z-index of alerts + * The style of the alert container * - * Default value: 100 + * Default z-index value: 100 */ - zIndex?: number; + containerStyle?: React.CSSProperties; + /** + * The alert component for each message + */ + template: React.ComponentType; + /** + * Custom context to separate alerts. + */ + context?: React.Context; } -export class Provider extends React.Component {} +export interface AlertComponentProps { + id: string; + message: React.ReactNode; + options: AlertCustomOptionsWithType; + close(): void; +} -export const Alert: React.Consumer; +export interface AlertComponentPropsWithStyle extends AlertComponentProps { + style: React.CSSProperties; +} + +export class Provider extends React.Component {} export interface AlertCustomOptions { /** @@ -70,28 +118,32 @@ export interface AlertCustomOptions { /** * Callback that will be executed after this alert open */ - onOpen?(): undefined; + onOpen?(): void; /** * Callback that will be executed after this alert is removed */ - onClose?(): undefined; + onClose?(): void; } export interface AlertCustomOptionsWithType extends AlertCustomOptions { type?: AlertType; } -export interface InjectedAlertProp { +export interface AlertManager { + root?: HTMLElement; + alerts: AlertComponentProps[]; show( - message?: string, + message?: React.ReactNode, options?: AlertCustomOptionsWithType - ): InjectedAlertProp; - remove(alert: InjectedAlertProp): undefined; - success(message?: string, options?: AlertCustomOptions): InjectedAlertProp; - error(message?: string, options?: AlertCustomOptions): InjectedAlertProp; - info(message?: string, options?: AlertCustomOptions): InjectedAlertProp; + ): AlertComponentProps; + remove(alert: AlertComponentProps): void; + success(message?: React.ReactNode, options?: AlertCustomOptions): AlertComponentProps; + error(message?: React.ReactNode, options?: AlertCustomOptions): AlertComponentProps; + info(message?: React.ReactNode, options?: AlertCustomOptions): AlertComponentProps; } -export function withAlert

( - c: React.ComponentType

-): React.ComponentType>>; +export function withAlert

(context?: React.Context): + (c: React.ComponentType

) => + React.ComponentType>>; + +export function useAlert(context?: React.Context): AlertManager; diff --git a/types/react-alert/react-alert-tests.tsx b/types/react-alert/react-alert-tests.tsx index c6088a52a8..64a4e5cf97 100644 --- a/types/react-alert/react-alert-tests.tsx +++ b/types/react-alert/react-alert-tests.tsx @@ -1,20 +1,28 @@ import * as React from 'react'; import { + AlertComponentPropsWithStyle, + AlertManager, Provider as AlertProvider, - Alert, + AlertProviderProps, + useAlert, withAlert, - AlertPosition, - AlertTransition, - ProviderOptions, - InjectedAlertProp } from 'react-alert'; -class AppWithoutAlert extends React.Component<{ alert: InjectedAlertProp }> { +class AppWithoutAlert extends React.Component<{ alert: AlertManager }> { render() { return ( - )} - - ); - } -} +const App = withAlert(customContext)(AppWithoutAlert); -class AlertTemplate extends React.Component { +const AlertHook = (): JSX.Element => { + const alert = useAlert(); + return ( + + ); +}; + +class AlertTemplate extends React.Component { render() { // the style contains only the margin given as offset // options contains all alert given options @@ -63,28 +76,25 @@ class AlertTemplate extends React.Component { } } -const options: ProviderOptions = { - position: 'bottom center' as AlertPosition, +const options: AlertProviderProps = { + position: 'bottom center', timeout: 5000, offset: '30px', - transition: 'scale' as AlertTransition + transition: 'scale', + context: customContext, + className: 'cssClass', + template: AlertTemplate, + containerStyle: { + margin: 5, + }, }; class Root extends React.Component { render() { return ( - + - - ); - } -} - -class RootAlert extends React.Component { - render() { - return ( - - + ); }