forked from ElemeFE/element-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilterPannel.jsx
155 lines (136 loc) · 4.43 KB
/
FilterPannel.jsx
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
import * as React from 'react';
import ReactDOM from 'react-dom';
import { Component, Transition, View } from '../../libs';
import Popper from '../../libs/utils/popper';
import Checkbox from '../checkbox';
import { FilterProps, FilterState } from './Types'
import local from '../locale';
function getPopupContainer() {
const container = document.createElement('div');
container.className = 'el-table-poper';
container.style.zIndex = 999;
document.body.appendChild(container);
return container;
}
export default class FilterPannel extends Component<FilterProps, FilterState> {
constructor(props) {
super(props);
this.container = getPopupContainer();
['handleClickOutside', 'onEnter', 'onAfterLeave'].forEach(fn => { this[fn] = this[fn].bind(this) });
this.state = {
filteredValue: props.filteredValue,
}
}
componentDidMount() {
this.renderPortal(this.renderContent(), this.container);
document.addEventListener('click', this.handleClickOutside);
}
componentWillReceiveProps(nextProps) {
if (this.props.filteredValue !== nextProps.filteredValue) {
this.setState({ filteredValue: nextProps.filteredValue })
}
}
componentDidUpdate() {
this.renderPortal(this.renderContent(), this.container);
}
componentWillUnmount() {
this.poperIns && this.poperIns.destroy();
ReactDOM.unmountComponentAtNode(this.container);
document.removeEventListener('click', this.handleClickOutside);
document.body.removeChild(this.container);
}
handleFiltersChange(value) {
this.setState({
filteredValue: value
})
}
changeFilteredValue(value) {
this.props.onFilterChange(value);
this.props.toggleFilter();
}
handleClickOutside() {
if (this.props.visible) {
this.props.toggleFilter();
}
}
onEnter() {
this.poperIns = new Popper(this.refer, this.container, {
placement: this.props.placement
});
}
onAfterLeave() {
this.poperIns.destroy();
}
renderPortal(element, container) {
ReactDOM.render(element, container);
}
renderContent() {
const { multiple, filters, visible } = this.props;
const { filteredValue } = this.state;
let content;
if (multiple) {
content = [(
<div className="el-table-filter__content" key="content">
<Checkbox.Group value={filteredValue || []} onChange={this.handleFiltersChange.bind(this)} className="el-table-filter__checkbox-group">
{filters && filters.map(filter => (
<Checkbox value={filter.value} label={filter.text} key={filter.value} />
))}
</Checkbox.Group>
</div>
), (
<div className="el-table-filter__bottom" key="bottom">
<button
className={this.classNames({ 'is-disabled': !filteredValue || !filteredValue.length })}
disabled={!filteredValue || !filteredValue.length}
onClick={this.changeFilteredValue.bind(this, filteredValue)}
>
{local.t('el.table.confirmFilter')}
</button>
<button onClick={this.changeFilteredValue.bind(this, null)}>{local.t('el.table.resetFilter')}</button>
</div>
)]
} else {
content = (
<ul className="el-table-filter__list">
<li
className={this.classNames('el-table-filter__list-item', { 'is-active': !filteredValue })}
onClick={this.changeFilteredValue.bind(this, null)}
>
{local.t('el.table.clearFilter')}
</li>
{filters && filters.map(filter => (
<li
key={filter.value}
className={this.classNames('el-table-filter__list-item', { 'is-active': filter.value === filteredValue })}
onClick={this.changeFilteredValue.bind(this, filter.value)}
>
{filter.text}
</li>
))}
</ul>
)
}
return (
<Transition
name="el-zoom-in-top"
onEnter={this.onEnter}
onAfterLeave={this.onAfterLeave}
>
<View show={visible}>
<div
className={'el-table-filter'}
ref={(dom) => { this.poper = dom; }}
onClick={(e) => { e.nativeEvent.stopImmediatePropagation() }} // prevent document click event
>
{content}
</div>
</View>
</Transition>
)
}
render() {
return React.cloneElement(this.props.children, {
ref: (dom) => { this.refer = dom; }
});
}
}