diff --git a/docs/columns.md b/docs/columns.md index afeffe752..5bb23a657 100644 --- a/docs/columns.md +++ b/docs/columns.md @@ -13,6 +13,7 @@ Available properties in a column object: * [formatExtraData](#formatExtraData) * [sort](#sort) * [sortFunc](#sortFunc) +* [sortCaret](#sortCaret) * [onSort](#onSort) * [classes](#classes) * [style](#style) @@ -154,6 +155,20 @@ Enable the column sort via a `true` value given. } ``` +## column.sortCaret - [Function] +Use`column.sortCaret` to custom the sort caret. This callback function accept two arguments: `order` and `column` + +```js +{ + // omit... + sort: true, + sortCaret: (order, column) => { + return //... + } +} +``` +> The possible value of `order` argument is **`asc`**, **`desc`** and **`undefined`**. + ## column.classes - [String | Function] It's available to have custom class on table column: diff --git a/packages/react-bootstrap-table2-example/examples/sort/custom-sort-caret.js b/packages/react-bootstrap-table2-example/examples/sort/custom-sort-caret.js new file mode 100644 index 000000000..4a1a650cb --- /dev/null +++ b/packages/react-bootstrap-table2-example/examples/sort/custom-sort-caret.js @@ -0,0 +1,59 @@ +/* eslint no-unused-vars: 0 */ +import React from 'react'; + +import BootstrapTable from 'react-bootstrap-table-next'; +import Code from 'components/common/code-block'; +import { productsGenerator } from 'utils/common'; + +const products = productsGenerator(); + +const columns = [{ + dataField: 'id', + text: 'Product ID', + sort: true +}, { + dataField: 'name', + text: 'Product Name', + sort: true, + sortCaret: (order, column) => { + if (!order) return (  Desc/Asc); + else if (order === 'asc') return (  Desc/Asc); + else if (order === 'desc') return (  Desc/Asc); + return null; + } +}, { + dataField: 'price', + text: 'Product Price' +}]; + +const sourceCode = `\ +import BootstrapTable from 'react-bootstrap-table-next'; + +const columns = [{ + dataField: 'id', + text: 'Product ID', + sort: true +}, { + dataField: 'name', + text: 'Product Name', + sort: true, + sortCaret: (order, column) => { + if (!order) return (  Desc/Asc); + else if (order === 'asc') return (  Desc/Asc); + else if (order === 'desc') return (  Desc/Asc); + return null; + } +}, { + dataField: 'price', + text: 'Product Price' +}]; + + +`; + +export default () => ( +
+ + { sourceCode } +
+); diff --git a/packages/react-bootstrap-table2-example/stories/index.js b/packages/react-bootstrap-table2-example/stories/index.js index d254bb2eb..2941f37f3 100644 --- a/packages/react-bootstrap-table2-example/stories/index.js +++ b/packages/react-bootstrap-table2-example/stories/index.js @@ -84,6 +84,7 @@ import DefaultSortTable from 'examples/sort/default-sort-table'; import DefaultSortDirectionTable from 'examples/sort/default-sort-direction'; import SortEvents from 'examples/sort/sort-events'; import CustomSortTable from 'examples/sort/custom-sort-table'; +import CustomSortCaretTable from 'examples/sort/custom-sort-caret'; import HeaderSortingClassesTable from 'examples/sort/header-sorting-classes'; import HeaderSortingStyleTable from 'examples/sort/header-sorting-style'; @@ -269,6 +270,7 @@ storiesOf('Sort Table', module) .add('Default Sort Direction Table', () => ) .add('Sort Events', () => ) .add('Custom Sort Fuction', () => ) + .add('Custom Sort Caret', () => ) .add('Custom Classes on Sorting Header Column', () => ) .add('Custom Style on Sorting Header Column', () => ); diff --git a/packages/react-bootstrap-table2/src/header-cell.js b/packages/react-bootstrap-table2/src/header-cell.js index 79b4b9b61..12700821b 100644 --- a/packages/react-bootstrap-table2/src/header-cell.js +++ b/packages/react-bootstrap-table2/src/header-cell.js @@ -24,6 +24,7 @@ const HeaderCell = (props) => { const { text, sort, + sortCaret, filter, filterRenderer, headerTitle, @@ -69,7 +70,7 @@ const HeaderCell = (props) => { cellAttrs.className = cs(cellAttrs.className, 'sortable'); if (sorting) { - sortSymbol = ; + sortSymbol = sortCaret ? sortCaret(sortOrder, column) : ; // append customized classes or style if table was sorting based on the current column. cellClasses = cs( @@ -86,7 +87,7 @@ const HeaderCell = (props) => { : headerSortingStyle }; } else { - sortSymbol = ; + sortSymbol = sortCaret ? sortCaret(undefined, column) : ; } } @@ -151,6 +152,7 @@ HeaderCell.propTypes = { onSort: PropTypes.func, sorting: PropTypes.bool, sortOrder: PropTypes.oneOf([Const.SORT_ASC, Const.SORT_DESC]), + sortCaret: PropTypes.func, isLastSorting: PropTypes.bool, onFilter: PropTypes.func, onExternalFilter: PropTypes.func diff --git a/packages/react-bootstrap-table2/test/header-cell.test.js b/packages/react-bootstrap-table2/test/header-cell.test.js index 681413567..b0c45254d 100644 --- a/packages/react-bootstrap-table2/test/header-cell.test.js +++ b/packages/react-bootstrap-table2/test/header-cell.test.js @@ -403,6 +403,24 @@ describe('HeaderCell', () => { it('header should render SortSymbol as default', () => { expect(wrapper.find(SortSymbol).length).toBe(1); }); + + describe('when sortCaret is defined ', () => { + beforeEach(() => { + column = { ...column, sortCaret: jest.fn() }; + wrapper = shallow( + + ); + }); + + it('header should not render SortSymbol', () => { + expect(wrapper.find(SortSymbol).length).toBe(0); + }); + + it('should call column.sortCaret correctly', () => { + expect(column.sortCaret).toHaveBeenCalledTimes(1); + expect(column.sortCaret).toHaveBeenCalledWith(undefined, column); + }); + }); }); describe('and sorting prop is true', () => { @@ -420,6 +438,30 @@ describe('HeaderCell', () => { }); }); + describe('when sortCaret is defined ', () => { + beforeEach(() => { + column = { ...column, sortCaret: jest.fn() }; + wrapper = shallow( + + ); + }); + + it('header should not render SortSymbol', () => { + expect(wrapper.find(SortSymbol).length).toBe(0); + }); + + it('should call column.sortCaret correctly', () => { + expect(column.sortCaret).toHaveBeenCalledTimes(1); + expect(column.sortCaret).toHaveBeenCalledWith(Const.SORT_ASC, column); + }); + }); + describe('when headerSortingClasses is defined ', () => { const classes = 'foo'; const order = Const.SORT_DESC;