mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
AutoSizeInput: Allow to be controlled by value (#92997)
This commit is contained in:
parent
d5ebaa0ef9
commit
35ba8fbad1
@ -35,6 +35,8 @@ const meta: Meta = {
|
|||||||
suffixVisible: '',
|
suffixVisible: '',
|
||||||
invalid: false,
|
invalid: false,
|
||||||
loading: false,
|
loading: false,
|
||||||
|
value: '',
|
||||||
|
defaultValue: '',
|
||||||
},
|
},
|
||||||
argTypes: {
|
argTypes: {
|
||||||
prefixVisible: {
|
prefixVisible: {
|
||||||
@ -80,6 +82,8 @@ export const Simple: StoryFn = (args) => {
|
|||||||
type={args.type}
|
type={args.type}
|
||||||
placeholder={args.placeholder}
|
placeholder={args.placeholder}
|
||||||
minWidth={args.minWidth}
|
minWidth={args.minWidth}
|
||||||
|
value={args.value}
|
||||||
|
defaultValue={args.defaultValue}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@ -88,6 +92,8 @@ Simple.args = {
|
|||||||
before: false,
|
before: false,
|
||||||
after: false,
|
after: false,
|
||||||
placeholder: 'Enter your name here...',
|
placeholder: 'Enter your name here...',
|
||||||
|
value: '',
|
||||||
|
defaultValue: '',
|
||||||
};
|
};
|
||||||
|
|
||||||
export default meta;
|
export default meta;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { screen, render, fireEvent, waitFor } from '@testing-library/react';
|
import { screen, render, fireEvent, waitFor } from '@testing-library/react';
|
||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
|
||||||
import { measureText } from '../../utils/measureText';
|
import { measureText } from '../../utils/measureText';
|
||||||
|
|
||||||
@ -117,4 +118,30 @@ describe('AutoSizeInput', () => {
|
|||||||
|
|
||||||
expect(getComputedStyle(screen.getByTestId('input-wrapper')).width).toBe('32px');
|
expect(getComputedStyle(screen.getByTestId('input-wrapper')).width).toBe('32px');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should update the input value if the value prop changes', () => {
|
||||||
|
// Wrapper component to control the `value` prop
|
||||||
|
const Wrapper = () => {
|
||||||
|
const [value, setValue] = useState('Initial');
|
||||||
|
|
||||||
|
// Simulate prop change after render
|
||||||
|
useEffect(() => {
|
||||||
|
setTimeout(() => setValue('Updated'), 100); // Update `value` after 100ms
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return <AutoSizeInput value={value} />;
|
||||||
|
};
|
||||||
|
|
||||||
|
render(<Wrapper />);
|
||||||
|
|
||||||
|
const input: HTMLInputElement = screen.getByTestId('autosize-input');
|
||||||
|
|
||||||
|
// Check initial value
|
||||||
|
expect(input.value).toBe('Initial');
|
||||||
|
|
||||||
|
// Wait for the value to update
|
||||||
|
return waitFor(() => {
|
||||||
|
expect(input.value).toBe('Updated');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -15,13 +15,32 @@ export interface Props extends InputProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const AutoSizeInput = React.forwardRef<HTMLInputElement, Props>((props, ref) => {
|
export const AutoSizeInput = React.forwardRef<HTMLInputElement, Props>((props, ref) => {
|
||||||
const { defaultValue = '', minWidth = 10, maxWidth, onCommitChange, onKeyDown, onBlur, ...restProps } = props;
|
const {
|
||||||
const [value, setValue] = React.useState(defaultValue);
|
defaultValue = '',
|
||||||
|
minWidth = 10,
|
||||||
|
maxWidth,
|
||||||
|
onCommitChange,
|
||||||
|
onKeyDown,
|
||||||
|
onBlur,
|
||||||
|
value: controlledValue,
|
||||||
|
...restProps
|
||||||
|
} = props;
|
||||||
|
|
||||||
|
// Initialize internal state
|
||||||
|
const [value, setValue] = React.useState(controlledValue ?? defaultValue);
|
||||||
const [inputWidth, setInputWidth] = React.useState(minWidth);
|
const [inputWidth, setInputWidth] = React.useState(minWidth);
|
||||||
|
|
||||||
|
// Update internal state when controlled `value` prop changes
|
||||||
|
useEffect(() => {
|
||||||
|
if (controlledValue !== undefined) {
|
||||||
|
setValue(controlledValue);
|
||||||
|
}
|
||||||
|
}, [controlledValue]);
|
||||||
|
|
||||||
|
// Update input width when `value`, `minWidth`, or `maxWidth` change
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setInputWidth(getWidthFor(value.toString(), minWidth, maxWidth));
|
setInputWidth(getWidthFor(value.toString(), minWidth, maxWidth));
|
||||||
}, [value, maxWidth, minWidth]);
|
}, [value, minWidth, maxWidth]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Input
|
<Input
|
||||||
|
Loading…
Reference in New Issue
Block a user