mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Table: Fix viz suggestions (#99335)
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user