bee-table6/demo/demolist/Demo9.js

164 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-08-30 11:18:38 +08:00
/**
2017-09-29 16:04:08 +08:00
*
* @title 表格+搜索
* @description 搜索刷新表格数据
*
*
* import {Table} from 'tinper-bee';
*/
2017-08-30 11:18:38 +08:00
2017-10-25 16:41:38 +08:00
import React, { Component } from "react";
2017-08-30 13:31:26 +08:00
2017-10-25 16:41:38 +08:00
import Table from "../../src";
2017-08-30 13:31:26 +08:00
import Icon from "bee-icon";
2017-10-25 16:41:38 +08:00
import InputGroup from "bee-input-group";
import FormControl from "bee-form-control";
2017-08-30 13:31:26 +08:00
2017-08-30 11:18:38 +08:00
class Search extends Component {
2017-10-25 16:41:38 +08:00
state = {
searchValue: "",
empty: false
};
2017-09-29 16:04:08 +08:00
2017-10-25 16:41:38 +08:00
/**
2017-09-29 16:04:08 +08:00
* 搜索
*/
2017-10-25 16:41:38 +08:00
handleSearch = () => {
let { onSearch } = this.props;
this.setState({
empty: true
});
onSearch && onSearch(this.state.searchValue);
};
/**
2017-09-29 16:04:08 +08:00
* 捕获回车
* @param e
*/
2017-10-25 16:41:38 +08:00
handleKeyDown = e => {
if (e.keyCode === 13) {
this.handleSearch();
}
};
2017-09-29 16:04:08 +08:00
2017-10-25 16:41:38 +08:00
/**
2017-09-29 16:04:08 +08:00
* 输入框改变
* @param e
*/
2017-10-25 16:41:38 +08:00
handleChange = e => {
this.setState({
searchValue: e.target.value
});
};
2017-09-29 16:04:08 +08:00
2017-10-25 16:41:38 +08:00
/**
2017-09-29 16:04:08 +08:00
* 清空输入框
*/
2017-10-25 16:41:38 +08:00
emptySearch = () => {
let { onEmpty } = this.props;
this.setState({
searchValue: "",
empty: false
});
onEmpty && onEmpty();
};
render() {
return (
<InputGroup simple className="search-component">
<FormControl
onChange={this.handleChange}
value={this.state.searchValue}
onKeyDown={this.handleKeyDown}
placeholder="请输入用户名"
type="text"
/>
{this.state.empty ? (
<Icon
type="uf-close-c"
onClick={this.emptySearch}
className="empty-search"
/>
) : null}
<InputGroup.Button onClick={this.handleSearch} shape="border">
<Icon type="uf-search" />
</InputGroup.Button>
</InputGroup>
);
}
2017-08-30 11:18:38 +08:00
}
const columns9 = [
2017-10-25 16:41:38 +08:00
{
title: "姓名",
dataIndex: "a",
key: "a",
width: 100
},
{
title: "性别",
dataIndex: "b",
key: "b",
width: 100
},
{
title: "年龄",
dataIndex: "c",
key: "c",
width: 200
},
{
title: "武功级别",
dataIndex: "d",
key: "d"
}
2017-09-29 16:04:08 +08:00
];
const userData = [
2017-10-25 16:41:38 +08:00
{ a: "杨过", b: "男", c: 30, d: "内行", key: "2" },
{ a: "令狐冲", b: "男", c: 41, d: "大侠", key: "1" },
{ a: "郭靖", b: "男", c: 25, d: "大侠", key: "3" }
2017-08-30 11:18:38 +08:00
];
class Demo9 extends Component {
2017-10-25 16:41:38 +08:00
constructor(props) {
super(props);
this.state = {
data: userData
};
}
2017-09-29 16:04:08 +08:00
2017-10-25 16:41:38 +08:00
handleSearch = value => {
if (value === "") {
return this.setState({
data: userData
});
2017-09-29 16:04:08 +08:00
}
2017-10-25 16:41:38 +08:00
let regExp = new RegExp(value, "ig");
let data = userData.filter(item => regExp.test(item.a));
this.setState({
data
});
};
handleEmpty = () => {
this.setState({
data: userData
});
};
render() {
return (
<div>
<div className="clearfix">
<Search onSearch={this.handleSearch} onEmpty={this.handleEmpty} />
</div>
<Table columns={columns9} data={this.state.data} />
</div>
);
}
2017-08-30 11:18:38 +08:00
}
2017-08-30 13:31:26 +08:00
2017-10-25 16:41:38 +08:00
export default Demo9;