2019-12-17 20:44:55 +08:00
'use strict' ;
2019-05-20 15:50:13 +08:00
Object . defineProperty ( exports , "__esModule" , {
value : true
} ) ;
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 ; } ;
exports [ "default" ] = singleSelect ;
2019-12-17 20:44:55 +08:00
var _react = require ( 'react' ) ;
2019-05-20 15:50:13 +08:00
var _react2 = _interopRequireDefault ( _react ) ;
2019-12-17 20:44:55 +08:00
var _propTypes = require ( 'prop-types' ) ;
var _propTypes2 = _interopRequireDefault ( _propTypes ) ;
var _util = require ( './util' ) ;
2019-05-20 15:50:13 +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 ) ; }
/ * *
* 参数 : 单选表头
* @ param { * } Table
* @ param { * } Radio
* /
function singleSelect ( Table , Radio ) {
var _class , _temp ;
return _temp = _class = function ( _Component ) {
_inherits ( SingleSelect , _Component ) ;
function SingleSelect ( props ) {
_classCallCheck ( this , SingleSelect ) ;
var _this = _possibleConstructorReturn ( this , _Component . call ( this , props ) ) ;
2019-12-13 14:13:13 +08:00
_this . handleRadioClick = function ( e ) {
e . stopPropagation ( ) ;
} ;
2019-05-20 15:50:13 +08:00
_this . onRadioChange = function ( value , record , index ) {
2019-06-10 16:46:36 +08:00
var selectedRowIndex = _this . state . selectedRowIndex ;
if ( selectedRowIndex === index ) {
_this . setState ( { selectedRowIndex : '' } ) ;
_this . props . getSelectedDataFunc ( ) ;
} else {
_this . setState ( { selectedRowIndex : index } ) ;
_this . props . getSelectedDataFunc ( record , index ) ;
}
2019-05-20 15:50:13 +08:00
} ;
_this . getDefaultColumns = function ( columns ) {
var selectedRowIndex = _this . state . selectedRowIndex ;
var _defaultColumns = [ {
title : '' ,
key : "radio" ,
dataIndex : "radio" ,
fixed : "left" ,
width : 49 ,
render : function render ( text , record , index ) {
return _react2 [ "default" ] . createElement (
Radio . RadioGroup ,
{
2019-12-17 20:44:55 +08:00
className : 'table-radio' ,
name : 'table-radio' ,
2019-05-20 15:50:13 +08:00
selectedValue : selectedRowIndex ,
2019-12-13 14:13:13 +08:00
onClick : _this . handleRadioClick ,
2019-05-20 15:50:13 +08:00
onChange : function onChange ( value ) {
return _this . onRadioChange ( value , record , index ) ;
} ,
2019-07-01 16:22:24 +08:00
style : { width : '14px' , height : '14px' , display : 'block' , marginLeft : '4px' } } ,
2019-05-20 15:50:13 +08:00
_react2 [ "default" ] . createElement ( Radio , { value : index } )
) ;
}
} ] ;
return _defaultColumns . concat ( columns ) ;
} ;
2019-12-13 14:13:13 +08:00
_this . onRowClick = function ( record , index , event ) {
var _this$props = _this . props ,
autoCheckedByClickRows = _this$props . autoCheckedByClickRows ,
onRowClick = _this$props . onRowClick ;
if ( autoCheckedByClickRows ) {
_this . onRadioChange ( '' , record , index ) ;
}
onRowClick && onRowClick ( record , index , event ) ;
} ;
2019-12-24 16:45:37 +08:00
_this . getRowClassName = function ( record , index , event ) {
var selectedRowIndex = _this . state . selectedRowIndex ;
if ( index === selectedRowIndex ) {
return 'selected' ;
} else {
return '' ;
}
} ;
2019-05-20 15:50:13 +08:00
_this . state = {
data : ( 0 , _util . ObjectAssign ) ( props . data ) ,
selectedRowIndex : props . selectedRowIndex
} ;
return _this ;
}
SingleSelect . prototype . componentWillReceiveProps = function componentWillReceiveProps ( nextProps ) {
if ( 'data' in nextProps ) {
this . setState ( {
data : ( 0 , _util . ObjectAssign ) ( nextProps . data )
} ) ;
}
if ( 'selectedRowIndex' in nextProps ) {
this . setState ( {
selectedRowIndex : nextProps . selectedRowIndex
} ) ;
}
} ;
/ * *
* 判断是否是数组
* @ param { * } o
* /
SingleSelect . prototype . isArray = function isArray ( o ) {
return Object . prototype . toString . call ( o ) == '[object Array]' ;
} ;
2019-12-13 14:13:13 +08:00
// 实现行点击时触发单选框勾选的需求
2019-05-20 15:50:13 +08:00
SingleSelect . prototype . render = function render ( ) {
var columns = this . props . columns ;
var data = this . state . data ;
return _react2 [ "default" ] . createElement ( Table , _extends ( { } , this . props , {
columns : this . getDefaultColumns ( columns ) ,
2019-12-13 14:13:13 +08:00
data : data ,
2019-12-24 16:45:37 +08:00
onRowClick : this . onRowClick ,
rowClassName : this . getRowClassName
} ) ) ;
2019-05-20 15:50:13 +08:00
} ;
return SingleSelect ;
2019-12-13 14:13:13 +08:00
} ( _react . Component ) , _class . propTypes = {
2019-12-17 20:44:55 +08:00
autoCheckedByClickRows : _propTypes2 [ "default" ] . bool //行点击时,是否自动勾选单选框
2019-12-13 14:13:13 +08:00
} , _class . defaultProps = {
2019-05-20 15:50:13 +08:00
prefixCls : "u-table-single-select" ,
getSelectedDataFunc : function getSelectedDataFunc ( ) { } ,
2019-12-13 14:13:13 +08:00
selectedRowIndex : '' ,
autoCheckedByClickRows : true
2019-05-20 15:50:13 +08:00
} , _temp ;
}
2019-12-17 20:44:55 +08:00
module . exports = exports [ 'default' ] ;