UNPKG

wix-style-react

Version:
122 lines (106 loc) 5.24 kB
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _class, _temp; function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { 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) : subClass.__proto__ = superClass; } import React from 'react'; import DataTable from 'wix-style-react/DataTable'; import { any, string } from 'prop-types'; import './Example.scss'; var baseData = [{ firstName: 'Meghan', lastName: 'Bishop' }, { firstName: 'Sara', lastName: 'Porter' }, { firstName: 'Deborah', lastName: 'Rhodes' }, { firstName: 'Walter', lastName: 'Jenning' }, { firstName: 'Amanda', lastName: 'Woods' }]; var DataTableSortableExample = (_temp = _class = function (_React$Component) { _inherits(DataTableSortableExample, _React$Component); function DataTableSortableExample(props) { _classCallCheck(this, DataTableSortableExample); var _this = _possibleConstructorReturn(this, (DataTableSortableExample.__proto__ || Object.getPrototypeOf(DataTableSortableExample)).call(this, props)); _this.state = { sort: {}, data: baseData }; return _this; } _createClass(DataTableSortableExample, [{ key: 'handleSortClick', value: function handleSortClick(colNum) { var desc = !this.state.sort[colNum]; var sort = Object.assign({}, this.state.sort, _defineProperty({}, colNum, desc)); var filelds = { 1: 'firstName', 2: 'lastName' }; var sortedData = this.sortDataByField(filelds[colNum], desc); this.setState({ sort: sort, data: sortedData }); } }, { key: 'sortDataByField', value: function sortDataByField(field, desc) { return this.state.data.sort(function (a, b) { return desc ? ~~(field ? a[field] < b[field] : a < b) : ~~(field ? a[field] > b[field] : a > b); }); // eslint-disable-line } }, { key: 'render', value: function render() { var _this2 = this; return React.createElement( 'div', { style: this.props.style }, React.createElement(DataTable, { dataHook: this.props.dataHook, data: this.state.data, onSortClick: function onSortClick(col, colNum) { return _this2.handleSortClick(colNum); }, itemsPerPage: 20, newDesign: true, columns: [{ title: 'Row Number', render: function render(row, rowNum) { return '#' + (rowNum + 1); }, width: '20%', minWidth: '75px', important: true, infoTooltipProps: { content: 'Very informative tooltip text' } }, { title: 'First Name', sortable: true, sortDescending: !!this.state.sort[1], render: function render(row) { return React.createElement( 'span', null, row.firstName ); }, width: '40%', minWidth: '100px' }, { title: 'Last Name', sortable: true, sortDescending: !!this.state.sort[2], render: function render(row) { return React.createElement( 'span', null, row.lastName ); }, width: '40%', minWidth: '100px', infoTooltipProps: { content: 'Very informative tooltip text' } }], showLastRowDivider: false }) ); } }]); return DataTableSortableExample; }(React.Component), _class.propTypes = { style: any, dataHook: string }, _class.defaultProps = { style: { width: '966px' }, dataHook: 'story-data-table-sortable' }, _temp); export default DataTableSortableExample;