Skip to content

Commit

Permalink
Reuse default TableHeadingCellContainer for local plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlbyk committed Apr 29, 2018
1 parent c48a179 commit 6a42e3b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 73 deletions.
75 changes: 3 additions & 72 deletions src/plugins/local/components/TableHeadingCellContainer.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from '../../../utils/griddleConnect';
import compose from 'recompose/compose';
import mapProps from 'recompose/mapProps';
import getContext from 'recompose/getContext';
import withHandlers from 'recompose/withHandlers';
import { sortPropertyByIdSelector, iconsForComponentSelector, customHeadingComponentSelector, stylesForComponentSelector, classNamesForComponentSelector, cellPropertiesSelector } from '../../../selectors/dataSelectors';
import { setSortColumn } from '../../../actions';
import { combineHandlers } from '../../../utils/compositionUtils';
import { getSortIconProps, setSortProperties } from '../../../utils/sortUtils';
import { valueOrResult } from '../../../utils/valueUtils';
import TableHeadingCellContainer from '../../../components/TableHeadingCellContainer';

const DefaultTableHeadingCellContent = ({title, icon, iconClassName}) => (
<span>
{ title }
{ icon && <span className={iconClassName}>{icon}</span> }
</span>
)

const EnhancedHeadingCell = OriginalComponent => compose(
getContext({
events: PropTypes.object,
selectors: PropTypes.object,
}),
connect(
(state, props) => ({
sortProperty: sortPropertyByIdSelector(state, props),
customHeadingComponent: customHeadingComponentSelector(state, props),
cellProperties: cellPropertiesSelector(state, props),
className: classNamesForComponentSelector(state, 'TableHeadingCell'),
sortAscendingClassName: classNamesForComponentSelector(state, 'TableHeadingCellAscending'),
sortDescendingClassName: classNamesForComponentSelector(state, 'TableHeadingCellDescending'),
style: stylesForComponentSelector(state, 'TableHeadingCell'),
...iconsForComponentSelector(state, 'TableHeadingCell'),
}),
(dispatch, { events: { onSort } }) => ({
setSortColumn: combineHandlers([
onSort,
compose(dispatch, setSortColumn),
]),
})
),
withHandlers(props => ({
onClick: props.cellProperties.sortable === false ? (() => () => {}) :
props.events.setSortProperties || setSortProperties,
})),
//TODO: use with props on change or something more performant here
mapProps(props => {
const iconProps = getSortIconProps(props);
const title = props.customHeadingComponent ?
<props.customHeadingComponent {...props.cellProperties.extraData} {...props} {...iconProps} /> :
<DefaultTableHeadingCellContent title={props.title} {...iconProps} />;
const className = valueOrResult(props.cellProperties.headerCssClassName, props) || props.className;
const style = {
...(props.cellProperties.sortable === false || { cursor: 'pointer' }),
...props.style,
};

return {
...props.cellProperties.extraData,
...props,
...iconProps,
title,
style,
className
};
})
)(props =>
<OriginalComponent
{...props}
/>
);
// Obsolete
const EnhancedHeadingCell = TableHeadingCellContainer;

export default EnhancedHeadingCell;
2 changes: 1 addition & 1 deletion src/plugins/local/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ export default {
PreviousButtonContainer,
PageDropdownContainer,
TableContainer,
TableHeadingCellContainer,
TableHeadingCellContainer, // TODO: Obsolete; remove
};

0 comments on commit 6a42e3b

Please sign in to comment.