2019-09-03 09:34:11 +08:00
'use strict' ;
2017-09-25 15:36:23 +08:00
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 ;
2019-09-03 09:34:11 +08:00
var _react = require ( 'react' ) ;
2017-09-25 15:36:23 +08:00
var _react2 = _interopRequireDefault ( _react ) ;
2019-09-03 09:34:11 +08:00
var _propTypes = require ( 'prop-types' ) ;
var _propTypes2 = _interopRequireDefault ( _propTypes ) ;
var _util = require ( './util' ) ;
2018-11-16 14:51:40 +08:00
2017-09-25 15:36:23 +08:00
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 ) ; }
/ * *
2018-11-16 14:51:40 +08:00
* 参数 : 过滤表头
* @ param { * } Table
* @ param { * } Checkbox
* @ param { * } Popover
* @ param { * } Icon
2017-09-25 15:36:23 +08:00
* /
2018-11-16 14:51:40 +08:00
2018-05-22 19:38:15 +08:00
function multiSelect ( Table , Checkbox ) {
2018-04-02 14:22:39 +08:00
var _class , _temp , _initialiseProps ;
return _temp = _class = function ( _Component ) {
2019-03-20 12:38:43 +08:00
_inherits ( MultiSelect , _Component ) ;
2017-09-25 15:36:23 +08:00
2019-03-20 12:38:43 +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-04-02 14:22:39 +08:00
_initialiseProps . call ( _this ) ;
2017-09-25 15:36:23 +08:00
2018-11-16 14:51:40 +08:00
var obj = _this . getCheckedOrIndeter ( props . data ) ;
_this . state = _extends ( { } , obj , {
data : ( 0 , _util . ObjectAssign ) ( props . data )
} ) ;
2017-09-25 15:36:23 +08:00
return _this ;
}
2019-03-20 12:38:43 +08:00
MultiSelect . prototype . componentWillReceiveProps = function componentWillReceiveProps ( nextProps ) {
2019-03-27 16:53:26 +08:00
if ( 'data' in nextProps ) {
2018-11-20 10:59:24 +08:00
var obj = this . getCheckedOrIndeter ( nextProps . data ) ;
2018-11-16 14:51:40 +08:00
this . setState ( _extends ( { } , obj , {
data : ( 0 , _util . ObjectAssign ) ( nextProps . data )
} ) ) ;
}
2018-03-28 10:37:18 +08:00
} ;
2018-11-16 14:51:40 +08:00
/ * *
* @ param { * } data
* /
2019-03-20 12:38:43 +08:00
MultiSelect . prototype . getCheckedOrIndeter = function getCheckedOrIndeter ( data ) {
2018-11-16 14:51:40 +08:00
var obj = { } ;
2019-08-27 16:45:52 +08:00
var checkStatus = this . checkAllSelected ( data ) ;
2018-11-16 14:51:40 +08:00
if ( ! checkStatus ) {
obj . checkedAll = false ;
obj . indeterminate = false ;
return obj ;
}
if ( checkStatus == 'indeter' ) {
obj . indeterminate = true ;
obj . checkedAll = false ;
} else if ( checkStatus == 'all' ) {
obj . checkedAll = true ;
obj . indeterminate = false ;
2018-01-03 14:19:24 +08:00
}
2018-11-16 14:51:40 +08:00
return obj ;
2018-01-03 14:19:24 +08:00
} ;
2018-11-16 14:51:40 +08:00
/ * *
* 判断数据是否全部选中
* @ param { * } data
2019-08-27 16:45:52 +08:00
* return string all ( 全选 ) 、 indeter ( 半选 )
2018-11-16 14:51:40 +08:00
* /
2017-09-25 15:36:23 +08:00
2019-03-20 12:38:43 +08:00
MultiSelect . prototype . setChecked = function setChecked ( data ) {
2018-11-16 14:51:40 +08:00
if ( ! this . isArray ( data ) ) return false ;
2018-12-04 16:25:22 +08:00
if ( data . length == 0 ) return false ;
2018-11-16 14:51:40 +08:00
var count = 0 ;
2018-12-05 10:03:11 +08:00
var disabledCount = 0 ;
2018-11-16 14:51:40 +08:00
data . forEach ( function ( da ) {
2019-04-23 16:04:02 +08:00
if ( da . _checked && ! da . _disabled ) {
2018-11-16 14:51:40 +08:00
count ++ ;
2017-09-25 15:36:23 +08:00
}
2018-12-05 10:03:11 +08:00
if ( da . _disabled ) {
disabledCount ++ ;
}
2018-11-16 14:51:40 +08:00
} ) ;
2019-05-22 18:07:03 +08:00
if ( data . length == count + disabledCount && count > 0 ) {
2018-11-16 14:51:40 +08:00
return "all" ;
2017-09-25 15:36:23 +08:00
}
2018-11-16 14:51:40 +08:00
return count == 0 ? false : "indeter" ;
2017-09-25 15:36:23 +08:00
} ;
2018-11-16 14:51:40 +08:00
/ * *
2019-08-27 16:45:52 +08:00
* 重写 : 判断数据是否全部选中
2018-11-16 14:51:40 +08:00
* /
2018-01-16 17:18:12 +08:00
2018-11-16 14:51:40 +08:00
2019-08-27 16:45:52 +08:00
/ * *
* 判断是否是数组
* @ param { * } o
* /
2019-03-20 12:38:43 +08:00
MultiSelect . prototype . isArray = function isArray ( o ) {
2018-11-16 14:51:40 +08:00
return Object . prototype . toString . call ( o ) == '[object Array]' ;
2017-09-25 15:36:23 +08:00
} ;
2019-08-27 16:45:52 +08:00
/ * *
* 遍历树节点和它的子孙节点 , 设置 _checked
* /
/ * *
* 遍历树节点和它的子孙节点 , 获取对应状态的节点数组
* /
2019-08-23 13:48:27 +08:00
// 实现行点击时触发多选框勾选的需求
2019-03-20 12:38:43 +08:00
MultiSelect . prototype . render = function render ( ) {
2019-08-27 16:45:52 +08:00
var _props = this . props ,
columns = _props . columns ,
expandIconColumnIndex = _props . expandIconColumnIndex ;
2018-11-16 14:51:40 +08:00
var data = this . state . data ;
2018-04-02 14:22:39 +08:00
2019-08-27 16:45:52 +08:00
return _react2 [ "default" ] . createElement ( Table , _extends ( { } , this . props , {
columns : this . getDefaultColumns ( columns ) ,
data : data ,
onRowClick : this . onRowClick ,
expandIconColumnIndex : expandIconColumnIndex ? expandIconColumnIndex + 1 : 1
} ) ) ;
2018-04-02 14:22:39 +08:00
} ;
2019-03-20 12:38:43 +08:00
return MultiSelect ;
2019-09-02 17:53:53 +08:00
} ( _react . Component ) , _class . propTypes = {
2019-09-03 09:34:11 +08:00
autoCheckedByClickRows : _propTypes2 [ "default" ] . bool //行点击时,是否自动勾选复选框
2019-09-02 17:53:53 +08:00
} , _class . defaultProps = {
2019-03-13 17:43:56 +08:00
prefixCls : "u-table-mult-select" ,
2019-08-27 16:45:52 +08:00
getSelectedDataFunc : function getSelectedDataFunc ( ) { } ,
2019-09-02 17:53:53 +08:00
autoSelect : false ,
2019-09-09 17:30:16 +08:00
autoCheckedByClickRows : true ,
multiSelectConfig : { }
2018-11-16 14:51:40 +08:00
} , _initialiseProps = function _initialiseProps ( ) {
var _this2 = this ;
2018-04-02 14:22:39 +08:00
2019-08-27 16:45:52 +08:00
this . checkAllSelected = function ( data ) {
if ( ! _this2 . isArray ( data ) ) return false ;
if ( data . length == 0 ) return false ;
var count = 0 ;
var disabledCount = 0 ;
var length = 0 ;
var getTree = function getTree ( arr ) {
arr . forEach ( function ( item ) {
length ++ ;
if ( item . _checked && ! item . _disabled ) {
count ++ ;
} else if ( item . _disabled ) {
disabledCount ++ ;
}
if ( item . children ) {
getTree ( item . children ) ;
}
} ) ;
} ;
getTree ( data ) ;
if ( length == count + disabledCount && count > 0 ) {
return "all" ;
}
return count == 0 ? false : "indeter" ;
} ;
2018-04-02 14:22:39 +08:00
this . onAllCheckChange = function ( ) {
2018-11-16 14:51:40 +08:00
var _state = _this2 . state ,
data = _state . data ,
checkedAll = _state . checkedAll ,
indeterminate = _state . indeterminate ;
var check = false ;
if ( checkedAll ) {
check = false ;
2018-04-02 14:22:39 +08:00
} else {
2018-12-04 13:26:33 +08:00
check = true ;
2018-04-02 14:22:39 +08:00
}
2018-11-16 14:51:40 +08:00
var selectList = [ ] ;
2018-12-04 13:26:33 +08:00
2018-11-16 14:51:40 +08:00
data . forEach ( function ( item ) {
2019-08-27 16:45:52 +08:00
if ( item . children ) {
var res = _this2 . setTree ( item , check , true ) ;
selectList = selectList . concat ( res ) ;
} else {
if ( ! item . _disabled ) {
item . _checked = check ;
}
2018-12-04 13:26:33 +08:00
2019-08-27 16:45:52 +08:00
if ( item . _checked ) {
selectList . push ( item ) ;
}
2018-11-16 14:51:40 +08:00
}
2018-04-02 14:22:39 +08:00
} ) ;
2018-12-04 13:26:33 +08:00
if ( selectList . length > 0 ) {
indeterminate = true ;
} else {
indeterminate = false ;
}
2018-11-16 14:51:40 +08:00
_this2 . setState ( {
2018-12-04 13:26:33 +08:00
indeterminate : indeterminate ,
2018-11-16 14:51:40 +08:00
checkedAll : check
} ) ;
2019-10-19 11:05:51 +08:00
_this2 . props . getSelectedDataFunc ( selectList , undefined , undefined , data ) ;
2018-04-02 14:22:39 +08:00
} ;
2019-08-27 16:45:52 +08:00
this . setTree = function ( node , flag , autoSelect ) {
var res = [ ] ;
var setTreeNodeFlag = function setTreeNodeFlag ( node , flag ) {
if ( ! node . _disabled ) {
node . _checked = flag ;
}
if ( flag ) {
res . push ( node ) ;
}
if ( node . children && autoSelect ) {
node . children . forEach ( function ( item ) {
setTreeNodeFlag ( item , flag ) ;
} ) ;
}
} ;
setTreeNodeFlag ( node , flag ) ;
return res ;
} ;
this . getTree = function ( node , key , value ) {
var res = [ ] ;
var getTreeNodeByFlag = function getTreeNodeByFlag ( node ) {
if ( node [ key ] === value ) {
res . push ( node ) ;
}
if ( node . children ) {
node . children . forEach ( function ( item ) {
getTreeNodeByFlag ( item ) ;
} ) ;
}
} ;
getTreeNodeByFlag ( node ) ;
return res ;
} ;
2018-04-02 14:22:39 +08:00
this . onCheckboxChange = function ( text , record , index ) {
2018-11-16 14:51:40 +08:00
return function ( ) {
var data = _this2 . state . data ;
var selectList = [ ] ;
2019-08-27 16:45:52 +08:00
// record._checked = record._checked?false:true;
var flag = record . _checked ? false : true ;
if ( record . children ) {
_this2 . setTree ( record , flag , _this2 . props . autoSelect ) ;
} else {
record . _checked = flag ;
}
2018-11-16 14:51:40 +08:00
var obj = _this2 . getCheckedOrIndeter ( data ) ;
_this2 . setState ( _extends ( {
data : data
} , obj ) ) ;
data . forEach ( function ( da ) {
2019-08-27 16:45:52 +08:00
if ( da . children ) {
selectList = selectList . concat ( _this2 . getTree ( da , '_checked' , true ) ) ;
} else if ( da . _checked ) {
2018-11-16 14:51:40 +08:00
selectList . push ( da ) ;
}
} ) ;
2019-10-19 11:05:51 +08:00
_this2 . props . getSelectedDataFunc ( selectList , record , index , data ) ;
2018-11-16 14:51:40 +08:00
} ;
2018-04-02 14:22:39 +08:00
} ;
2018-04-11 14:34:18 +08:00
2018-11-16 14:51:40 +08:00
this . getDefaultColumns = function ( columns ) {
2019-09-09 17:30:16 +08:00
var multiSelectConfig = _this2 . props . multiSelectConfig ;
2018-11-16 14:51:40 +08:00
var _state2 = _this2 . state ,
checkedAll = _state2 . checkedAll ,
indeterminate = _state2 . indeterminate ;
var checkAttr = { checked : checkedAll ? true : false } ;
2018-12-04 13:26:33 +08:00
var data = _this2 . props . data ;
var dataLength = data . length ;
var disabledCount = 0 ;
2018-11-16 14:51:40 +08:00
indeterminate ? checkAttr . indeterminate = true : "" ;
2018-12-04 13:26:33 +08:00
//设置 表头Checkbox是否可以点击
data . forEach ( function ( item , index , arr ) {
if ( item . _disabled ) {
disabledCount ++ ;
}
} ) ;
2018-11-16 14:51:40 +08:00
var _defaultColumns = [ {
2019-07-03 21:12:17 +08:00
className : 'u-table-multiSelect-column' ,
2018-11-16 14:51:40 +08:00
title : _react2 [ "default" ] . createElement ( Checkbox , _extends ( {
2019-09-03 09:34:11 +08:00
className : 'table-checkbox'
2019-09-09 17:30:16 +08:00
} , checkAttr , multiSelectConfig , {
2018-12-04 13:26:33 +08:00
disabled : disabledCount == dataLength ? true : false ,
2018-11-16 14:51:40 +08:00
onChange : _this2 . onAllCheckChange
} ) ) ,
key : "checkbox" ,
dataIndex : "checkbox" ,
fixed : "left" ,
2019-05-08 17:29:40 +08:00
width : 49 ,
2018-11-16 14:51:40 +08:00
render : function render ( text , record , index ) {
var attr = { } ;
record . _disabled ? attr . disabled = record . _disabled : "" ;
return _react2 [ "default" ] . createElement ( Checkbox , _extends ( {
key : index ,
2019-09-03 09:34:11 +08:00
className : 'table-checkbox'
2019-09-09 17:30:16 +08:00
} , attr , multiSelectConfig , {
2018-11-16 14:51:40 +08:00
checked : record . _checked ,
onChange : _this2 . onCheckboxChange ( text , record , index )
} ) ) ;
}
} ] ;
return _defaultColumns . concat ( columns ) ;
2018-04-11 14:34:18 +08:00
} ;
2019-08-23 13:48:27 +08:00
this . onRowClick = function ( record , index , event ) {
2019-09-09 10:46:35 +08:00
if ( record . _disabled ) return ;
2019-09-02 17:53:53 +08:00
var _props2 = _this2 . props ,
autoCheckedByClickRows = _props2 . autoCheckedByClickRows ,
onRowClick = _props2 . onRowClick ;
if ( autoCheckedByClickRows ) {
_this2 . onCheckboxChange ( '' , record , index ) ( ) ;
2019-08-23 13:48:27 +08:00
}
2019-09-02 17:53:53 +08:00
onRowClick && onRowClick ( record , index , event ) ;
2019-08-23 13:48:27 +08:00
} ;
2018-04-02 14:22:39 +08:00
} , _temp ;
2017-11-29 16:51:07 +08:00
}
2019-09-03 09:34:11 +08:00
module . exports = exports [ 'default' ] ;