forked from ant-design/ant-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createBodyRow.tsx
74 lines (61 loc) · 1.71 KB
/
createBodyRow.tsx
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
import * as React from 'react';
import classnames from 'classnames';
import omit from 'omit.js';
import { Store } from './createStore';
interface BodyRowProps {
store: Store;
className?: string;
rowKey: string;
prefixCls: string;
}
interface BodyRowState {
selected: boolean;
}
export interface BodyRowClass extends React.ComponentClass {}
export default function createTableRow(Component: React.ReactType = 'tr') {
class BodyRow extends React.Component<BodyRowProps, BodyRowState> {
private store: Store;
private unsubscribe: () => void;
constructor(props: BodyRowProps) {
super(props);
this.store = props.store;
const { selectedRowKeys } = this.store.getState();
this.state = {
selected: selectedRowKeys.indexOf(props.rowKey) >= 0,
};
}
componentDidMount() {
this.subscribe();
}
componentWillUnmount() {
if (this.unsubscribe) {
this.unsubscribe();
}
}
subscribe() {
const { store, rowKey } = this.props;
this.unsubscribe = store.subscribe(() => {
const { selectedRowKeys } = this.store.getState();
const selected = selectedRowKeys.indexOf(rowKey) >= 0;
if (selected !== this.state.selected) {
this.setState({ selected });
}
});
}
render() {
const rowProps = omit(this.props, ['prefixCls', 'rowKey', 'store']);
const className = classnames(this.props.className, {
[`${this.props.prefixCls}-row-selected`]: this.state.selected,
});
return React.createElement(
Component,
{
...rowProps,
className,
},
this.props.children,
);
}
}
return BodyRow as BodyRowClass;
}