@@ -3,46 +3,78 @@ import ReactDOM from 'react-dom';
33import PropTypes from 'prop-types' ;
44import NotificationBody from './notification-body' ;
55import './index.less' ;
6- import { DEFAULT_BODY_PROPS , NOTIFICATION_BODY_PROPS } from './constant' ;
76import { getRootWindow , prefixCls } from '../../utils' ;
87
98const CONTAINER_CLS = `${ prefixCls } -notification-container` ;
109const WRAPPER_CLS = `${ prefixCls } -notification-wrapper` ;
1110
1211const PLACEMENT_LIST = [ 'top-left' , 'top-right' , 'bottom-left' , 'bottom-right' , 'top' , 'bottom' ] ;
1312
13+ const DEFAULT_BODY_PROPS = {
14+ placement : 'top-right' ,
15+ container : null ,
16+ duration : 4500 ,
17+ showCloseIcon : null ,
18+ btn : null ,
19+ className : '' ,
20+ borderRadiusSize : 'default' ,
21+ showIcon : false ,
22+ iconType : 'info' ,
23+ icon : null ,
24+ showCancelBtn : false ,
25+ showConfirmBtn : false ,
26+ showDetailBtn : false ,
27+ onConfirm : ( ) => { } ,
28+ style : { } ,
29+ isLightTheme : false ,
30+ }
31+
1432/**
1533 * 更新各条信息提示的样式
1634 */
17- function updateNotifyStyle ( excludeId = null ) {
35+ function updateNotifyStyle ( placement , excludeId = null ) {
1836 const rootDocument = getRootWindow ( ) . document ;
19- const wrapperContainer = rootDocument . querySelector ( `.${ CONTAINER_CLS } ` ) ;
37+ const wrapperContainer = rootDocument . querySelector ( `.${ CONTAINER_CLS } . ${ placement } ` ) ;
2038 const wrapperNodes = Array . from ( wrapperContainer . childNodes ) ;
2139
22- wrapperNodes
23- . filter ( node => node . dataset . id !== excludeId )
24- . forEach ( ( node , index ) => {
25- const yHeight = wrapperNodes . slice ( 0 , index ) . reduce ( ( sum , n ) => {
26- sum += n . clientHeight ;
27- return sum ;
28- } , 0 ) + 20 * index ;
29- node . style . transform = `translate3d(0px, ${ yHeight } px, 0px) scaleX(1)` ;
30- } ) ;
40+ if ( placement . includes ( 'top' ) ) {
41+ wrapperNodes
42+ . filter ( node => node . dataset . id !== excludeId )
43+ . forEach ( ( node , index ) => {
44+ const yHeight = wrapperNodes . slice ( 0 , index ) . reduce ( ( sum , n ) => {
45+ sum += n . clientHeight ;
46+ return sum ;
47+ } , 0 ) + 20 * index ;
48+ node . style . transform = `translate3d(0px, ${ yHeight } px, 0px) scaleX(1)` ;
49+ } ) ;
50+ } else {
51+ wrapperNodes
52+ . filter ( node => node . dataset . id !== excludeId )
53+ . reverse ( )
54+ . forEach ( ( node , index ) => {
55+ const yHeight = wrapperNodes . slice ( 0 , index ) . reduce ( ( sum , n ) => {
56+ sum += n . clientHeight ;
57+ return sum ;
58+ } , 0 ) + 20 * index ;
59+ node . style . transform = `translate3d(0px, -${ yHeight } px, 0px) scaleX(1)` ;
60+ } ) ;
61+ }
3162}
3263
3364/**
3465 * 关闭信息提示
3566 * @param dataSetId
67+ * @param placement
3668 */
37- function closeNotification ( dataSetId ) {
69+ function closeNotification ( dataSetId , placement ) {
3870 const rootDocument = getRootWindow ( ) . document ;
39- const wrapperContainer = rootDocument . querySelector ( `.${ CONTAINER_CLS } ` ) ;
71+ const wrapperContainer = rootDocument . querySelector ( `.${ CONTAINER_CLS } . ${ placement } ` ) ;
4072 const wrapperNodes = Array . from ( wrapperContainer . childNodes ) ;
4173 const wrapper = wrapperNodes . find ( node => node . dataset . id === dataSetId ) ;
4274 if ( wrapper ) {
4375 wrapper . classList . add ( 'fade-out' ) ;
4476
45- updateNotifyStyle ( dataSetId ) ;
77+ updateNotifyStyle ( placement , dataSetId ) ;
4678
4779 setTimeout ( ( ) => {
4880 wrapperContainer . removeChild ( wrapper ) ;
@@ -55,29 +87,37 @@ function closeNotification(dataSetId) {
5587 * @param props
5688 */
5789function openNotification ( props ) {
90+ const _props = { ...DEFAULT_BODY_PROPS , ...props } ;
5891 const rootDocument = getRootWindow ( ) . document ;
59- const rootContainer = props . container || rootDocument . body ;
92+ const rootContainer = _props . container || rootDocument . body ;
6093
6194 // wrapperContainer:所有提示信息外层的 div
62- let wrapperContainer = rootDocument . querySelector ( `.${ CONTAINER_CLS } ` ) ;
95+ let wrapperContainer = rootDocument . querySelector ( `.${ CONTAINER_CLS } . ${ _props . placement } ` ) ;
6396 if ( ! wrapperContainer ) {
6497 wrapperContainer = rootDocument . createElement ( 'div' ) ;
65- wrapperContainer . className = CONTAINER_CLS ;
98+ wrapperContainer . classList . add ( CONTAINER_CLS ) ;
99+ wrapperContainer . classList . add ( _props . placement ) ;
66100 rootContainer . appendChild ( wrapperContainer ) ;
67101 }
68102
69103 // wrapper:每个提示信息外层的 div
70104 const wrapper = rootDocument . createElement ( 'div' ) ;
71105 wrapper . classList . add ( WRAPPER_CLS ) ;
72106 wrapper . dataset . id = `${ new Date ( ) . getTime ( ) } ` ;
73- wrapperContainer . insertBefore ( wrapper , wrapperContainer . firstChild ) ; // 将新的消息放在最上方
107+ if ( _props . placement . includes ( 'top' ) ) {
108+ // 将新的消息放在最上方
109+ wrapperContainer . insertBefore ( wrapper , wrapperContainer . firstChild ) ;
110+ } else {
111+ wrapperContainer . appendChild ( wrapper ) ;
112+ }
113+
74114 setTimeout ( ( ) => {
75115 wrapper . classList . add ( 'fade-in' ) ;
76- updateNotifyStyle ( ) ;
116+ updateNotifyStyle ( _props . placement ) ;
77117 } )
78118
79119 ReactDOM . render (
80- < NotificationBody { ...props } dataSetId = { wrapper . dataset . id } close = { closeNotification } /> ,
120+ < NotificationBody { ..._props } dataSetId = { wrapper . dataset . id } close = { closeNotification } /> ,
81121 wrapper ,
82122 ) ;
83123 return new Promise ( resolve => {
@@ -87,18 +127,29 @@ function openNotification(props) {
87127
88128const Notification = {
89129 open : openNotification ,
90- close : dataSetId => closeNotification ( dataSetId )
130+ close : ( dataSetId , placement ) => closeNotification ( dataSetId , placement )
91131} ;
92132
93133export default Notification ;
94134
95135Notification . propTypes = {
96- ...NOTIFICATION_BODY_PROPS ,
136+ title : PropTypes . any . isRequired ,
137+ content : PropTypes . any . isRequired ,
138+ duration : PropTypes . number ,
139+ showCloseIcon : PropTypes . bool ,
140+ btn : PropTypes . any ,
141+ className : PropTypes . string ,
142+ borderRadiusSize : PropTypes . oneOf ( [ 'small' , 'default' , 'large' ] ) ,
143+ showIcon : PropTypes . bool ,
144+ iconType : PropTypes . oneOf ( [ 'info' , 'success' , 'warn' , 'fail' ] ) ,
145+ icon : PropTypes . any ,
146+ showCancelBtn : PropTypes . bool ,
147+ showConfirmBtn : PropTypes . bool ,
148+ showDetailBtn : PropTypes . bool ,
149+ onConfirm : PropTypes . func ,
97150 placement : PropTypes . oneOf ( PLACEMENT_LIST ) ,
98151 container : PropTypes . any ,
152+ style : PropTypes . object ,
153+ isLightTheme : PropTypes . bool ,
99154} ;
100- Notification . defaultProps = {
101- ...DEFAULT_BODY_PROPS ,
102- placement : 'top-left' ,
103- container : null ,
104- } ;
155+ Notification . defaultProps = DEFAULT_BODY_PROPS ;
0 commit comments