fix(explore): DndColumnSelect sometimes not working with multi: false (#15731)

* fix(explore): DndColumnSelect not working with multi: false

* fix values not synchronized when dataset changes
This commit is contained in:
Kamil Gabryjelski
2021-07-16 14:20:13 +02:00
committed by GitHub
parent 0721f54580
commit 66c28d653f
2 changed files with 40 additions and 25 deletions

View File

@@ -17,28 +17,23 @@
* under the License.
*/
import { ColumnMeta } from '@superset-ui/chart-controls';
import { ensureIsArray } from '@superset-ui/core';
export class OptionSelector {
values: ColumnMeta[];
options: { string: ColumnMeta };
isArray: boolean;
multi: boolean;
constructor(
options: { string: ColumnMeta },
multi: boolean,
initialValues?: string[] | string,
) {
this.options = options;
let values: string[];
if (Array.isArray(initialValues)) {
values = initialValues;
this.isArray = true;
} else {
values = initialValues ? [initialValues] : [];
this.isArray = false;
}
this.values = values
this.multi = multi;
this.values = ensureIsArray(initialValues)
.map(value => {
if (value in options) {
return options[value];
@@ -68,12 +63,12 @@ export class OptionSelector {
[this.values[a], this.values[b]] = [this.values[b], this.values[a]];
}
has(groupBy: string): boolean {
return !!this.getValues()?.includes(groupBy);
has(value: string): boolean {
return !!this.getValues()?.includes(value);
}
getValues(): string[] | string | undefined {
if (!this.isArray) {
if (!this.multi) {
return this.values.length > 0 ? this.values[0].column_name : undefined;
}
return this.values.map(option => option.column_name);