From 173bd0407040bfc9f00758b53fe76d8b0a7be8e3 Mon Sep 17 00:00:00 2001 From: yuxuanhuo Date: Fri, 14 Aug 2020 12:41:33 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E8=B0=83=E6=95=B4message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/ui/message/package.json | 4 +- packages/ui/message/src/index.tsx | 47 ++++++++++++------- packages/ui/message/stories/index.stories.tsx | 6 +++ yarn.lock | 5 ++ 4 files changed, 44 insertions(+), 18 deletions(-) diff --git a/packages/ui/message/package.json b/packages/ui/message/package.json index dbbc511c..07407423 100644 --- a/packages/ui/message/package.json +++ b/packages/ui/message/package.json @@ -28,6 +28,7 @@ "@storybook/client-api": "^5.3.19", "@storybook/react": "^5.3.19", "@types/classnames": "^2.2.10", + "@types/lodash": "^4.14.159", "@types/react-dom": "^16.9.8", "antd": "^4.4.0", "babel-loader": "^8.1.0", @@ -51,6 +52,7 @@ "author": "huoyuxuan,liushaozhen", "license": "MIT", "dependencies": { - "classnames": "^2.2.6" + "classnames": "^2.2.6", + "lodash": "^4.17.19" } } diff --git a/packages/ui/message/src/index.tsx b/packages/ui/message/src/index.tsx index 6b82c6e8..01788bd3 100644 --- a/packages/ui/message/src/index.tsx +++ b/packages/ui/message/src/index.tsx @@ -1,5 +1,6 @@ -import { message as AntdMessage } from 'antd'; -import { MessageInstance as AntdMessageInstance, MessageApi as AntdMessageApi } from 'antd/lib/message'; +import {partial} from 'lodash'; +import {message as AntdMessage} from 'antd'; +import {MessageInstance as AntdMessageInstance, MessageApi as AntdMessageApi} from 'antd/lib/message'; import './index.less'; const clsPrefix = 'osui-message'; @@ -7,21 +8,33 @@ const clsPrefix = 'osui-message'; export type MessageInstance = AntdMessageInstance; export type MessageApi = AntdMessageApi; -const types = ['success', 'error', 'info', 'warning', 'warn', 'loading', 'config', 'destroy', 'useMessage']; +type MessageType = 'success'|'error'|'warning'|'info'|'loading'|'warn'; -enum message { } +const messageBuilder = (type: MessageType, content: any, ...restArgs: any[]) => { + // 为了把classname加进去,所有content都变成了对象形式,而不是string形式 + const modifiedContent = {className: '', content: ''}; -types.forEach(item => { - message[item] = props => { - const defaultProps = { className: '' }; - if (typeof props === 'object') { - defaultProps.className = clsPrefix + ` ${props.className ? props.className : ''}`; - } else if (typeof props === 'string') { - defaultProps.className = clsPrefix; - defaultProps.content = props; - } - AntdMessage[item](defaultProps); - }; -}); + if (typeof content === 'object') { + modifiedContent.className = clsPrefix + ` ${content.className ?? ''}`; + } else if (typeof content === 'string') { + modifiedContent.className = clsPrefix; + modifiedContent.content = content; + } -export default message; + const message = AntdMessage[type]; + return message(modifiedContent, ...restArgs); +}; + +const openMessageBuilder = (args: any) => { + return AntdMessage.open({className: clsPrefix + ` ${args.className ?? ''}`, ...args}); +}; + +export default Object.assign({}, AntdMessage, { + success: partial(messageBuilder, 'success'), + error: partial(messageBuilder, 'error'), + warning: partial(messageBuilder, 'warning'), + info: partial(messageBuilder, 'info'), + loading: partial(messageBuilder, 'loading'), + warn: partial(messageBuilder, 'warn'), + open: openMessageBuilder, +}) as MessageApi; diff --git a/packages/ui/message/stories/index.stories.tsx b/packages/ui/message/stories/index.stories.tsx index 6319b21f..03a0db9d 100644 --- a/packages/ui/message/stories/index.stories.tsx +++ b/packages/ui/message/stories/index.stories.tsx @@ -25,6 +25,11 @@ export const Demo = () => { const loading = () => { message.loading('This is a loading message'); }; + + const open = () => { + message.open({type: 'success', content: 'This is a loading message', duration: 2}); + }; + return (
@@ -34,6 +39,7 @@ export const Demo = () => { +
); }; diff --git a/yarn.lock b/yarn.lock index 9dab82b6..6c637761 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3347,6 +3347,11 @@ resolved "https://registry.npm.taobao.org/@types/json-schema/download/@types/json-schema-7.0.5.tgz?cache=0&sync_timestamp=1596838615209&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Fjson-schema%2Fdownload%2F%40types%2Fjson-schema-7.0.5.tgz#dcce4430e64b443ba8945f0290fb564ad5bac6dd" integrity sha1-3M5EMOZLRDuolF8CkPtWStW6xt0= +"@types/lodash@^4.14.159": + version "4.14.159" + resolved "https://registry.npm.taobao.org/@types/lodash/download/@types/lodash-4.14.159.tgz#61089719dc6fdd9c5cb46efc827f2571d1517065" + integrity sha1-YQiXGdxv3ZxctG78gn8lcdFRcGU= + "@types/mdast@^3.0.0": version "3.0.3" resolved "https://registry.npm.taobao.org/@types/mdast/download/@types/mdast-3.0.3.tgz#2d7d671b1cd1ea3deb306ea75036c2a0407d2deb" -- Gitee From f1bb806df8756692941059af6fc445d70a6a36e1 Mon Sep 17 00:00:00 2001 From: yuxuanhuo Date: Fri, 14 Aug 2020 13:12:17 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20message=E5=8D=95=E7=8B=AC=E5=BC=95?= =?UTF-8?q?=E5=85=A5lodash.partial?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/ui/message/package.json | 4 ++-- packages/ui/message/src/index.tsx | 2 +- yarn.lock | 14 +++++++++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/ui/message/package.json b/packages/ui/message/package.json index 07407423..68dec2c9 100644 --- a/packages/ui/message/package.json +++ b/packages/ui/message/package.json @@ -28,7 +28,7 @@ "@storybook/client-api": "^5.3.19", "@storybook/react": "^5.3.19", "@types/classnames": "^2.2.10", - "@types/lodash": "^4.14.159", + "@types/lodash.partial": "^4.2.6", "@types/react-dom": "^16.9.8", "antd": "^4.4.0", "babel-loader": "^8.1.0", @@ -53,6 +53,6 @@ "license": "MIT", "dependencies": { "classnames": "^2.2.6", - "lodash": "^4.17.19" + "lodash.partial": "^4.2.1" } } diff --git a/packages/ui/message/src/index.tsx b/packages/ui/message/src/index.tsx index 01788bd3..ba8cfd1e 100644 --- a/packages/ui/message/src/index.tsx +++ b/packages/ui/message/src/index.tsx @@ -1,4 +1,4 @@ -import {partial} from 'lodash'; +import partial from 'lodash.partial'; import {message as AntdMessage} from 'antd'; import {MessageInstance as AntdMessageInstance, MessageApi as AntdMessageApi} from 'antd/lib/message'; import './index.less'; diff --git a/yarn.lock b/yarn.lock index 6c637761..6268b17c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3347,7 +3347,14 @@ resolved "https://registry.npm.taobao.org/@types/json-schema/download/@types/json-schema-7.0.5.tgz?cache=0&sync_timestamp=1596838615209&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Fjson-schema%2Fdownload%2F%40types%2Fjson-schema-7.0.5.tgz#dcce4430e64b443ba8945f0290fb564ad5bac6dd" integrity sha1-3M5EMOZLRDuolF8CkPtWStW6xt0= -"@types/lodash@^4.14.159": +"@types/lodash.partial@^4.2.6": + version "4.2.6" + resolved "https://registry.npm.taobao.org/@types/lodash.partial/download/@types/lodash.partial-4.2.6.tgz#c580f1b3c9b0b8b04fabf760f8a2839bdd76ca23" + integrity sha1-xYDxs8mwuLBPq/dg+KKDm912yiM= + dependencies: + "@types/lodash" "*" + +"@types/lodash@*", "@types/lodash@^4.14.159": version "4.14.159" resolved "https://registry.npm.taobao.org/@types/lodash/download/@types/lodash-4.14.159.tgz#61089719dc6fdd9c5cb46efc827f2571d1517065" integrity sha1-YQiXGdxv3ZxctG78gn8lcdFRcGU= @@ -10067,6 +10074,11 @@ lodash.memoize@^4.1.2: resolved "https://registry.npm.taobao.org/lodash.memoize/download/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe" integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4= +lodash.partial@^4.2.1: + version "4.2.1" + resolved "https://registry.npm.taobao.org/lodash.partial/download/lodash.partial-4.2.1.tgz#49f3d8cfdaa3bff8b3a91d127e923245418961d4" + integrity sha1-SfPYz9qjv/izqR0SfpIyRUGJYdQ= + lodash.set@^4.3.2: version "4.3.2" resolved "https://registry.npm.taobao.org/lodash.set/download/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23" -- Gitee