Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(react-grid): switch to column extensions in LocalSorting #603

Merged
merged 2 commits into from
Dec 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ const comparePriority = (a, b) => {
return (priorityA < priorityB) ? -1 : 1;
};

const getColumnCompare = columnName =>
(columnName === 'priority' ? comparePriority : undefined);

export default class Demo extends React.PureComponent {
constructor(props) {
super(props);
Expand All @@ -43,6 +40,9 @@ export default class Demo extends React.PureComponent {
{ name: 'dueDate', title: 'Due Date' },
{ name: 'priority', title: 'Priority' },
],
localSortingColumnExtensions: [
{ columnName: 'priority', compare: comparePriority },
],
tableColumnExtensions: [
{ columnName: 'subject', width: 300 },
],
Expand All @@ -53,7 +53,9 @@ export default class Demo extends React.PureComponent {
};
}
render() {
const { rows, columns, tableColumnExtensions } = this.state;
const {
rows, columns, localSortingColumnExtensions, tableColumnExtensions,
} = this.state;

return (
<Grid
Expand All @@ -62,7 +64,7 @@ export default class Demo extends React.PureComponent {
>
<SortingState />
<LocalSorting
getColumnCompare={getColumnCompare}
columnExtensions={localSortingColumnExtensions}
/>
<Table
columnExtensions={tableColumnExtensions}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ const comparePriority = (a, b) => {
return (priorityA < priorityB) ? -1 : 1;
};

const getColumnCompare = columnName =>
(columnName === 'priority' ? comparePriority : undefined);

export default class Demo extends React.PureComponent {
constructor(props) {
super(props);
Expand All @@ -43,6 +40,9 @@ export default class Demo extends React.PureComponent {
{ name: 'dueDate', title: 'Due Date' },
{ name: 'priority', title: 'Priority' },
],
localSortingColumnExtensions: [
{ columnName: 'priority', compare: comparePriority },
],
tableColumnExtensions: [
{ columnName: 'subject', width: 300 },
],
Expand All @@ -53,7 +53,9 @@ export default class Demo extends React.PureComponent {
};
}
render() {
const { rows, columns, tableColumnExtensions } = this.state;
const {
rows, columns, localSortingColumnExtensions, tableColumnExtensions,
} = this.state;

return (
<Paper>
Expand All @@ -63,7 +65,7 @@ export default class Demo extends React.PureComponent {
>
<SortingState />
<LocalSorting
getColumnCompare={getColumnCompare}
columnExtensions={localSortingColumnExtensions}
/>
<Table
columnExtensions={tableColumnExtensions}
Expand Down
2 changes: 1 addition & 1 deletion packages/dx-react-grid/docs/guides/sorting.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Note that the `LocalGrouping` plugin should follow `LocalSorting` to provide cor

## Custom Sorting Algorithm

The [LocalSorting](../reference/local-sorting.md) plugin's `getColumnCompare` property allows you to implement a custom sorting algorithm. If the `getColumnCompare` function returns undefined, it applies the default sorting algorithm.
The [LocalSorting](../reference/local-sorting.md) plugin's `columnExtensions` property allows you to implement a custom sorting algorithm for a specific column.

.embedded-demo(sorting/local-custom-sorting)

Expand Down
13 changes: 10 additions & 3 deletions packages/dx-react-grid/docs/reference/local-sorting.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ A plugin that performs local data sorting.

Name | Type | Default | Description
-----|------|---------|------------
getColumnCompare | (columnName: string) => [Compare](#compare) &#124; undefined | | A function implementing custom sorting. See the [Sorting guide](../guides/sorting.md#custom-sorting-algorithm) for more information.
columnExtensions | Array&lg;[LocalSortingColumnExtension](#localsortingcolumnextension)&gt; | | Additional column properties that the plugin can handle.

## Interfaces

### <a name="compare"></a>Compare
### LocalSortingColumnExtension

A function with the following signature `(a: any, b: any) => number`
Describes additional column properties that the plugin can handle.

A value with the following shape:

Field | Type | Description
------|------|------------
columnName | string | The name of a column to extend.
compare? | (a: any, b: any) => number | A sort compare function. See the [Sorting guide](../guides/sorting.md#custom-sorting-algorithm) for more information.

## Plugin Developer Reference

Expand Down
17 changes: 8 additions & 9 deletions packages/dx-react-grid/src/plugins/local-sorting.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import React from 'react';
import PropTypes from 'prop-types';
import { Getter, PluginContainer } from '@devexpress/dx-react-core';
import { sortedRows } from '@devexpress/dx-grid-core';
import { sortedRows, getColumnExtension } from '@devexpress/dx-grid-core';

const pluginDependencies = [
{ pluginName: 'SortingState' },
];

export class LocalSorting extends React.PureComponent {
render() {
const { getColumnCompare } = this.props;
const { columnExtensions } = this.props;
const getColumnCompare = columnName =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we change getColumnCompare to columnCompare?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left the getColumnCompare name in order to make it consistent across local processing plugins.

getColumnExtension(columnExtensions, columnName).compare;

const rowsComputed = ({
rows,
sorting,
getCellValue,
isGroupRow,
getRowLevelKey,
rows, sorting, getCellValue, isGroupRow, getRowLevelKey,
}) =>
sortedRows(rows, sorting, getCellValue, getColumnCompare, isGroupRow, getRowLevelKey);

Expand All @@ -31,9 +30,9 @@ export class LocalSorting extends React.PureComponent {
}

LocalSorting.propTypes = {
getColumnCompare: PropTypes.func,
columnExtensions: PropTypes.array,
};

LocalSorting.defaultProps = {
getColumnCompare: undefined,
columnExtensions: undefined,
};