PanelOptions: fix array with siblings (#39620) (#39623)

(cherry picked from commit c9ae46a9ad)

Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
This commit is contained in:
Grot (@grafanabot)
2021-09-25 17:58:09 +02:00
committed by GitHub
co-authored by Ryan McKinley
parent 258f3eae32
commit fee11227da
2 changed files with 2 additions and 0 deletions
@@ -98,6 +98,7 @@ describe('setOptionImmutably', () => {
${{}} | ${'a.b[2]'} | ${'x'} | ${{ a: { b: [undefined, undefined, 'x'] } }}
${{}} | ${'a[0]'} | ${1} | ${{ a: [1] }}
${{}} | ${'a[0].b.c'} | ${1} | ${{ a: [{ b: { c: 1 } }] }}
${{ a: [{ b: 1 }] }} | ${'a[0].c'} | ${2} | ${{ a: [{ b: 1, c: 2 }] }}
`('property value:${value', ({ source, path, value, expected }) => {
expect(setOptionImmutably(source, path, value)).toEqual(expected);
});
@@ -75,6 +75,7 @@ export function setOptionImmutably<T extends object>(options: T, path: string |
let current = (options as Record<string, any>)[propKey];
const arr = Array.isArray(current) ? [...current] : [];
if (splat.length) {
current = arr[index];
if (current == null || typeof current !== 'object') {
current = {};
}