mirror of
https://github.com/grafana/grafana.git
synced 2025-01-07 22:53:56 -06:00
Query Builder: Fix max width of input component to prevent overflows (#61798)
* fix(auto-size-input): return maxWidth when realWidth exceeds limit * fix(query-builder): sex madWidth for simple editor component * fix(auto-size-input): add unit test
This commit is contained in:
parent
c0913ce718
commit
87f8e7e223
@ -1,13 +1,15 @@
|
||||
import { screen, render, fireEvent } from '@testing-library/react';
|
||||
import { screen, render, fireEvent, waitFor } from '@testing-library/react';
|
||||
import React from 'react';
|
||||
|
||||
import { measureText } from '../../utils/measureText';
|
||||
|
||||
import { AutoSizeInput } from './AutoSizeInput';
|
||||
|
||||
jest.mock('../../utils/measureText', () => {
|
||||
// Mocking measureText
|
||||
const measureText = (text: string, fontSize: number) => {
|
||||
const measureText = jest.fn().mockImplementation((text: string, fontSize: number) => {
|
||||
return { width: text.length * fontSize };
|
||||
};
|
||||
});
|
||||
|
||||
return { measureText };
|
||||
});
|
||||
@ -94,4 +96,26 @@ describe('AutoSizeInput', () => {
|
||||
|
||||
expect(onCommitChange).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should respect min width', async () => {
|
||||
render(<AutoSizeInput minWidth={4} defaultValue="" />);
|
||||
|
||||
await waitFor(() => expect(measureText).toHaveBeenCalled());
|
||||
|
||||
expect(getComputedStyle(screen.getByTestId('input-wrapper')).width).toBe('32px');
|
||||
});
|
||||
|
||||
it('should respect max width', async () => {
|
||||
render(
|
||||
<AutoSizeInput
|
||||
minWidth={1}
|
||||
maxWidth={4}
|
||||
defaultValue="thisisareallylongvalueandwhenisaylongireallymeanreallylongwithlotsofcharacterscommingfromuserinputwhotookthetimetocomeupwithalongstreamofcharacters"
|
||||
/>
|
||||
);
|
||||
|
||||
await waitFor(() => expect(measureText).toHaveBeenCalled());
|
||||
|
||||
expect(getComputedStyle(screen.getByTestId('input-wrapper')).width).toBe('32px');
|
||||
});
|
||||
});
|
||||
|
@ -63,7 +63,7 @@ function getWidthFor(value: string, minWidth: number, maxWidth: number | undefin
|
||||
}
|
||||
|
||||
if (maxWidth && realWidth > maxWidth) {
|
||||
return realWidth;
|
||||
return maxWidth;
|
||||
}
|
||||
|
||||
return realWidth;
|
||||
|
@ -38,6 +38,7 @@ function SimpleInputParamEditor(props: QueryBuilderOperationParamEditorProps) {
|
||||
minWidth={props.paramDef.minWidth}
|
||||
placeholder={props.paramDef.placeholder}
|
||||
title={props.paramDef.description}
|
||||
maxWidth={(props.paramDef.minWidth || 20) * 3}
|
||||
onCommitChange={(evt) => {
|
||||
props.onChange(props.index, evt.currentTarget.value);
|
||||
if (props.paramDef.runQueryOnEnter && evt.type === 'keydown') {
|
||||
|
Loading…
Reference in New Issue
Block a user