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

Remove sortColumn/sortDirection states #1768

Merged
merged 1 commit into from
Oct 4, 2019
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
4 changes: 3 additions & 1 deletion examples/scripts/example08-sortable-cols.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Example extends React.Component {

const rows = sortDirection === 'NONE' ? this.state.originalRows.slice(0) : this.state.rows.sort(comparer);

this.setState({ rows });
this.setState({ rows, sortColumn, sortDirection });
};

rowGetter = (i) => {
Expand All @@ -101,6 +101,8 @@ class Example extends React.Component {
columns={this._columns}
rowGetter={this.rowGetter}
rowsCount={this.state.rows.length}
sortDirection={this.state.sortDirection}
sortColumn={this.state.sortColumn}
minHeight={500}
/>
);
Expand Down
2 changes: 2 additions & 0 deletions examples/scripts/example16-filterable-sortable-grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class Example extends React.Component {
return (
<ReactDataGrid
onGridSort={this.handleGridSort}
sortDirection={this.state.sortDirection}
sortColumn={this.state.sortColumn}
enableCellSelect
columns={this._columns}
rowGetter={this.rowGetter}
Expand Down
4 changes: 3 additions & 1 deletion examples/scripts/example29-descendingFirstSortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class Example extends React.Component {

const rows = sortDirection === 'NONE' ? this.state.originalRows.slice(0) : this.state.rows.sort(comparer);

this.setState({ rows });
this.setState({ rows, sortColumn, sortDirection });
};

rowGetter = (i) => {
Expand All @@ -102,6 +102,8 @@ class Example extends React.Component {
columns={this._columns}
rowGetter={this.rowGetter}
rowsCount={this.state.rows.length}
sortDirection={this.state.sortDirection}
sortColumn={this.state.sortColumn}
minHeight={500}
/>
);
Expand Down
11 changes: 5 additions & 6 deletions packages/react-data-grid/src/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type SharedDataGridProps<R> = Pick<ReactDataGridProps<R>,
| 'enableIsScrolling'
| 'selectedRows'
| 'onSelectedRowsChange'
| 'sortColumn'
| 'sortDirection'
> & Required<Pick<ReactDataGridProps<R>,
| 'rowKey'
| 'enableCellSelect'
Expand All @@ -35,19 +37,16 @@ type SharedDataGridProps<R> = Pick<ReactDataGridProps<R>,
| 'cellNavigationMode'
| 'enableCellAutoFocus'
| 'editorPortalTarget'
>> & {
sortColumn?: keyof R;
sortDirection?: DEFINE_SORT;
columnMetrics: ColumnMetrics<R>;
};
>>;

export interface GridProps<R> extends SharedDataGridProps<R> {
columnMetrics: ColumnMetrics<R>;
headerRows: [HeaderRowData<R>, HeaderRowData<R> | undefined];
cellMetaData: CellMetaData<R>;
rowOffsetHeight: number;
eventBus: EventBus;
interactionMasksMetaData: InteractionMasksMetaData<R>;
onSort(columnKey: keyof R, sortDirection: DEFINE_SORT): void;
onSort?(columnKey: keyof R, direction: DEFINE_SORT): void;
onViewportKeydown?(e: React.KeyboardEvent<HTMLDivElement>): void;
onViewportKeyup?(e: React.KeyboardEvent<HTMLDivElement>): void;
onColumnResize(idx: number, width: number): void;
Expand Down
16 changes: 3 additions & 13 deletions packages/react-data-grid/src/ReactDataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,6 @@ const ReactDataGridBase = forwardRef(function ReactDataGrid<R extends {}>({
...props
}: ReactDataGridProps<R>, ref: React.Ref<ReactDataGridHandle>) {
const [canFilter, setCanFilter] = useState(false);
const [sortColumn, setSortColumn] = useState(props.sortColumn);
const [sortDirection, setSortDirection] = useState(props.sortDirection);
const [columnWidths, setColumnWidths] = useState(() => new Map<keyof R, number>());
const [eventBus] = useState(() => new EventBus());
const [gridWidth, setGridWidth] = useState(0);
Expand Down Expand Up @@ -326,14 +324,6 @@ const ReactDataGridBase = forwardRef(function ReactDataGrid<R extends {}>({
handleGridRowsUpdated(commit.cellKey, targetRow, targetRow, commit.updated, UpdateActions.CELL_UPDATE);
}

function handleSort(sortColumn: keyof R, sortDirection: DEFINE_SORT) {
setSortColumn(sortColumn);
setSortDirection(sortDirection);
if (props.onGridSort) {
props.onGridSort(sortColumn, sortDirection);
}
}

function getHeaderRows(): [HeaderRowData<R>, HeaderRowData<R> | undefined] {
const { headerRowHeight, onAddFilter } = props;
return [
Expand Down Expand Up @@ -424,9 +414,9 @@ const ReactDataGridBase = forwardRef(function ReactDataGrid<R extends {}>({
selectedRows={selectedRows}
onSelectedRowsChange={onSelectedRowsChange}
rowOffsetHeight={rowOffsetHeight}
sortColumn={sortColumn}
sortDirection={sortDirection}
onSort={handleSort}
sortColumn={props.sortColumn}
sortDirection={props.sortDirection}
onSort={props.onGridSort}
Copy link
Contributor

@qili26 qili26 Oct 4, 2019

Choose a reason for hiding this comment

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

Do we need to print warning message if the sortColumn, sortDirection, onGridSort are not used together?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should but may be a in a different PR. I did discuss to Nico about the same thing.

minHeight={minHeight}
onViewportKeydown={props.onGridKeyDown}
onViewportKeyup={props.onGridKeyUp}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const SORT_TEXT = {
export interface Props<R> {
column: CalculatedColumn<R>;
rowType: HeaderRowType;
onSort(columnKey: keyof R, direction: DEFINE_SORT): void;
onSort?(columnKey: keyof R, direction: DEFINE_SORT): void;
sortDirection: DEFINE_SORT;
sortDescendingFirst: boolean;
allRowsSelected: boolean;
Expand All @@ -22,6 +22,7 @@ export interface Props<R> {
export default function SortableHeaderCell<R>(props: Props<R>) {
const { column, rowType, onSort, sortDirection, sortDescendingFirst, allRowsSelected, onAllRowsSelectionChange } = props;
function onClick() {
if (!onSort) return;
let direction;
switch (sortDirection) {
case DEFINE_SORT.ASC:
Expand Down