2017-10-12 16:54:17 +08:00
"use strict" ;
2017-09-25 15:36:23 +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" ] = sort ;
2017-10-12 16:54:17 +08:00
var _react = require ( "react" ) ;
2017-09-25 15:36:23 +08:00
var _react2 = _interopRequireDefault ( _react ) ;
2017-10-12 16:54:17 +08:00
var _beeIcon = require ( "bee-icon" ) ;
2017-09-25 15:36:23 +08:00
var _beeIcon2 = _interopRequireDefault ( _beeIcon ) ;
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 ) ; }
/ * *
* 参数 : prefixCls , 默认bee - table , 用于设置图标的样式
* @ param { * } Table
* /
function sort ( Table ) {
return function ( _Component ) {
_inherits ( Demo11 , _Component ) ;
function Demo11 ( props ) {
_classCallCheck ( this , Demo11 ) ;
var _this = _possibleConstructorReturn ( this , _Component . call ( this , props ) ) ;
_this . toggleSortOrder = function ( order , column ) {
var _this$state = _this . state ,
sortOrder = _this$state . sortOrder ,
data = _this$state . data ,
oldData = _this$state . oldData ;
var ascend _sort = function ascend _sort ( key ) {
return function ( a , b ) {
return a . key - b . key ;
} ;
} ;
var descend _sort = function descend _sort ( key ) {
return function ( a , b ) {
return b . key - a . key ;
} ;
} ;
if ( sortOrder === order ) {
// 切换为未排序状态
order = "" ;
}
if ( ! oldData ) {
oldData = data . concat ( ) ;
}
if ( order === "ascend" ) {
data = data . sort ( function ( a , b ) {
return column . sorter ( a , b ) ;
} ) ;
} else if ( order === "descend" ) {
data = data . sort ( function ( a , b ) {
return column . sorter ( b , a ) ;
} ) ;
} else {
data = oldData . concat ( ) ;
}
_this . setState ( {
sortOrder : order ,
data : data ,
oldData : oldData
} ) ;
} ;
_this . state = {
sortOrder : "" ,
data : _this . props . data
} ;
return _this ;
}
Demo11 . prototype . renderColumnsDropdown = function renderColumnsDropdown ( columns ) {
var _this2 = this ;
var sortOrder = this . state . sortOrder ;
2017-10-12 16:54:17 +08:00
var prefixCls = this . props . prefixCls || "bee-table" ;
2017-09-25 15:36:23 +08:00
return columns . map ( function ( originColumn ) {
var column = _extends ( { } , originColumn ) ;
var sortButton = void 0 ;
if ( column . sorter ) {
var isAscend = sortOrder === "ascend" ;
var isDescend = sortOrder === "descend" ;
sortButton = _react2 [ "default" ] . createElement (
2017-10-12 16:54:17 +08:00
"div" ,
{ className : prefixCls + "-column-sorter" } ,
2017-09-25 15:36:23 +08:00
_react2 [ "default" ] . createElement (
2017-10-12 16:54:17 +08:00
"span" ,
2017-09-25 15:36:23 +08:00
{
2017-10-12 16:54:17 +08:00
className : prefixCls + "-column-sorter-up " + ( isAscend ? "on" : "off" ) ,
title : "\u2191" ,
2017-09-25 15:36:23 +08:00
onClick : function onClick ( ) {
return _this2 . toggleSortOrder ( "ascend" , column ) ;
}
} ,
2017-10-12 16:54:17 +08:00
_react2 [ "default" ] . createElement ( _beeIcon2 [ "default" ] , { type : "uf-triangle-up" } )
2017-09-25 15:36:23 +08:00
) ,
_react2 [ "default" ] . createElement (
2017-10-12 16:54:17 +08:00
"span" ,
2017-09-25 15:36:23 +08:00
{
2017-10-12 16:54:17 +08:00
className : prefixCls + "-column-sorter-down " + ( isDescend ? "on" : "off" ) ,
title : "\u2193" ,
2017-09-25 15:36:23 +08:00
onClick : function onClick ( ) {
return _this2 . toggleSortOrder ( "descend" , column ) ;
}
} ,
2017-10-12 16:54:17 +08:00
_react2 [ "default" ] . createElement ( _beeIcon2 [ "default" ] , { type : "uf-triangle-down" } )
2017-09-25 15:36:23 +08:00
)
) ;
}
column . title = _react2 [ "default" ] . createElement (
2017-10-12 16:54:17 +08:00
"span" ,
2017-09-25 15:36:23 +08:00
null ,
column . title ,
sortButton
) ;
return column ;
} ) ;
} ;
Demo11 . prototype . render = function render ( ) {
2017-10-12 16:54:17 +08:00
var columns = this . renderColumnsDropdown ( this . props . columns . concat ( ) ) ;
2017-09-25 15:36:23 +08:00
return _react2 [ "default" ] . createElement ( Table , _extends ( { } , this . props , { columns : columns , data : this . state . data } ) ) ;
} ;
return Demo11 ;
} ( _react . Component ) ;
}
2017-10-12 16:54:17 +08:00
module . exports = exports [ "default" ] ;