UNPKG

chowa

Version:

UI component library based on React

66 lines (65 loc) 2.62 kB
/** * @license chowa v1.1.3 * * Copyright (c) Chowa Techonlogies Co.,Ltd.(http://www.chowa.cn). * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const React = require("react"); const classnames_1 = require("classnames"); const utils_1 = require("../utils"); class TableSorter extends React.PureComponent { constructor(props) { super(props); this.doSort = this.doSort.bind(this); } componentDidUpdate(preProps) { if (this.props.sorted && preProps.sortMode !== this.props.sortMode) { this.doSort(this.props.sortMode); } } componentDidMount() { if (this.props.sorted && utils_1.isExist(this.props.sortMode)) { this.doSort(this.props.sortMode); } } doSort(mode) { const { dataIndex, activeSorter, updateTable, sortMethod } = this.props; const sorterInfo = utils_1.isExist(activeSorter) && activeSorter.dataIndex === dataIndex && activeSorter.mode === mode ? undefined : { dataIndex, mode }; if (sortMethod) { sortMethod(dataIndex, sorterInfo === undefined ? undefined : mode); } else { throw new Error('Table must provide a sorting method as sortMethod'); } updateTable({ sorterInfo }); } render() { const { activeSorter, dataIndex } = this.props; const ascBtnClass = classnames_1.default({ [utils_1.preClass('table-sort-btn')]: true, [utils_1.preClass('table-sort-asc')]: true, [utils_1.preClass('table-sorted')]: utils_1.isExist(activeSorter) && activeSorter.dataIndex === dataIndex && activeSorter.mode === 'asc' }); const descBtnClass = classnames_1.default({ [utils_1.preClass('table-sort-btn')]: true, [utils_1.preClass('table-sort-desc')]: true, [utils_1.preClass('table-sorted')]: utils_1.isExist(activeSorter) && activeSorter.dataIndex === dataIndex && activeSorter.mode === 'desc' }); return (React.createElement("div", { className: utils_1.preClass('table-sort') }, React.createElement("span", { className: ascBtnClass, onClick: this.doSort.bind(this, 'asc') }), React.createElement("span", { className: descBtnClass, onClick: this.doSort.bind(this, 'desc') }))); } } exports.default = TableSorter;