mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Options: support array value paths for panel options (#39499)
This commit is contained in:
@@ -89,13 +89,16 @@ describe('updateDefaultFieldConfigValue', () => {
|
||||
|
||||
describe('setOptionImmutably', () => {
|
||||
it.each`
|
||||
source | path | value | expected
|
||||
${{}} | ${'a'} | ${1} | ${{ a: 1 }}
|
||||
${{}} | ${'a.b.c'} | ${[1, 2]} | ${{ a: { b: { c: [1, 2] } } }}
|
||||
${{ a: {} }} | ${'a.b.c'} | ${[1, 2]} | ${{ a: { b: { c: [1, 2] } } }}
|
||||
${{ b: {} }} | ${'a.b.c'} | ${[1, 2]} | ${{ a: { b: { c: [1, 2] } }, b: {} }}
|
||||
${{ a: { b: { c: 3 } } }} | ${'a.b.c'} | ${[1, 2]} | ${{ a: { b: { c: [1, 2] } } }}
|
||||
`('numeric-like text mapping, value:${value', ({ source, path, value, expected }) => {
|
||||
source | path | value | expected
|
||||
${{}} | ${'a'} | ${1} | ${{ a: 1 }}
|
||||
${{}} | ${'a.b.c'} | ${[1, 2]} | ${{ a: { b: { c: [1, 2] } } }}
|
||||
${{ a: {} }} | ${'a.b.c'} | ${[1, 2]} | ${{ a: { b: { c: [1, 2] } } }}
|
||||
${{ b: {} }} | ${'a.b.c'} | ${[1, 2]} | ${{ a: { b: { c: [1, 2] } }, b: {} }}
|
||||
${{ a: { b: { c: 3 } } }} | ${'a.b.c'} | ${[1, 2]} | ${{ a: { b: { c: [1, 2] } } }}
|
||||
${{}} | ${'a.b[2]'} | ${'x'} | ${{ a: { b: [undefined, undefined, 'x'] } }}
|
||||
${{}} | ${'a[0]'} | ${1} | ${{ a: [1] }}
|
||||
${{}} | ${'a[0].b.c'} | ${1} | ${{ a: [{ b: { c: 1 } }] }}
|
||||
`('property value:${value', ({ source, path, value, expected }) => {
|
||||
expect(setOptionImmutably(source, path, value)).toEqual(expected);
|
||||
});
|
||||
|
||||
|
||||
@@ -68,6 +68,21 @@ export function setOptionImmutably<T extends object>(options: T, path: string |
|
||||
const splat = !Array.isArray(path) ? path.split('.') : path;
|
||||
|
||||
const key = splat.shift()!;
|
||||
if (key.endsWith(']')) {
|
||||
const idx = key.lastIndexOf('[');
|
||||
const index = +key.substring(idx + 1, key.length - 1);
|
||||
const propKey = key.substr(0, idx);
|
||||
let current = (options as Record<string, any>)[propKey];
|
||||
const arr = Array.isArray(current) ? [...current] : [];
|
||||
if (splat.length) {
|
||||
if (current == null || typeof current !== 'object') {
|
||||
current = {};
|
||||
}
|
||||
value = setOptionImmutably(current, splat, value);
|
||||
}
|
||||
arr[index] = value;
|
||||
return { ...options, [propKey]: arr };
|
||||
}
|
||||
|
||||
if (!splat.length) {
|
||||
return { ...options, [key]: value };
|
||||
|
||||
Reference in New Issue
Block a user