mirror of https://github.com/grafana/grafana
Fields: __field.name as field name and __field.displayName as displayName (#26531)
* name vs displayName * name vs displayName * add __values * add docs for displayName expressions * Update docs/sources/panels/field-configuration-options.md Co-authored-by: Diana Payton <52059945+oddlittlebird@users.noreply.github.com> * Update docs/sources/panels/field-configuration-options.md Co-authored-by: Diana Payton <52059945+oddlittlebird@users.noreply.github.com> * Update docs/sources/panels/field-configuration-options.md Co-authored-by: Diana Payton <52059945+oddlittlebird@users.noreply.github.com> * Update docs/sources/panels/field-configuration-options.md Co-authored-by: Diana Payton <52059945+oddlittlebird@users.noreply.github.com> * Update docs/sources/panels/field-configuration-options.md Co-authored-by: Diana Payton <52059945+oddlittlebird@users.noreply.github.com> * Update docs/sources/panels/field-configuration-options.md Co-authored-by: Diana Payton <52059945+oddlittlebird@users.noreply.github.com> Co-authored-by: kyle <kyle@grafana.com> Co-authored-by: Diana Payton <52059945+oddlittlebird@users.noreply.github.com>pull/26806/head^2
parent
9e5fe8dbdb
commit
ec783fbff4
@ -0,0 +1,32 @@ |
||||
import { getTemplateProxyForField } from './templateProxies'; |
||||
import { toDataFrame } from '../dataframe'; |
||||
|
||||
describe('Template proxies', () => { |
||||
it('supports name and displayName', () => { |
||||
const frames = [ |
||||
toDataFrame({ |
||||
fields: [ |
||||
{ |
||||
name: '🔥', |
||||
config: { displayName: '✨' }, |
||||
labels: { |
||||
b: 'BBB', |
||||
a: 'AAA', |
||||
}, |
||||
}, |
||||
], |
||||
}), |
||||
]; |
||||
|
||||
const f = getTemplateProxyForField(frames[0].fields[0], frames[0], frames); |
||||
|
||||
expect(f.name).toEqual('🔥'); |
||||
expect(f.displayName).toEqual('✨'); |
||||
expect(`${f.labels}`).toEqual('a="AAA", b="BBB"'); |
||||
expect(f.labels.__values).toEqual('AAA, BBB'); |
||||
expect(f.labels.a).toEqual('AAA'); |
||||
|
||||
// Deprecated syntax
|
||||
expect(`${f.formattedLabels}`).toEqual('a="AAA", b="BBB"'); |
||||
}); |
||||
}); |
@ -0,0 +1,41 @@ |
||||
import { DataFrame, Field } from '../types'; |
||||
import { getFieldDisplayName } from './fieldState'; |
||||
import { formatLabels } from '../utils/labels'; |
||||
|
||||
/** |
||||
* This object is created often, and only used when tmplates exist. Using a proxy lets us delay |
||||
* calculations of the more complex structures (label names) until they are actually used |
||||
*/ |
||||
export function getTemplateProxyForField(field: Field, frame?: DataFrame, frames?: DataFrame[]): any { |
||||
return new Proxy( |
||||
{} as any, // This object shows up in test snapshots
|
||||
{ |
||||
get: (obj: Field, key: string, reciever: any) => { |
||||
if (key === 'name') { |
||||
return field.name; |
||||
} |
||||
|
||||
if (key === 'displayName') { |
||||
return getFieldDisplayName(field, frame, frames); |
||||
} |
||||
|
||||
if (key === 'labels' || key === 'formattedLabels') { |
||||
// formattedLabels deprecated
|
||||
if (!field.labels) { |
||||
return ''; |
||||
} |
||||
return { |
||||
...field.labels, |
||||
__values: Object.values(field.labels) |
||||
.sort() |
||||
.join(', '), |
||||
toString: () => { |
||||
return formatLabels(field.labels!, '', true); |
||||
}, |
||||
}; |
||||
} |
||||
return undefined; // (field as any)[key]; // any property?
|
||||
}, |
||||
} |
||||
); |
||||
} |
Loading…
Reference in new issue