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

QueryVariable: Use correct option property for variable options sorting #1015

Merged
merged 4 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -322,9 +322,9 @@ describe.each(['11.1.2', '11.1.1'])('QueryVariable', (v) => {
await lastValueFrom(variable.validateAndUpdate());

expect(variable.state.options).toEqual([
{ label: 'val1', value: 'val1' },
{ label: 'val2', value: 'val2' },
{ label: 'val11', value: 'val11' },
{ label: 'val2', value: 'val2' },
{ label: 'val1', value: 'val1' },
]);
});

Expand All @@ -339,9 +339,9 @@ describe.each(['11.1.2', '11.1.1'])('QueryVariable', (v) => {
await lastValueFrom(variable.validateAndUpdate());

expect(variable.state.options).toEqual([
{ label: 'val11', value: 'val11' },
{ label: 'val2', value: 'val2' },
{ label: 'val1', value: 'val1' },
{ label: 'val2', value: 'val2' },
{ label: 'val11', value: 'val11' },
]);
});

Expand Down
57 changes: 27 additions & 30 deletions packages/scenes/src/variables/variants/query/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,40 +74,11 @@ const getAllMatches = (str: string, regex: RegExp): RegExpExecArray[] => {
return results;
};

export const sortVariableValues = (options: any[], sortOrder: VariableSort) => {
export const sortVariableValues = (options: VariableValueOption[], sortOrder: VariableSort) => {
if (sortOrder === VariableSort.disabled) {
return options;
}

// @ts-ignore
const sortByNumeric = (opt) => {
if (!opt.text) {
return -1;
}
const matches = opt.text.match(/.*?(\d+).*/);
if (!matches || matches.length < 2) {
return -1;
} else {
return parseInt(matches[1], 10);
}
};

// @ts-ignore
const sortByNaturalSort = (options) => {
//@ts-ignore
return options.sort((a, b) => {
if (!a.text) {
return -1;
}

if (!b.text) {
return 1;
}

return a.text.localeCompare(b.text, undefined, { numeric: true });
});
};

switch (sortOrder) {
case VariableSort.alphabeticalAsc:
options = sortBy(options, 'label');
Expand Down Expand Up @@ -146,3 +117,29 @@ export const sortVariableValues = (options: any[], sortOrder: VariableSort) => {
}
return options;
};

function sortByNumeric(opt: VariableValueOption) {
if (!opt.label) {
return -1;
}
const matches = opt.label.match(/.*?(\d+).*/);
if (!matches || matches.length < 2) {
return -1;
} else {
return parseInt(matches[1], 10);
}
}

function sortByNaturalSort(options: VariableValueOption[]) {
return options.sort((a, b) => {
leeoniya marked this conversation as resolved.
Show resolved Hide resolved
if (!a.label) {
return -1;
}

if (!b.label) {
return 1;
}

return a.label.localeCompare(b.label, undefined, { numeric: true });
leeoniya marked this conversation as resolved.
Show resolved Hide resolved
});
}
Loading