UNPKG

9.85 kBJavaScriptView Raw
1import React, { Component } from 'react';
2import PropTypes from 'prop-types';
3import objectPath from 'object-path';
4import i18n from './lib/i18n';
5import { getComponentLocale } from 'bee-locale/build/tool';
6import { formatMoney } from './lib/utils';
7import Dropdown from 'bee-dropdown';
8import Menu from 'bee-menus';
9const { Item } = Menu;
10const propTypes = {
11 record: PropTypes.object,
12 clsPrefix: PropTypes.string,
13 index: PropTypes.number,
14 indent: PropTypes.number,
15 indentSize: PropTypes.number,
16 column: PropTypes.object,
17 expandIcon: PropTypes.node,
18 onPaste:PropTypes.func
19};
20
21class TableCell extends Component{
22 constructor(props){
23 super(props);
24 this.isInvalidRenderCellText = this.isInvalidRenderCellText.bind(this);
25 this.handleClick = this.handleClick.bind(this);
26 this.state = {
27 showDropdowm: false
28 }
29 }
30 isInvalidRenderCellText(text) {
31 return text && !React.isValidElement(text) &&
32 Object.prototype.toString.call(text) === '[object Object]';
33 }
34 handleClick(e) {
35 const { record, column: { onCellClick } } = this.props;
36 if (onCellClick) {
37 onCellClick(record, e);
38 }
39 }
40
41 // 渲染链接类型
42 renderLinkType = ( data, record, index, config={}) => {
43 const { url, urlIndex, linkType, className, underline, descIndex, desc, linkColor } = config;
44 let linkUrl = '';
45 if(url){
46 linkUrl = url(data, record, index);
47 }
48 else if(urlIndex){
49 linkUrl = record[urlIndex];
50 }
51 if(linkUrl){
52 let link = () => {
53 window.open(linkUrl,linkType || '_blank');
54 }
55 let cls = 'u-table-link u-table-fieldtype ';
56 if(className){
57 cls += `${className} `;
58 }
59 if(underline){
60 cls += 'u-table-link-underline ';
61 }
62 let title = '';
63
64 if(desc === true){
65 title = linkUrl;
66 }
67 else if( typeof desc === 'string'){
68 title = desc;
69 }
70 else if( typeof desc === 'function'){
71 title = desc(data, record, index);
72 }
73 else if(descIndex){
74 title = record[descIndex];
75 }
76 return <span onClick={link} className={cls} style={{color:linkColor || ''}} title={title}>{data}</span>
77 }
78 return data;
79 }
80
81 // 渲染布尔类型
82 renderBoolType = ( data, config={} ) => {
83 let locale = getComponentLocale(this.props, this.context, 'Table', () => i18n);
84 let boolConfig = {...{ trueText: locale['bool_true'], falseText: locale['bool_false']},...config};
85 if(typeof data === 'string'){
86 if(data === 'false' || data === '0'){
87 return boolConfig.falseText;
88 }
89 }
90 else if(!data){
91 return boolConfig.falseText;
92 }
93 return boolConfig.trueText;
94 }
95
96 // 渲染整数/货币类型
97 renderNumber = (data, config={}, width=200) => {
98 const { precision, thousand, makeUp, preSymbol, nextSymbol } = config;
99 let number = formatMoney(data, precision, thousand);
100 if(makeUp === false && number.indexOf('.') !== -1) {
101 number = number.replace(/0*$/,'').replace(/\.$/,'');
102 }
103 let numberWidth = parseInt(width) - 16; // 减去默认的左右padding共计16px
104 let res = <span className='u-table-currency-number' >{number}</span>;
105 let pre = preSymbol ? <span className='u-table-currency-pre'>{preSymbol}</span> : null;
106 let next = nextSymbol ? <span className='u-table-currency-next'>{nextSymbol}</span> : null;
107 let title = '';
108 title += typeof preSymbol === 'string' ? preSymbol : '';
109 title += number;
110 title += typeof nextSymbol === 'string' ? nextSymbol : '';
111 return <span className='u-table-currency u-table-fieldtype' style={{width:numberWidth}} title={title}>
112 {pre}
113 {res}
114 {next}
115 </span>;
116 }
117
118 // 渲染时间类型-l
119 renderDate = ( data, config={}) => {
120 const { moment, format } = config;
121 if(!moment)return data;
122 return moment(data).format(format || 'YYYY-MM-DD');
123 }
124
125 // 渲染下拉类型,主要为编辑表格铺垫
126 renderSelect = ( data, config={}) => {
127 if(config.options){
128 data = config.options[data] || config.defaultShow;
129 }
130 return data;
131 }
132
133
134 // 渲染行内菜单
135 renderColumnMenu = (colMenu, text, record, index) => {
136 if (!colMenu) return null;
137 let { menu, trigger = 'hover', className = '', icon = <i className='uf uf-3dot-h' />, iconSize = 21 } = colMenu;
138 let items = [];
139 items = menu.map((item) => {
140 return <Item key={item.key} onClick={() => { this.onClickColMenu(item.callback, text, record, index) }}>
141 {item.icon}
142 {item.text}
143 </Item>
144 })
145 if (items.length === 0) return null;
146 className += ' u-table-inline-op-dropdowm'
147 let menus = <Menu className={className}>{items}</Menu>;
148 let top = `calc(50% - ${iconSize / 2}px)`;
149 let visibility = this.state.showDropdowm ? 'visible' : '';
150 let iconClassName = `u-table-inline-op-icon u-table-inline-op-icon-hover`;
151 return <Dropdown
152 trigger={[trigger]}
153 overlay={menus}
154 animation="slide-up"
155 onVisibleChange={this.changeShowDropdowm}
156 >
157 {<span className={iconClassName} style={{ fontSize: iconSize, top: top, visibility: visibility }}>{icon}</span>}
158 </Dropdown>
159 }
160
161 // 下拉按钮状态改变,点击后保持图标常驻
162 changeShowDropdowm = (val) => {
163 this.setState({
164 showDropdowm: val
165 })
166 }
167
168 // 菜单点击事件
169 onClickColMenu = (callback, text, record, index) => {
170 if (callback) {
171 callback(text, record, index);
172 }
173 this.setState({
174 showDropdowm: false,
175 })
176 }
177 onPaste=(e)=>{
178 let { index:row,onPaste,fixed,col } = this.props
179 let position = {
180 row,
181 col,
182 fixed:!!fixed
183 }
184 onPaste(e,position)
185 }
186
187 render() {
188 const { record, indentSize, clsPrefix, indent,
189 index, expandIcon, column ,fixed,showSum, bodyDisplayInRow,lazyStartIndex,lazyEndIndex} = this.props;
190 const { dataIndex, render, fieldType, linkConfig, fontColor, bgColor,...other } = column;
191 let {className = ''} = column;
192
193 let text = objectPath.get(record, dataIndex);
194 let tdProps;
195 let colSpan;
196 let rowSpan,title;
197
198 if (render && !showSum) {
199 text = render(text, record, index,{
200 dataIndex, render, fieldType, linkConfig, fontColor, bgColor,...other
201 });
202 if (this.isInvalidRenderCellText(text)) {
203 tdProps = text.props || {};
204 rowSpan = (tdProps.rowSpan>lazyEndIndex && lazyEndIndex>5)?lazyEndIndex-index:tdProps.rowSpan;
205 colSpan = tdProps.colSpan;
206 text = text.children;
207 }
208 }
209
210 let colMenu = this.renderColumnMenu(column.cellMenu, text, record, index);
211 // 根据 fieldType 来渲染数据
212 if(!render){
213 switch(column.fieldType){
214 case 'link':{
215 text = this.renderLinkType(text, record, index, column.linkConfig);
216 break;
217 }
218 case 'bool':{
219 text = this.renderBoolType(text, column.boolConfig);
220 break;
221 }
222 case 'currency':{
223 let config = {
224 precision: 2, // 精度值,需要大于0
225 thousand: true, // 是否显示千分符号
226 makeUp: true, // 末位是否补零
227 preSymbol: '', // 前置符号
228 nextSymbol: '', // 后置符号
229 }
230 text = this.renderNumber(text, {...config,...column.currencyConfig}, column.width);
231 break;
232 }
233 case 'number':{
234 let config = {
235 precision: 0, // 精度值,需要大于0
236 thousand: true, // 是否显示千分符号
237 makeUp: false, // 末位是否补零
238 preSymbol: '', // 前置符号
239 nextSymbol: '', // 后置符号
240 }
241 text = this.renderNumber(text, {...config,...column.numberConfig}, column.width);
242 break;
243 }
244 case 'date':{
245 text = this.renderDate(text, column.dateConfig);
246 break;
247 }
248 case 'select':{
249 text = this.renderSelect(text, column.selectConfig);
250 break;
251 }
252 default : {
253 break;
254 }
255 }
256 }
257
258 if (this.isInvalidRenderCellText(text)) {
259 text = null;
260 }
261
262 const indentText = expandIcon ? (
263 <span
264 style={{ paddingLeft: `${indentSize * indent}px` }}
265 className={`${clsPrefix}-indent indent-level-${indent}`}
266 />
267 ) : null;
268
269 if ((lazyStartIndex !==index) &&(rowSpan === 0 || colSpan === 0) ) {
270 return null;
271 }
272 if(tdProps && tdProps.mergeEndIndex && index<tdProps.mergeEndIndex && rowSpan === 0){
273 rowSpan = tdProps.mergeEndIndex - index;
274 text = ''
275 }
276 //不是固定表格并且当前列是固定,则隐藏当前列
277 if(column.fixed && !fixed){
278 className = className+` ${clsPrefix}-fixed-columns-in-body`;
279 }
280 if(column.contentAlign){
281 className = className+` text-${column.contentAlign}`;
282 }
283 else if(column.textAlign){
284 className = className+` text-${column.textAlign}`;
285 }
286 if((typeof text == 'string' || typeof text === 'number') && bodyDisplayInRow){
287 title = text
288 }
289 if(expandIcon && expandIcon.props.expandable){
290 className = className+` ${clsPrefix}-has-expandIcon`
291 }
292 if(colMenu){
293 className += ' u-table-inline-icon'
294 }
295 if(colSpan==0)return null;
296 return <td
297 draggable={column.draggable}
298 colSpan={colSpan}
299 rowSpan={rowSpan}
300 className={className}
301 onClick={this.handleClick}
302 title={title}
303 onPaste={this.onPaste}
304 style={{maxWidth:column.width, color:fontColor, backgroundColor:bgColor, ...column.style}}>
305 {indentText}
306 {expandIcon}
307 {text}
308 {colMenu}
309 </td>
310 }
311};
312
313TableCell.propTypes = propTypes;
314
315export default TableCell;