zent
Version:
一套前端设计语言和基于React的实现
54 lines (53 loc) • 2.43 kB
JavaScript
import { __assign, __extends } from "tslib";
import { jsx as _jsx } from "react/jsx-runtime";
import { PureComponent } from 'react';
import Checkbox from '../checkbox';
import Pop from '../pop';
var SelectionCheckbox = (function (_super) {
__extends(SelectionCheckbox, _super);
function SelectionCheckbox(props) {
var _this = _super.call(this, props) || this;
_this.subscribe = function () {
var store = _this.props.store;
_this.unsubscribe = store.subscribe('selectedRowKeys', function () {
var checked = _this.getCheckState(_this.props);
if (_this.state.checked !== checked) {
_this.setState({ checked: checked });
}
});
};
_this.getCheckState = function (props) {
var _a;
var store = props.store, rowIndex = props.rowIndex;
return ((_a = store.getState('selectedRowKeys')) !== null && _a !== void 0 ? _a : []).indexOf(rowIndex) !== -1;
};
_this.state = {
checked: _this.getCheckState(props),
};
return _this;
}
SelectionCheckbox.prototype.componentDidMount = function () {
this.subscribe();
};
SelectionCheckbox.prototype.componentWillReceiveProps = function (nextProps, nextState) {
var checked = this.getCheckState(nextProps);
if (checked !== nextState.checked) {
this.setState({ checked: checked });
}
};
SelectionCheckbox.prototype.componentWillUnmount = function () {
if (this.unsubscribe) {
this.unsubscribe();
}
};
SelectionCheckbox.prototype.render = function () {
var _a = this.props, onChange = _a.onChange, disabled = _a.disabled, reason = _a.reason, indeterminate = _a.indeterminate;
var checked = this.state.checked;
if (indeterminate) {
checked = false;
}
return reason && disabled ? (_jsx(Pop, __assign({ content: reason, trigger: "hover", position: "top-left", centerArrow: true }, { children: _jsx(Checkbox, { onChange: onChange, indeterminate: indeterminate, checked: checked, disabled: disabled }, void 0) }), void 0)) : (_jsx(Checkbox, { onChange: onChange, indeterminate: indeterminate, checked: checked, disabled: disabled }, void 0));
};
return SelectionCheckbox;
}(PureComponent));
export default SelectionCheckbox;