diff --git a/src/components/Message/index.tsx b/src/components/Message/index.tsx new file mode 100644 index 0000000..f98f355 --- /dev/null +++ b/src/components/Message/index.tsx @@ -0,0 +1,92 @@ +import * as React from 'react' +import { Icon } from '@components' +import classnames from 'classnames' +import { unmountComponentAtNode, render } from 'react-dom' +import './style.scss' + +type NoticeType = 'success' | 'info' | 'warning' | 'error' +type ConfigOnClose = () => void +interface ArgsProps { + content: string + type: NoticeType + duration?: number + onClose?: () => void +} + +interface MessageProps { + content?: string + icon?: React.ReactNode + onClose?: () => void + duration?: number + removeComponent: () => void +} + +class Message extends React.Component { + static defaultProps: MessageProps = { + content: '', + icon: , + duration: 1500, + onClose: () => {}, + removeComponent: () => {} + } + state = { + visible: false + } + + componentDidMount () { + this.setState({ + visible: true + }) + setTimeout(() => { + this.setState({ + visible: false + }) + this.props.onClose() + }, this.props.duration) + } + + render () { + const { removeComponent, icon, content } = this.props + return ( +
!this.state.visible && removeComponent()}> + {icon} + {content} +
+ ) + } +} + +function notice (args: ArgsProps) { + const container = document.createElement('div') + document.body.appendChild(container) + const removeComponent = () => { + const isUnMount = unmountComponentAtNode(container) + if (isUnMount) { + document.body.removeChild(container) + } + } + const icon = + const props: MessageProps = { + icon, + content: args.content, + removeComponent, + duration: args.duration, + onClose: args.onClose + } + + render(, container) +} +export interface MessageApi { + info (content: string, type: NoticeType, duration?: number, onClose?: ConfigOnClose) +} + +const api: any = { +} +const types = ['success', 'info', 'warning', 'error'] +types.forEach(type => { + api[type] = (content: string, type: NoticeType, duration?: number, onClose?: ConfigOnClose) => notice({ + content, duration, type, onClose + }) +}) + +export default api as MessageApi diff --git a/src/components/Message/style.scss b/src/components/Message/style.scss new file mode 100644 index 0000000..e5a542f --- /dev/null +++ b/src/components/Message/style.scss @@ -0,0 +1,14 @@ +@import '~@styles/variables'; + +.message { + position: fixed; + top: 20px; + left: 50%; + opacity: 0; + transition: all 2000ms ease-out; +} + +.message-show { + opacity: 1; + transition: all 2000ms ease-out; +} diff --git a/src/components/index.ts b/src/components/index.ts index 65dfbb2..4223eb4 100644 --- a/src/components/index.ts +++ b/src/components/index.ts @@ -11,3 +11,4 @@ export * from './Select' export * from './Modal' export * from './Alert' export * from './Button' +export { default as Message } from './Message'