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

Add GroupingChooserModel.setDimensions() #3743

Merged
merged 12 commits into from
Aug 30, 2024
46 changes: 37 additions & 9 deletions cmp/grouping/GroupingChooserModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import {
PlainObject,
XH
} from '@xh/hoist/core';
import {action, computed, observable, makeObservable} from '@xh/hoist/mobx';
import {genDisplayName} from '@xh/hoist/data';
import {action, computed, makeObservable, observable} from '@xh/hoist/mobx';
import {throwIf} from '@xh/hoist/utils/js';
import {createObservableRef} from '@xh/hoist/utils/react';
import {
cloneDeep,
difference,
isFunction,
isArray,
isEmpty,
isEqual,
isFunction,
isString,
keys,
sortBy
Expand Down Expand Up @@ -84,7 +84,8 @@ export class GroupingChooserModel extends HoistModel {

@observable.ref favorites: string[][] = [];

dimensions: Record<string, DimensionSpec>;
@observable.ref private _dimensions: Record<string, DimensionSpec>;

dimensionNames: string[];
allowEmpty: boolean;
maxDepth: number;
Expand All @@ -106,6 +107,11 @@ export class GroupingChooserModel extends HoistModel {
return difference(this.dimensionNames, this.pendingValue);
}

@computed
get dimensions() {
return this._dimensions;
}

@computed
get isValid(): boolean {
return this.validateValue(this.pendingValue);
Expand All @@ -132,13 +138,15 @@ export class GroupingChooserModel extends HoistModel {
super();
makeObservable(this);

this.dimensions = this.normalizeDimensions(dimensions);
this.dimensionNames = keys(this.dimensions);
this.setDimensions(dimensions);
this.allowEmpty = allowEmpty;
this.maxDepth = maxDepth;
this.commitOnChange = commitOnChange;

throwIf(isEmpty(this.dimensions), 'Must provide valid dimensions available for selection.');
throwIf(
isEmpty(this._dimensions),
'Must provide valid dimensions available for selection.'
);

// Read and validate value and favorites
let value = isFunction(initialValue) ? initialValue() : initialValue,
Expand Down Expand Up @@ -187,6 +195,13 @@ export class GroupingChooserModel extends HoistModel {
this.setFavorites(favorites);
}

@action
setDimensions(dimensions: Array<DimensionSpec | string>) {
this._dimensions = this.normalizeDimensions(dimensions);
this.dimensionNames = keys(this._dimensions);
this.cleanStaleDims();
}
amcclain marked this conversation as resolved.
Show resolved Hide resolved

@action
setValue(value: string[]) {
if (!this.validateValue(value)) {
Ryanseanlee marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -284,12 +299,14 @@ export class GroupingChooserModel extends HoistModel {
return {displayName: genDisplayName(src.name), ...src};
}

getValueLabel(value: string[]) {
return value.map(dimName => this.getDimDisplayName(dimName)).join(' › ');
getValueLabel(value: string[]): string {
return (
value.map(dimName => this.getDimDisplayName(dimName)).join(' › ') ?? value.toString()
amcclain marked this conversation as resolved.
Show resolved Hide resolved
);
}

getDimDisplayName(dimName: string) {
return this.dimensions[dimName].displayName;
return this._dimensions[dimName]?.displayName ?? dimName;
}

//--------------------
Expand Down Expand Up @@ -343,4 +360,15 @@ export class GroupingChooserModel extends HoistModel {
if (this.persistFavorites) ret.favorites = this.favorites;
return ret;
}

//------------------------
// Implementation
//------------------------
private cleanStaleDims() {
const {value, dimensionNames, allowEmpty} = this,
newValue = value?.filter(dim => dimensionNames.includes(dim));
isEmpty(newValue) && !allowEmpty
? this.setValue([this.dimensionNames[0]])
: this.setValue(newValue);
}
}
Loading