fix(multiSelect.js): fix initCheckedObj will change selIds

This commit is contained in:
huyueb 2018-03-28 10:37:18 +08:00
parent b33c0365bd
commit cf109500e7
5 changed files with 239 additions and 220 deletions

View File

@ -35,8 +35,6 @@ function _inherits(subClass, superClass) { if (typeof superClass !== "function"
* 使用全选时得注意data中的key值一定要是唯一值 * 使用全选时得注意data中的key值一定要是唯一值
*/ */
function multiSelect(Table) { function multiSelect(Table) {
var _class, _temp, _initialiseProps;
Array.prototype.indexOf = function (val) { Array.prototype.indexOf = function (val) {
for (var i = 0; i < this.length; i++) { for (var i = 0; i < this.length; i++) {
if (this[i] == val) return i; if (this[i] == val) return i;
@ -49,7 +47,7 @@ function multiSelect(Table) {
this.splice(index, 1); this.splice(index, 1);
} }
}; };
return _temp = _class = function (_Component) { return function (_Component) {
_inherits(multiSelect, _Component); _inherits(multiSelect, _Component);
function multiSelect(props) { function multiSelect(props) {
@ -57,20 +55,121 @@ function multiSelect(Table) {
var _this = _possibleConstructorReturn(this, _Component.call(this, props)); var _this = _possibleConstructorReturn(this, _Component.call(this, props));
_initialiseProps.call(_this); _this.getRowKey = function (record, index) {
var rowKey = _this.props.rowKey || 'key';
var key = typeof rowKey === 'function' ? rowKey(record, index) : record[rowKey];
return key;
};
var data = props.data, _this.initCheckedObj = function () {
checkedObj = _this.initCheckedObj(props); var checkedObj = {},
_this$props = _this.props,
selectDisabled = _this$props.selectDisabled,
selectedRow = _this$props.selectedRow,
data = _this$props.data,
selIds_ = _this.state.selIds;
for (var i = 0; i < data.length; i++) {
var bool = selectDisabled && selectDisabled(data[i], i) || false;
var rowKey = data[i]["key"] ? data[i]["key"] : _this.getRowKey(data[i], i);
if (!bool) {
if (selectedRow && selectedRow(data[i], i)) {
selIds_.push(data[i]);
checkedObj[rowKey] = true;
} else {
checkedObj[rowKey] = false;
}
}
}
return {
checkedObj: checkedObj,
selIds: selIds_
};
};
_this.onAllCheckChange = function () {
var self = _this;
var listData = self.state.data.concat();
var checkedObj = _extends({}, self.state.checkedObj);
var data = self.props.data;
var selIds = [];
var id = self.props.multiSelect.param;
if (self.state.checkedAll) {
selIds = [];
} else {
for (var i = 0; i < listData.length; i++) {
if (id) {
selIds[i] = listData[i][id];
} else {
selIds[i] = listData[i];
}
}
}
for (var i = 0; i < data.length; i++) {
var rowKey = data[i]["key"] ? data[i]["key"] : _this.getRowKey(data[i], i);
var bool = checkedObj.hasOwnProperty(rowKey);
if (!bool) {
selIds.splice(i, 1);
} else {
checkedObj[rowKey] = !self.state.checkedAll;
}
}
self.setState({
checkedAll: !self.state.checkedAll,
checkedObj: checkedObj,
selIds: selIds
});
self.props.getSelectedDataFunc(selIds);
};
_this.onCheckboxChange = function (text, record, index) {
var self = _this;
var allFlag = false;
var selIds = self.state.selIds;
var id = self.props.multiSelect ? self.props.multiSelect.param ? record[self.props.multiSelect.param] : record : record;
var checkedObj = _extends({}, self.state.checkedObj);
var checkedArray = Object.keys(checkedObj);
var getSelectedDataFunc = self.props.getSelectedDataFunc;
var rowKey = record["key"] ? record["key"] : _this.getRowKey(record, i);
if (checkedObj[rowKey]) {
selIds.remove(id);
} else {
selIds.push(id);
}
checkedObj[rowKey] = !checkedObj[rowKey];
for (var i = 0; i < checkedArray.length; i++) {
if (!checkedObj[checkedArray[i]]) {
allFlag = false;
break;
} else {
allFlag = true;
}
}
self.setState({
checkedAll: allFlag,
checkedObj: checkedObj,
selIds: selIds
});
if (typeof getSelectedDataFunc === "function") {
getSelectedDataFunc(selIds);
}
};
_this.state = { _this.state = {
checkedAll: false, checkedAll: false,
checkedObj: checkedObj, checkedObj: {},
selIds: [], selIds: [],
data: _this.props.data data: props.data
}; };
return _this; return _this;
} }
multiSelect.prototype.componentDidMount = function componentDidMount() {
this.setState(this.initCheckedObj());
};
multiSelect.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) { multiSelect.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
var props = this.props, var props = this.props,
selectDisabled = props.selectDisabled, selectDisabled = props.selectDisabled,
@ -79,7 +178,7 @@ function multiSelect(Table) {
checkedObj = {}; checkedObj = {};
if (nextProps.data !== data || nextProps.selectDisabled !== selectDisabled || nextProps.selectedRow !== selectedRow) { if (nextProps.data !== data || nextProps.selectDisabled !== selectDisabled || nextProps.selectedRow !== selectedRow) {
checkedObj = this.initCheckedObj(nextProps); checkedObj = this.initCheckedObj(nextProps).checkedObj;
this.setState({ this.setState({
checkedAll: false, checkedAll: false,
checkedObj: checkedObj, checkedObj: checkedObj,
@ -147,100 +246,6 @@ function multiSelect(Table) {
}; };
return multiSelect; return multiSelect;
}(_react.Component), _initialiseProps = function _initialiseProps() { }(_react.Component);
var _this4 = this;
this.getRowKey = function (record, index) {
var rowKey = _this4.props.rowKey || 'key';
var key = typeof rowKey === 'function' ? rowKey(record, index) : record[rowKey];
return key;
};
this.initCheckedObj = function (props) {
var checkedObj = {},
selectDisabled = props.selectDisabled,
selectedRow = props.selectedRow,
data = props.data;
for (var i = 0; i < data.length; i++) {
var bool = selectDisabled && selectDisabled(data[i], i) || false;
var rowKey = data[i]["key"] ? data[i]["key"] : _this4.getRowKey(data[i], i);
if (!bool) {
checkedObj[rowKey] = selectedRow && selectedRow(data[i], i) || false;
}
}
return checkedObj;
};
this.onAllCheckChange = function () {
var self = _this4;
var listData = self.state.data.concat();
var checkedObj = _extends({}, self.state.checkedObj);
var data = self.props.data;
var selIds = [];
var id = self.props.multiSelect.param;
if (self.state.checkedAll) {
selIds = [];
} else {
for (var i = 0; i < listData.length; i++) {
if (id) {
selIds[i] = listData[i][id];
} else {
selIds[i] = listData[i];
}
}
}
for (var i = 0; i < data.length; i++) {
var rowKey = data[i]["key"] ? data[i]["key"] : _this4.getRowKey(data[i], i);
var bool = checkedObj.hasOwnProperty(rowKey);
if (!bool) {
selIds.splice(i, 1);
} else {
checkedObj[rowKey] = !self.state.checkedAll;
}
}
self.setState({
checkedAll: !self.state.checkedAll,
checkedObj: checkedObj,
selIds: selIds
});
self.props.getSelectedDataFunc(selIds);
};
this.onCheckboxChange = function (text, record, index) {
var self = _this4;
var allFlag = false;
var selIds = self.state.selIds;
var id = self.props.multiSelect ? self.props.multiSelect.param ? record[self.props.multiSelect.param] : record : record;
var checkedObj = _extends({}, self.state.checkedObj);
var checkedArray = Object.keys(checkedObj);
var getSelectedDataFunc = self.props.getSelectedDataFunc;
var rowKey = record["key"] ? record["key"] : _this4.getRowKey(record, i);
if (checkedObj[rowKey]) {
selIds.remove(id);
} else {
selIds.push(id);
}
checkedObj[rowKey] = !checkedObj[rowKey];
for (var i = 0; i < checkedArray.length; i++) {
if (!checkedObj[checkedArray[i]]) {
allFlag = false;
break;
} else {
allFlag = true;
}
}
self.setState({
checkedAll: allFlag,
checkedObj: checkedObj,
selIds: selIds
});
if (typeof getSelectedDataFunc === "function") {
getSelectedDataFunc(selIds);
}
};
}, _temp;
} }
module.exports = exports["default"]; module.exports = exports["default"];

213
dist/demo.js vendored
View File

@ -9969,8 +9969,6 @@
* 使用全选时得注意data中的key值一定要是唯一值 * 使用全选时得注意data中的key值一定要是唯一值
*/ */
function multiSelect(Table) { function multiSelect(Table) {
var _class, _temp, _initialiseProps;
Array.prototype.indexOf = function (val) { Array.prototype.indexOf = function (val) {
for (var i = 0; i < this.length; i++) { for (var i = 0; i < this.length; i++) {
if (this[i] == val) return i; if (this[i] == val) return i;
@ -9983,7 +9981,7 @@
this.splice(index, 1); this.splice(index, 1);
} }
}; };
return _temp = _class = function (_Component) { return function (_Component) {
_inherits(multiSelect, _Component); _inherits(multiSelect, _Component);
function multiSelect(props) { function multiSelect(props) {
@ -9991,20 +9989,121 @@
var _this = _possibleConstructorReturn(this, _Component.call(this, props)); var _this = _possibleConstructorReturn(this, _Component.call(this, props));
_initialiseProps.call(_this); _this.getRowKey = function (record, index) {
var rowKey = _this.props.rowKey || 'key';
var key = typeof rowKey === 'function' ? rowKey(record, index) : record[rowKey];
return key;
};
var data = props.data, _this.initCheckedObj = function () {
checkedObj = _this.initCheckedObj(props); var checkedObj = {},
_this$props = _this.props,
selectDisabled = _this$props.selectDisabled,
selectedRow = _this$props.selectedRow,
data = _this$props.data,
selIds_ = _this.state.selIds;
for (var i = 0; i < data.length; i++) {
var bool = selectDisabled && selectDisabled(data[i], i) || false;
var rowKey = data[i]["key"] ? data[i]["key"] : _this.getRowKey(data[i], i);
if (!bool) {
if (selectedRow && selectedRow(data[i], i)) {
selIds_.push(data[i]);
checkedObj[rowKey] = true;
} else {
checkedObj[rowKey] = false;
}
}
}
return {
checkedObj: checkedObj,
selIds: selIds_
};
};
_this.onAllCheckChange = function () {
var self = _this;
var listData = self.state.data.concat();
var checkedObj = _extends({}, self.state.checkedObj);
var data = self.props.data;
var selIds = [];
var id = self.props.multiSelect.param;
if (self.state.checkedAll) {
selIds = [];
} else {
for (var i = 0; i < listData.length; i++) {
if (id) {
selIds[i] = listData[i][id];
} else {
selIds[i] = listData[i];
}
}
}
for (var i = 0; i < data.length; i++) {
var rowKey = data[i]["key"] ? data[i]["key"] : _this.getRowKey(data[i], i);
var bool = checkedObj.hasOwnProperty(rowKey);
if (!bool) {
selIds.splice(i, 1);
} else {
checkedObj[rowKey] = !self.state.checkedAll;
}
}
self.setState({
checkedAll: !self.state.checkedAll,
checkedObj: checkedObj,
selIds: selIds
});
self.props.getSelectedDataFunc(selIds);
};
_this.onCheckboxChange = function (text, record, index) {
var self = _this;
var allFlag = false;
var selIds = self.state.selIds;
var id = self.props.multiSelect ? self.props.multiSelect.param ? record[self.props.multiSelect.param] : record : record;
var checkedObj = _extends({}, self.state.checkedObj);
var checkedArray = Object.keys(checkedObj);
var getSelectedDataFunc = self.props.getSelectedDataFunc;
var rowKey = record["key"] ? record["key"] : _this.getRowKey(record, i);
if (checkedObj[rowKey]) {
selIds.remove(id);
} else {
selIds.push(id);
}
checkedObj[rowKey] = !checkedObj[rowKey];
for (var i = 0; i < checkedArray.length; i++) {
if (!checkedObj[checkedArray[i]]) {
allFlag = false;
break;
} else {
allFlag = true;
}
}
self.setState({
checkedAll: allFlag,
checkedObj: checkedObj,
selIds: selIds
});
if (typeof getSelectedDataFunc === "function") {
getSelectedDataFunc(selIds);
}
};
_this.state = { _this.state = {
checkedAll: false, checkedAll: false,
checkedObj: checkedObj, checkedObj: {},
selIds: [], selIds: [],
data: _this.props.data data: props.data
}; };
return _this; return _this;
} }
multiSelect.prototype.componentDidMount = function componentDidMount() {
this.setState(this.initCheckedObj());
};
multiSelect.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) { multiSelect.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
var props = this.props, var props = this.props,
selectDisabled = props.selectDisabled, selectDisabled = props.selectDisabled,
@ -10013,7 +10112,7 @@
checkedObj = {}; checkedObj = {};
if (nextProps.data !== data || nextProps.selectDisabled !== selectDisabled || nextProps.selectedRow !== selectedRow) { if (nextProps.data !== data || nextProps.selectDisabled !== selectDisabled || nextProps.selectedRow !== selectedRow) {
checkedObj = this.initCheckedObj(nextProps); checkedObj = this.initCheckedObj(nextProps).checkedObj;
this.setState({ this.setState({
checkedAll: false, checkedAll: false,
checkedObj: checkedObj, checkedObj: checkedObj,
@ -10081,101 +10180,7 @@
}; };
return multiSelect; return multiSelect;
}(_react.Component), _initialiseProps = function _initialiseProps() { }(_react.Component);
var _this4 = this;
this.getRowKey = function (record, index) {
var rowKey = _this4.props.rowKey || 'key';
var key = typeof rowKey === 'function' ? rowKey(record, index) : record[rowKey];
return key;
};
this.initCheckedObj = function (props) {
var checkedObj = {},
selectDisabled = props.selectDisabled,
selectedRow = props.selectedRow,
data = props.data;
for (var i = 0; i < data.length; i++) {
var bool = selectDisabled && selectDisabled(data[i], i) || false;
var rowKey = data[i]["key"] ? data[i]["key"] : _this4.getRowKey(data[i], i);
if (!bool) {
checkedObj[rowKey] = selectedRow && selectedRow(data[i], i) || false;
}
}
return checkedObj;
};
this.onAllCheckChange = function () {
var self = _this4;
var listData = self.state.data.concat();
var checkedObj = _extends({}, self.state.checkedObj);
var data = self.props.data;
var selIds = [];
var id = self.props.multiSelect.param;
if (self.state.checkedAll) {
selIds = [];
} else {
for (var i = 0; i < listData.length; i++) {
if (id) {
selIds[i] = listData[i][id];
} else {
selIds[i] = listData[i];
}
}
}
for (var i = 0; i < data.length; i++) {
var rowKey = data[i]["key"] ? data[i]["key"] : _this4.getRowKey(data[i], i);
var bool = checkedObj.hasOwnProperty(rowKey);
if (!bool) {
selIds.splice(i, 1);
} else {
checkedObj[rowKey] = !self.state.checkedAll;
}
}
self.setState({
checkedAll: !self.state.checkedAll,
checkedObj: checkedObj,
selIds: selIds
});
self.props.getSelectedDataFunc(selIds);
};
this.onCheckboxChange = function (text, record, index) {
var self = _this4;
var allFlag = false;
var selIds = self.state.selIds;
var id = self.props.multiSelect ? self.props.multiSelect.param ? record[self.props.multiSelect.param] : record : record;
var checkedObj = _extends({}, self.state.checkedObj);
var checkedArray = Object.keys(checkedObj);
var getSelectedDataFunc = self.props.getSelectedDataFunc;
var rowKey = record["key"] ? record["key"] : _this4.getRowKey(record, i);
if (checkedObj[rowKey]) {
selIds.remove(id);
} else {
selIds.push(id);
}
checkedObj[rowKey] = !checkedObj[rowKey];
for (var i = 0; i < checkedArray.length; i++) {
if (!checkedObj[checkedArray[i]]) {
allFlag = false;
break;
} else {
allFlag = true;
}
}
self.setState({
checkedAll: allFlag,
checkedObj: checkedObj,
selIds: selIds
});
if (typeof getSelectedDataFunc === "function") {
getSelectedDataFunc(selIds);
}
};
}, _temp;
} }
module.exports = exports["default"]; module.exports = exports["default"];

2
dist/demo.js.map vendored

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
{ {
"name": "bee-table", "name": "bee-table",
"version": "1.0.13", "version": "1.0.14",
"description": "Table ui component for react", "description": "Table ui component for react",
"keywords": [ "keywords": [
"react", "react",

View File

@ -24,15 +24,16 @@ export default function multiSelect(Table) {
return class multiSelect extends Component { return class multiSelect extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
let { data } = props,
checkedObj = this.initCheckedObj(props);
this.state = { this.state = {
checkedAll: false, checkedAll: false,
checkedObj: checkedObj, checkedObj: {},
selIds: [], selIds: [],
data: this.props.data data: props.data
}; };
} }
componentDidMount(){
this.setState(this.initCheckedObj());
}
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
let props = this.props, let props = this.props,
{ selectDisabled, selectedRow, data } = props, { selectDisabled, selectedRow, data } = props,
@ -42,7 +43,7 @@ export default function multiSelect(Table) {
nextProps.selectDisabled !== selectDisabled || nextProps.selectDisabled !== selectDisabled ||
nextProps.selectedRow !== selectedRow nextProps.selectedRow !== selectedRow
) { ) {
checkedObj = this.initCheckedObj(nextProps); checkedObj = this.initCheckedObj(nextProps).checkedObj;
this.setState({ this.setState({
checkedAll: false, checkedAll: false,
checkedObj: checkedObj, checkedObj: checkedObj,
@ -57,18 +58,26 @@ export default function multiSelect(Table) {
rowKey(record, index) : record[rowKey]; rowKey(record, index) : record[rowKey];
return key; return key;
} }
initCheckedObj = props => { initCheckedObj = () => {
let checkedObj = {}, let checkedObj = {},
{ selectDisabled, selectedRow, data } = props; { selectDisabled, selectedRow, data } = this.props,
selIds_ = this.state.selIds;
for (var i = 0; i < data.length; i++) { for (var i = 0; i < data.length; i++) {
let bool = (selectDisabled && selectDisabled(data[i], i)) || false; let bool = (selectDisabled && selectDisabled(data[i], i)) || false;
let rowKey = data[i]["key"] ? data[i]["key"] : this.getRowKey(data[i],i); let rowKey = data[i]["key"] ? data[i]["key"] : this.getRowKey(data[i],i);
if (!bool) { if (!bool) {
checkedObj[rowKey] = if(selectedRow && selectedRow(data[i], i)){
(selectedRow && selectedRow(data[i], i)) || false; selIds_.push(data[i]);
checkedObj[rowKey] = true;
}else{
checkedObj[rowKey] = false;
}
} }
} }
return checkedObj; return {
checkedObj:checkedObj,
selIds:selIds_
};
}; };
onAllCheckChange = () => { onAllCheckChange = () => {
let self = this; let self = this;