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

fix: Keep chosen columns sort option when changing a column #15918

Merged
merged 2 commits into from Jul 28, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 38 additions & 26 deletions superset-frontend/src/CRUD/CollectionTable.tsx
Expand Up @@ -67,15 +67,20 @@ function createCollectionArray(collection: object) {
}

function createKeyedCollection(arr: Array<object>) {
const newArr = arr.map((o: any) => ({
const collectionArray = arr.map((o: any) => ({
...o,
id: o.id || shortid.generate(),
}));
const map = {};
newArr.forEach((o: any) => {
map[o.id] = o;

const collection = {};
collectionArray.forEach((o: any) => {
collection[o.id] = o;
});
return map;

return {
collection,
collectionArray,
};
}

const CrudTableWrapper = styled.div<{ stickyHeader?: boolean }>`
Expand Down Expand Up @@ -114,11 +119,13 @@ export default class CRUDCollection extends React.PureComponent<
constructor(props: CRUDCollectionProps) {
super(props);

const collection = createKeyedCollection(props.collection);
const { collection, collectionArray } = createKeyedCollection(
props.collection,
);
this.state = {
expandedColumns: {},
collection,
collectionArray: createCollectionArray(collection),
collectionArray,
sortColumn: '',
sort: 0,
};
Expand All @@ -135,10 +142,12 @@ export default class CRUDCollection extends React.PureComponent<

UNSAFE_componentWillReceiveProps(nextProps: CRUDCollectionProps) {
if (nextProps.collection !== this.props.collection) {
const collection = createKeyedCollection(nextProps.collection);
const { collection, collectionArray } = createKeyedCollection(
nextProps.collection,
);
this.setState({
collection,
collectionArray: createCollectionArray(collection),
collectionArray,
});
}
}
Expand Down Expand Up @@ -186,7 +195,10 @@ export default class CRUDCollection extends React.PureComponent<
changeCollection(collection: any) {
this.setState({ collection });
if (this.props.onChange) {
this.props.onChange(Object.keys(collection).map(k => collection[k]));
const collectionArray = this.state.collectionArray
.map((c: { id: number }) => collection[c.id])
.filter(c => c !== undefined);
this.props.onChange(collectionArray);
}
}

Expand Down Expand Up @@ -227,29 +239,29 @@ export default class CRUDCollection extends React.PureComponent<
if (sortColumns?.includes(col)) {
// display in unsorted order if no sort specified
if (sort === SortOrder.unsort) {
const collection = createKeyedCollection(this.props.collection);
const { collection } = createKeyedCollection(this.props.collection);
const collectionArray = createCollectionArray(collection);
this.setState({
collectionArray: createCollectionArray(collection),
collectionArray,
sortColumn: '',
sort,
});
return;
}

this.setState(prevState => {
// newly ordered collection
const sorted = [
...prevState.collectionArray,
].sort((a: object, b: object) => compareSort(a[col], b[col]));
const newCollection =
sort === SortOrder.asc ? sorted : sorted.reverse();
return {
...prevState,
collectionArray: newCollection,
sortColumn: col,
sort,
};
});
// newly ordered collection
const sorted = [
...this.state.collectionArray,
].sort((a: object, b: object) => compareSort(a[col], b[col]));
const newCollection =
sort === SortOrder.asc ? sorted : sorted.reverse();

this.setState(prevState => ({
...prevState,
collectionArray: newCollection,
sortColumn: col,
sort,
}));
}
};
}
Expand Down