/** @format */ /** * External dependencies */ import { __, sprintf } from '@wordpress/i18n'; import { Component, createRef, Fragment } from '@wordpress/element'; import classnames from 'classnames'; import { IconButton } from '@wordpress/components'; import { find, get, isEqual, noop, uniqueId } from 'lodash'; import Gridicon from 'gridicons'; import PropTypes from 'prop-types'; const ASC = 'asc'; const DESC = 'desc'; const getDisplay = cell => cell.display || null; class Table extends Component { constructor( props ) { super( props ); this.state = { tabIndex: null, rows: props.rows || [], }; this.container = createRef(); this.sortBy = this.sortBy.bind( this ); this.headersID = uniqueId( 'header-' ); this.captionID = uniqueId( 'caption-' ); } componentDidUpdate( prevProps ) { if ( ! isEqual( this.props.rows, prevProps.rows ) ) { /* eslint-disable react/no-did-update-set-state */ this.setState( { rows: this.props.rows, } ); /* eslint-enable react/no-did-update-set-state */ } } componentDidMount() { const { scrollWidth, clientWidth } = this.container.current; const scrollable = scrollWidth > clientWidth; /* eslint-disable react/no-did-mount-set-state */ this.setState( { tabIndex: scrollable ? '0' : null, } ); /* eslint-enable react/no-did-mount-set-state */ } sortBy( key ) { const { headers, query } = this.props; return () => { const currentKey = query.orderby || get( find( headers, { defaultSort: true } ), 'key', false ); const currentDir = query.order || DESC; let dir = DESC; if ( key === currentKey ) { dir = DESC === currentDir ? ASC : DESC; } this.props.onSort( key, dir ); }; } render() { const { caption, classNames, headers, query, rowHeader } = this.props; const { rows, tabIndex } = this.state; const classes = classnames( 'woocommerce-table__table', classNames ); const sortedBy = query.orderby || get( find( headers, { defaultSort: true } ), 'key', false ); const sortDir = query.order || DESC; return (
{ isSortable ? (
|
);
} ) }
|
---|---|
{ getDisplay( cell ) } | ) : ({ getDisplay( cell ) } | ) ) }