ant-design/components/table/createBodyRow.tsx

76 lines
1.7 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import classnames from 'classnames';
import omit from 'omit.js';
import { Store } from './createStore';
2017-11-18 00:36:22 +08:00
interface BodyRowProps {
store: Store;
className?: string;
rowKey: string;
prefixCls: string;
}
2017-11-18 00:36:22 +08:00
interface BodyRowState {
selected: boolean;
}
2017-11-18 00:54:54 +08:00
export interface BodyRowClass extends React.ComponentClass {}
export default function createTableRow(Component: React.ReactType = 'tr') {
2017-11-18 00:36:22 +08:00
class BodyRow extends React.Component<BodyRowProps, BodyRowState> {
private store: Store;
2019-08-05 18:38:10 +08:00
private unsubscribe: () => void;
2017-11-21 13:48:37 +08:00
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() {
2017-11-18 00:36:22 +08:00
const rowProps = omit(this.props, ['prefixCls', 'rowKey', 'store']);
2018-12-07 16:17:45 +08:00
const className = classnames(this.props.className, {
[`${this.props.prefixCls}-row-selected`]: this.state.selected,
});
return React.createElement(
Component,
{
2018-12-07 16:17:45 +08:00
...rowProps,
className,
},
2018-12-07 16:17:45 +08:00
this.props.children,
);
}
}
2017-11-18 00:54:54 +08:00
return BodyRow as BodyRowClass;
}