2017-09-25 15:36:23 +08:00
"use strict" ;
2017-11-29 16:51:07 +08:00
Object . defineProperty ( exports , "__esModule" , {
value : true
} ) ;
2017-09-25 15:36:23 +08:00
var _extends = Object . assign || function ( target ) { for ( var i = 1 ; i < arguments . length ; i ++ ) { var source = arguments [ i ] ; for ( var key in source ) { if ( Object . prototype . hasOwnProperty . call ( source , key ) ) { target [ key ] = source [ key ] ; } } } return target ; } ;
2017-11-29 16:51:07 +08:00
exports [ "default" ] = multiSelect ;
2017-09-25 15:36:23 +08:00
var _react = require ( "react" ) ;
var _react2 = _interopRequireDefault ( _react ) ;
var _beeCheckbox = require ( "bee-checkbox" ) ;
var _beeCheckbox2 = _interopRequireDefault ( _beeCheckbox ) ;
function _interopRequireDefault ( obj ) { return obj && obj . _ _esModule ? obj : { "default" : obj } ; }
function _defaults ( obj , defaults ) { var keys = Object . getOwnPropertyNames ( defaults ) ; for ( var i = 0 ; i < keys . length ; i ++ ) { var key = keys [ i ] ; var value = Object . getOwnPropertyDescriptor ( defaults , key ) ; if ( value && value . configurable && obj [ key ] === undefined ) { Object . defineProperty ( obj , key , value ) ; } } return obj ; }
function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
function _possibleConstructorReturn ( self , call ) { if ( ! self ) { throw new ReferenceError ( "this hasn't been initialised - super() hasn't been called" ) ; } return call && ( typeof call === "object" || typeof call === "function" ) ? call : self ; }
function _inherits ( subClass , superClass ) { if ( typeof superClass !== "function" && superClass !== null ) { throw new TypeError ( "Super expression must either be null or a function, not " + typeof superClass ) ; } subClass . prototype = Object . create ( superClass && superClass . prototype , { constructor : { value : subClass , enumerable : false , writable : true , configurable : true } } ) ; if ( superClass ) Object . setPrototypeOf ? Object . setPrototypeOf ( subClass , superClass ) : _defaults ( subClass , superClass ) ; }
/ * *
* multiSelect = {
* type -- 默认值为checkbox
* param -- 可以设置返回的选中的数据属性 ; 默认值 : null ;
* }
* getSelectedDataFunc -- function , 能获取到选中的数据
* 使用全选时得注意 , data中的key值一定要是唯一值
* /
2017-11-29 16:51:07 +08:00
function multiSelect ( Table ) {
2018-01-03 14:19:24 +08:00
var _class , _temp , _initialiseProps ;
2017-09-25 15:36:23 +08:00
Array . prototype . indexOf = function ( val ) {
for ( var i = 0 ; i < this . length ; i ++ ) {
if ( this [ i ] == val ) return i ;
}
return - 1 ;
} ;
Array . prototype . remove = function ( val ) {
var index = this . indexOf ( val ) ;
if ( index > - 1 ) {
this . splice ( index , 1 ) ;
}
} ;
2018-01-03 14:19:24 +08:00
return _temp = _class = function ( _Component ) {
_inherits ( multiSelect , _Component ) ;
2017-09-25 15:36:23 +08:00
2018-01-03 14:19:24 +08:00
function multiSelect ( props ) {
_classCallCheck ( this , multiSelect ) ;
2017-09-25 15:36:23 +08:00
var _this = _possibleConstructorReturn ( this , _Component . call ( this , props ) ) ;
2018-01-03 14:19:24 +08:00
_initialiseProps . call ( _this ) ;
2017-09-25 15:36:23 +08:00
2018-01-03 14:19:24 +08:00
var data = props . data ,
checkedObj = _this . initCheckedObj ( props ) ;
2017-09-25 15:36:23 +08:00
_this . state = {
checkedAll : false ,
2018-01-03 14:19:24 +08:00
checkedObj : checkedObj ,
2017-09-25 15:36:23 +08:00
selIds : [ ] ,
data : _this . props . data
} ;
return _this ;
}
2018-01-03 14:19:24 +08:00
multiSelect . prototype . componentWillReceiveProps = function componentWillReceiveProps ( nextProps ) {
var props = this . props ,
selectDisabled = props . selectDisabled ,
selectedRow = props . selectedRow ,
data = props . data ,
checkedObj = { } ;
if ( nextProps . data !== data || nextProps . selectDisabled !== selectDisabled || nextProps . selectedRow !== selectedRow ) {
checkedObj = this . initCheckedObj ( nextProps ) ;
this . setState ( {
checkedAll : false ,
checkedObj : checkedObj ,
selIds : [ ] ,
data : nextProps . data
} ) ;
}
} ;
multiSelect . prototype . renderColumnsMultiSelect = function renderColumnsMultiSelect ( columns ) {
2017-09-25 15:36:23 +08:00
var _this2 = this ;
var data = this . state . data ;
var checkedObj = _extends ( { } , this . state . checkedObj ) ;
var checkedArray = Object . keys ( checkedObj ) ;
var multiSelect = this . props . multiSelect ;
var select _column = { } ;
var indeterminate _bool = false ;
if ( ! multiSelect || ! multiSelect . type ) {
multiSelect = _extends ( { } , multiSelect , { type : "checkbox" } ) ;
}
if ( multiSelect && multiSelect . type === "checkbox" ) {
var i = checkedArray . length ;
while ( i -- ) {
if ( checkedObj [ checkedArray [ i ] ] ) {
indeterminate _bool = true ;
break ;
}
}
var defaultColumns = [ {
title : _react2 [ "default" ] . createElement ( _beeCheckbox2 [ "default" ] , {
className : "table-checkbox" ,
checked : this . state . checkedAll ,
indeterminate : indeterminate _bool && ! this . state . checkedAll ,
onChange : this . onAllCheckChange
} ) ,
key : "checkbox" ,
dataIndex : "checkbox" ,
2018-01-03 14:19:24 +08:00
width : "100px" ,
2017-09-25 15:36:23 +08:00
render : function render ( text , record , index ) {
2018-01-16 17:18:12 +08:00
var rowKey = record [ "key" ] ? record [ "key" ] : _this2 . getRowKey ( record , i ) ;
var bool = checkedObj . hasOwnProperty ( rowKey ) ;
2017-09-25 15:36:23 +08:00
return _react2 [ "default" ] . createElement ( _beeCheckbox2 [ "default" ] , {
className : "table-checkbox" ,
2018-01-16 17:18:12 +08:00
checked : checkedObj [ rowKey ] ,
2018-01-03 14:19:24 +08:00
disabled : ! bool ,
2017-09-25 15:36:23 +08:00
onChange : _this2 . onCheckboxChange . bind ( _this2 , text , record , index )
} ) ;
}
} ] ;
columns = defaultColumns . concat ( columns ) ;
}
return columns ;
} ;
2018-01-03 14:19:24 +08:00
multiSelect . prototype . render = function render ( ) {
2018-01-16 17:18:12 +08:00
var _this3 = this ;
2017-09-25 15:36:23 +08:00
var columns = this . renderColumnsMultiSelect ( this . props . columns ) . concat ( ) ;
2018-01-16 17:18:12 +08:00
return _react2 [ "default" ] . createElement ( Table , _extends ( { ref : function ref ( table _ref ) {
_this3 . table _ref = table _ref ;
} } , this . props , { columns : columns } ) ) ;
2017-09-25 15:36:23 +08:00
} ;
2018-01-03 14:19:24 +08:00
return multiSelect ;
} ( _react . Component ) , _initialiseProps = function _initialiseProps ( ) {
2018-01-16 17:18:12 +08:00
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 ;
} ;
2018-01-03 14:19:24 +08:00
this . initCheckedObj = function ( props ) {
var checkedObj = { } ,
selectDisabled = props . selectDisabled ,
selectedRow = props . selectedRow ,
data = props . data ;
for ( var i = 0 ; i < data . length ; i ++ ) {
2018-01-03 16:14:01 +08:00
var bool = selectDisabled && selectDisabled ( data [ i ] , i ) || false ;
2018-01-16 17:18:12 +08:00
var rowKey = data [ i ] [ "key" ] ? data [ i ] [ "key" ] : _this4 . getRowKey ( data [ i ] , i ) ;
2018-01-03 14:19:24 +08:00
if ( ! bool ) {
2018-01-16 17:18:12 +08:00
checkedObj [ rowKey ] = selectedRow && selectedRow ( data [ i ] , i ) || false ;
2018-01-03 14:19:24 +08:00
}
}
return checkedObj ;
} ;
this . onAllCheckChange = function ( ) {
2018-01-16 17:18:12 +08:00
var self = _this4 ;
2018-01-03 14:19:24 +08:00
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 ++ ) {
2018-01-16 17:18:12 +08:00
var rowKey = data [ i ] [ "key" ] ? data [ i ] [ "key" ] : _this4 . getRowKey ( data [ i ] , i ) ;
var bool = checkedObj . hasOwnProperty ( rowKey ) ;
2018-01-03 14:19:24 +08:00
if ( ! bool ) {
selIds . splice ( i , 1 ) ;
} else {
2018-01-16 17:18:12 +08:00
checkedObj [ rowKey ] = ! self . state . checkedAll ;
2018-01-03 14:19:24 +08:00
}
}
self . setState ( {
checkedAll : ! self . state . checkedAll ,
checkedObj : checkedObj ,
selIds : selIds
} ) ;
self . props . getSelectedDataFunc ( selIds ) ;
} ;
this . onCheckboxChange = function ( text , record , index ) {
2018-01-16 17:18:12 +08:00
var self = _this4 ;
2018-01-03 14:19:24 +08:00
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 ;
2018-01-16 17:18:12 +08:00
var rowKey = record [ "key" ] ? record [ "key" ] : _this4 . getRowKey ( record , i ) ;
if ( checkedObj [ rowKey ] ) {
2018-01-03 14:19:24 +08:00
selIds . remove ( id ) ;
} else {
selIds . push ( id ) ;
}
2018-01-16 17:18:12 +08:00
checkedObj [ rowKey ] = ! checkedObj [ rowKey ] ;
2018-01-03 14:19:24 +08:00
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 ;
2017-11-29 16:51:07 +08:00
}
module . exports = exports [ "default" ] ;