Table: Fix viz suggestions (#99335)

This commit is contained in:
Leon Sorokin
2025-01-21 13:25:33 -06:00
committed by GitHub
parent dccca0729f
commit a03f897cd5
3 changed files with 16 additions and 14 deletions

View File

@@ -10,7 +10,7 @@ import {
} from 'react-table';
import { VariableSizeList } from 'react-window';
import { FieldType, ReducerID, getRowUniqueId, getFieldMatcher } from '@grafana/data';
import { FieldType, ReducerID, getRowUniqueId, getFieldMatcher, Field } from '@grafana/data';
import { selectors } from '@grafana/e2e-selectors';
import { TableCellHeight } from '@grafana/schema';
@@ -308,9 +308,12 @@ export const Table = memo((props: Props) => {
// Try to determine the longet field
// TODO: do we wrap only one field?
// What if there are multiple fields with long text?
const longestField = guessLongestField(fieldConfig, data);
let longestField: Field | undefined = undefined;
let textWrapField = undefined;
if (fieldConfig !== undefined) {
if (fieldConfig) {
longestField = guessLongestField(fieldConfig, data);
data.fields.forEach((field) => {
fieldConfig.overrides.forEach((override) => {
const matcher = getFieldMatcher(override.matcher);

View File

@@ -1,7 +1,7 @@
import { faker } from '@faker-js/faker';
import { Row } from 'react-table';
import { Field, FieldType, MutableDataFrame, SelectableValue } from '@grafana/data';
import { Field, FieldConfigSource, FieldType, MutableDataFrame, SelectableValue } from '@grafana/data';
import {
calculateUniqueFieldValues,
@@ -548,7 +548,7 @@ describe('Table utils', () => {
// FLAKY TEST - https://drone.grafana.net/grafana/grafana/201232/1/5
it.skip('should guess the longest field correctly if there are few records', () => {
const data = getWrappableData(10);
const config = {
const config: FieldConfigSource = {
defaults: {
custom: {
cellOptions: {
@@ -556,6 +556,7 @@ describe('Table utils', () => {
},
},
},
overrides: [],
};
const longestField = guessLongestField(config, data);
@@ -564,7 +565,7 @@ describe('Table utils', () => {
it('should guess the longest field correctly if there are many records', () => {
const data = getWrappableData(1000);
const config = {
const config: FieldConfigSource = {
defaults: {
custom: {
cellOptions: {
@@ -572,6 +573,7 @@ describe('Table utils', () => {
},
},
},
overrides: [],
};
const longestField = guessLongestField(config, data);
@@ -580,7 +582,7 @@ describe('Table utils', () => {
it('should return undefined if there is no data', () => {
const data = getData();
const config = {
const config: FieldConfigSource = {
defaults: {
custom: {
cellOptions: {
@@ -588,6 +590,7 @@ describe('Table utils', () => {
},
},
},
overrides: [],
};
const longestField = guessLongestField(config, data);

View File

@@ -9,6 +9,7 @@ import {
DisplayValue,
DisplayValueAlignmentFactors,
Field,
FieldConfigSource,
fieldReducers,
FieldType,
formattedValueToString,
@@ -713,19 +714,14 @@ export function guessTextBoundingBox(
* To do this we either select a single record if there aren't many records
* or we select records at random and sample their size.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function guessLongestField(fieldConfig: any, data: DataFrame) {
export function guessLongestField(fieldConfig: FieldConfigSource, data: DataFrame) {
let longestField = undefined;
const SAMPLE_SIZE = 3;
// If the default field option is set to allow text wrapping
// we determine the field to wrap text with here and then
// pass it to the RowsList
if (
fieldConfig !== undefined &&
fieldConfig.defaults.custom !== undefined &&
fieldConfig.defaults.custom.cellOptions.wrapText
) {
if (fieldConfig.defaults.custom?.cellOptions?.wrapText) {
const stringFields = data.fields.filter((field: Field) => field.type === FieldType.string);
if (stringFields.length >= 1 && stringFields[0].values.length > 0) {