From 7d28d461859eb57944b99611fb8e9e4cc58d38bb Mon Sep 17 00:00:00 2001 From: AllenFang Date: Sat, 7 Sep 2019 16:27:18 +0800 Subject: [PATCH] enhance footer event --- docs/columns.md | 2 +- .../examples/footer/column-event-table.js | 4 +- .../react-bootstrap-table2/src/footer-cell.js | 95 ++++++++++--------- 3 files changed, 53 insertions(+), 48 deletions(-) diff --git a/docs/columns.md b/docs/columns.md index 8770eb6..6d77335 100644 --- a/docs/columns.md +++ b/docs/columns.md @@ -666,7 +666,7 @@ It's also available to custom via a callback function: { // omit... footerEvents: { - onClick: e => { ... } + onClick: (e, column, columnIndex) => { ... } } } ``` diff --git a/packages/react-bootstrap-table2-example/examples/footer/column-event-table.js b/packages/react-bootstrap-table2-example/examples/footer/column-event-table.js index 6837eee..943c680 100644 --- a/packages/react-bootstrap-table2-example/examples/footer/column-event-table.js +++ b/packages/react-bootstrap-table2-example/examples/footer/column-event-table.js @@ -12,7 +12,7 @@ const columns = [{ dataField: 'id', text: 'Product ID', footerEvents: { - onClick: () => alert('Click on Product ID footer column') + onClick: (e, column, columnIndex) => alert(`Click on Product ID header column, columnIndex: ${columnIndex}`) }, footer: 'Footer 1' }, { @@ -32,7 +32,7 @@ const columns = [{ dataField: 'id', text: 'Product ID', footerEvents: { - onClick: () => alert('Click on Product ID footer column') + onClick: (e, column, columnIndex) => alert('Click on Product ID footer column') }, footer: 'Footer 1' }, { diff --git a/packages/react-bootstrap-table2/src/footer-cell.js b/packages/react-bootstrap-table2/src/footer-cell.js index 2995f4a..e47ad0a 100644 --- a/packages/react-bootstrap-table2/src/footer-cell.js +++ b/packages/react-bootstrap-table2/src/footer-cell.js @@ -4,56 +4,61 @@ import cs from 'classnames'; import PropTypes from 'prop-types'; import _ from './utils'; +import eventDelegater from './cell-event-delegater'; -const FooterCell = (props) => { - const { index, column, columnData } = props; +class FooterCell extends eventDelegater(React.Component) { + render() { + const { index, column, columnData } = this.props; - const { - footer, - footerTitle, - footerAlign, - footerFormatter, - footerEvents, - footerClasses, - footerStyle, - footerAttrs - } = column; + const { + footer, + footerTitle, + footerAlign, + footerFormatter, + footerEvents, + footerClasses, + footerStyle, + footerAttrs + } = column; - const cellAttrs = { - ...(_.isFunction(footerAttrs) ? footerAttrs(column, index) : footerAttrs), - ...footerEvents - }; + const delegateEvents = this.delegate(footerEvents); + const cellAttrs = { + ...(_.isFunction(footerAttrs) ? footerAttrs(column, index) : footerAttrs), + ...delegateEvents + }; - let text = ''; - if (_.isString(footer)) { - text = footer; - } else if (_.isFunction(footer)) { - text = footer(columnData, column, index); + + let text = ''; + if (_.isString(footer)) { + text = footer; + } else if (_.isFunction(footer)) { + text = footer(columnData, column, index); + } + + let cellStyle = {}; + const cellClasses = _.isFunction(footerClasses) ? footerClasses(column, index) : footerClasses; + + if (footerStyle) { + cellStyle = _.isFunction(footerStyle) ? footerStyle(column, index) : footerStyle; + cellStyle = cellStyle ? { ...cellStyle } : cellStyle; + } + + if (footerTitle) { + cellAttrs.title = _.isFunction(footerTitle) ? footerTitle(column, index) : text; + } + + if (footerAlign) { + cellStyle.textAlign = _.isFunction(footerAlign) ? footerAlign(column, index) : footerAlign; + } + + if (cellClasses) cellAttrs.className = cs(cellAttrs.className, cellClasses); + if (!_.isEmptyObject(cellStyle)) cellAttrs.style = cellStyle; + + const children = footerFormatter ? footerFormatter(column, index) : text; + + return React.createElement('th', cellAttrs, children); } - - let cellStyle = {}; - const cellClasses = _.isFunction(footerClasses) ? footerClasses(column, index) : footerClasses; - - if (footerStyle) { - cellStyle = _.isFunction(footerStyle) ? footerStyle(column, index) : footerStyle; - cellStyle = cellStyle ? { ...cellStyle } : cellStyle; - } - - if (footerTitle) { - cellAttrs.title = _.isFunction(footerTitle) ? footerTitle(column, index) : text; - } - - if (footerAlign) { - cellStyle.textAlign = _.isFunction(footerAlign) ? footerAlign(column, index) : footerAlign; - } - - if (cellClasses) cellAttrs.className = cs(cellAttrs.className, cellClasses); - if (!_.isEmptyObject(cellStyle)) cellAttrs.style = cellStyle; - - const children = footerFormatter ? footerFormatter(column, index) : text; - - return React.createElement('th', cellAttrs, children); -}; +} FooterCell.propTypes = { columnData: PropTypes.array,