bee-table/demo/demolist/Demo2.js

204 lines
4.4 KiB
JavaScript
Raw Normal View History

2016-12-26 22:04:16 +08:00
/**
*
2017-01-12 08:53:51 +08:00
* @title 增删改表格
2017-10-12 16:54:17 +08:00
* @description 这是带有增删改功能的表格此编辑功能未使用render组件
2016-12-26 22:04:16 +08:00
*
*/
import Button from "bee-button";
import React, { Component } from "react";
import Table from "../../src";
import Animate from "bee-animate";
2017-09-08 15:07:55 +08:00
import Icon from "bee-icon";
import Input from "bee-form-control";
import Popconfirm from "bee-popconfirm";
2017-09-08 15:07:55 +08:00
2017-01-11 17:01:50 +08:00
class EditableCell extends React.Component {
state = {
value: this.props.value,
editable: false
};
handleChange = e => {
2017-01-11 17:01:50 +08:00
const value = e.target.value;
this.setState({ value });
};
2017-01-11 17:01:50 +08:00
check = () => {
this.setState({ editable: false });
if (this.props.onChange) {
this.props.onChange(this.state.value);
}
};
2017-01-11 17:01:50 +08:00
edit = () => {
this.setState({ editable: true });
};
handleKeydown = event => {
if (event.keyCode == 13) {
this.check();
}
};
2017-01-11 17:01:50 +08:00
render() {
const { value, editable } = this.state;
return (
<div className="editable-cell">
{editable ? (
<div className="editable-cell-input-wrapper">
<Input
value={value}
onChange={this.handleChange}
onKeyDown={this.handleKeydown}
/>
<Icon
type="uf-correct"
className="editable-cell-icon-check"
onClick={this.check}
/>
</div>
) : (
<div className="editable-cell-text-wrapper">
{value || " "}
<Icon
type="uf-pencil"
className="editable-cell-icon"
onClick={this.edit}
/>
</div>
)}
</div>
);
2017-01-11 17:01:50 +08:00
}
}
2017-09-11 16:02:03 +08:00
class Demo2 extends React.Component {
2017-01-11 17:01:50 +08:00
constructor(props) {
super(props);
this.columns = [
{
title: "姓名",
dataIndex: "name",
key: "name",
width: "30%",
render: (text, record, index) => (
<EditableCell
value={text}
onChange={this.onCellChange(index, "name")}
/>
)
2017-01-11 17:01:50 +08:00
},
{
title: "年龄",
dataIndex: "age",
key: "age"
},
{
title: "你懂的",
dataIndex: "address",
key: "address"
},
{
title: "操作",
dataIndex: "operation",
key: "operation",
render: (text, record, index) => {
return this.state.dataSource.length > 1 ? (
<Popconfirm content="确认删除?" id="aa" onClose={this.onDelete(index)}>
<Icon type="uf-del" />
</Popconfirm>
) : null;
}
}
];
2016-12-26 22:04:16 +08:00
2017-01-11 17:01:50 +08:00
this.state = {
dataSource: [
{
key: "0",
name: "沉鱼",
age: "18",
address: "96, 77, 89"
},
{
key: "1",
name: "落雁",
age: "16",
address: "90, 70, 80"
},
{
key: "2",
name: "闭月",
age: "17",
address: "80, 60, 80"
},
{
key: "3",
name: "羞花",
age: "20",
address: "120, 60, 90"
}
],
count: 4
2017-01-11 17:01:50 +08:00
};
}
onCellChange = (index, key) => {
return value => {
2017-01-11 17:01:50 +08:00
const dataSource = [...this.state.dataSource];
dataSource[index][key] = value;
this.setState({ dataSource });
};
};
onDelete = index => {
2017-01-11 17:01:50 +08:00
return () => {
const dataSource = [...this.state.dataSource];
dataSource.splice(index, 1);
this.setState({ dataSource });
};
};
2017-01-11 17:01:50 +08:00
handleAdd = () => {
const { count, dataSource } = this.state;
const newData = {
key: count,
name: `凤姐 ${count}`,
age: 32,
address: `100 100 100`
2017-01-11 17:01:50 +08:00
};
this.setState({
dataSource: [...dataSource, newData],
count: count + 1
2017-01-11 17:01:50 +08:00
});
};
2016-12-26 22:04:16 +08:00
getBodyWrapper = body => {
2017-01-11 17:01:50 +08:00
return (
<Animate
transitionName="move"
component="tbody"
className={body.props.className}
>
2017-01-11 17:01:50 +08:00
{body.props.children}
</Animate>
);
};
2017-01-11 17:01:50 +08:00
render() {
const { dataSource } = this.state;
const columns = this.columns;
return (
<div>
<Button
className="editable-add-btn"
type="ghost"
onClick={this.handleAdd}
>
添加
</Button>
<Table
bordered
data={dataSource}
columns={columns}
getBodyWrapper={this.getBodyWrapper}
/>
</div>
);
2017-01-11 17:01:50 +08:00
}
2016-12-26 22:04:16 +08:00
}
2017-08-30 13:31:26 +08:00
2017-09-08 15:07:55 +08:00
export default Demo2;