forked from alibaba/ice
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCellEditor.jsx
More file actions
110 lines (99 loc) · 2.25 KB
/
CellEditor.jsx
File metadata and controls
110 lines (99 loc) · 2.25 KB
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
/* eslint no-unused-expressions: 0 */
import React, { Component } from 'react';
import { Icon, Input } from '@icedesign/base';
export default class CellEditor extends Component {
static displayName = 'CellEditor';
constructor(props) {
super(props);
this.tempValue = '';
this.state = {
editMode: false,
value: props.value || '',
};
}
componentWillReceiveProps(nextProps) {
if ('value' in nextProps) {
this.setState({
value: nextProps.value,
});
}
}
editThisCell = () => {
// 缓存数据以便回滚
this.tempValue = this.state.value;
this.setState({
editMode: true,
});
};
onValueChange = (value) => {
this.setState({
value,
});
};
updateValue = () => {
this.setState({
editMode: false,
});
const { index, valueKey } = this.props;
const { value } = this.state;
this.props.onChange && this.props.onChange(index, valueKey, value);
};
rollBackThisCell = () => {
this.setState({
value: this.tempValue,
editMode: false,
});
this.tempValue = '';
};
render() {
const { value, editMode } = this.state;
if (editMode) {
return (
<div className="celleditor">
<Input
style={styles.cellInput}
value={value}
onChange={this.onValueChange}
/>
<span
style={styles.operationIcon}
title="确定"
onClick={this.updateValue}
>
<Icon size="xs" type="select" />
</span>
<span
style={styles.operationIcon}
title="撤销"
onClick={this.rollBackThisCell}
>
<Icon size="xs" type="refresh" />
</span>
</div>
);
}
return (
<div className="celleditor">
<span>{value}</span>
<span
style={styles.operationIcon}
className="celleditor-trigger"
title="编辑"
onClick={this.editThisCell}
>
<Icon size="xs" type="edit" />
</span>
</div>
);
}
}
const styles = {
cellInput: {
width: 'calc(100% - 44px)',
},
operationIcon: {
marginLeft: '10px',
color: '#999',
cursor: 'pointer',
},
};