forked from ant-design/ant-design-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.web.tsx
157 lines (143 loc) · 4.56 KB
/
index.web.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* eslint no-console:0 */
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import Dialog from 'rc-dialog';
import classNames from 'classnames';
import Icon from '../icon/index.web';
import assign from 'object-assign';
const NORMAL = 'NORMAL';
const SHARE = 'SHARE';
function noop() { }
function getDataAttr(props) {
const dataAttrs = {};
Object.keys(props).forEach(i => {
if (i.indexOf('data-') === 0) {
dataAttrs[i] = props[i];
}
});
return dataAttrs;
}
const queue = [];
function createActionSheet(flag, config, callback) {
const props = assign({}, {
prefixCls: 'am-action-sheet',
cancelButtonText: '取消',
}, config);
const { prefixCls, className, transitionName, maskTransitionName, maskClosable = true } = props;
let div = document.createElement('div');
document.body.appendChild(div);
queue.push(close);
function close() {
if (div) {
ReactDOM.unmountComponentAtNode(div);
div.parentNode.removeChild(div);
div = null;
const index = queue.indexOf(close);
if (index !== -1) {
queue.splice(index, 1);
}
}
}
function cb(index, rowIndex = 0) {
const res = callback(index, rowIndex);
if (res && res.then) {
res.then(() => {
close();
});
} else {
close();
}
}
const { title, message, options, destructiveButtonIndex, cancelButtonIndex, cancelButtonText } = props;
const titleMsg = [
title ? <h3 key="0" className={`${prefixCls}-title`}>{title}</h3> : null,
message ? <div key="1" className={`${prefixCls}-message`}>{message}</div> : null,
];
let children = null;
let mode = 'normal';
switch (flag) {
case NORMAL:
mode = 'normal';
children = (<div {...getDataAttr(props)}>
{titleMsg}
<ul className={`${prefixCls}-button-list`}>
{options.map((item, index) => {
const extraProp = {
onClick: () => cb(index),
};
let li = <li className={[`${prefixCls}-button-list-item`]} key={index} {...extraProp}>{item}</li>;
const cls = {
[`${prefixCls}-destructive-button`]: destructiveButtonIndex === index,
[`${prefixCls}-cancel-button`]: cancelButtonIndex === index,
};
if (cancelButtonIndex === index || destructiveButtonIndex === index) {
li = (<li key={index} className={classNames(cls) } {...extraProp}>
{item}
{cancelButtonIndex === index ? <span className={`${prefixCls}-cancel-button-mask`}></span> : null}
</li>);
}
return li;
})}
</ul>
</div>);
break;
case SHARE:
mode = 'share';
const multipleLine = options.length && Array.isArray(options[0]) || false;
const createList = (item, index, rowIndex = 0) => (
<div className={`${prefixCls}-share-list-item`} key={index} onClick={() => cb(index, rowIndex) }>
<div className={`${prefixCls}-share-list-item-icon`}>
{item.iconName ? <Icon type={item.iconName}/> : item.icon}
</div>
<div className={`${prefixCls}-share-list-item-title`}>{item.title}</div>
</div>
);
children = (<div {...getDataAttr(props)}>
{titleMsg}
<div className={`${prefixCls}-share`}>
{multipleLine ? options.map((item, index) => (
<div key={index} className={`${prefixCls}-share-list`}>
{item.map((ii, ind) => createList(ii, ind, index))}
</div>
)) : (
<div className={`${prefixCls}-share-list`}>
{options.map((item, index) => createList(item, index))}
</div>
)}
<div className={`${prefixCls}-share-cancel-button`} onClick={() => cb(-1)}>{cancelButtonText}</div>
</div>
</div>);
break;
default:
break;
}
const rootCls = classNames({
[className]: !!className,
[`${prefixCls}-${mode}`]: true,
});
ReactDOM.render(<Dialog
visible
title=""
footer=""
prefixCls={prefixCls}
className={rootCls}
transitionName={transitionName || `am-slide-up`}
maskTransitionName={maskTransitionName || `am-fade`}
onClose={close}
maskClosable={maskClosable}
>{children}</Dialog>, div);
return {
close,
};
}
export default {
showActionSheetWithOptions(config, callback = noop) {
createActionSheet(NORMAL, config, callback);
},
showShareActionSheetWithOptions(config, callback = noop) {
createActionSheet(SHARE, config, callback);
},
close() {
queue.forEach(q => q());
},
};