-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TextBoxVariable: Fixes and make it auto size (#394)
- Loading branch information
Showing
5 changed files
with
84 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,28 @@ | ||
import { SceneObjectBase } from '../core/SceneObjectBase'; | ||
import { SceneObjectState } from '../core/types'; | ||
import { SceneObject, SceneObjectState } from '../core/types'; | ||
import { VariableDependencyConfig } from './VariableDependencyConfig'; | ||
|
||
/** | ||
* Used in a couple of unit tests | ||
*/ | ||
export interface TestSceneState extends SceneObjectState { | ||
nested?: TestScene; | ||
nested?: SceneObject; | ||
/** To test logic for inactive scene objects */ | ||
hidden?: SceneObject; | ||
} | ||
|
||
export class TestScene extends SceneObjectBase<TestSceneState> {} | ||
|
||
interface TestSceneObjectState extends SceneObjectState { | ||
title: string; | ||
variableValueChanged: number; | ||
} | ||
|
||
export class TestObjectWithVariableDependency extends SceneObjectBase<TestSceneObjectState> { | ||
protected _variableDependency = new VariableDependencyConfig(this, { | ||
statePaths: ['title'], | ||
onReferencedVariableValueChanged: () => { | ||
this.setState({ variableValueChanged: this.state.variableValueChanged + 1 }); | ||
}, | ||
}); | ||
} |
38 changes: 24 additions & 14 deletions
38
packages/scenes/src/variables/components/VariableValueInput.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,38 @@ | ||
import React, { useState, useCallback } from 'react'; | ||
import React, { useCallback } from 'react'; | ||
|
||
import { Input } from '@grafana/ui'; | ||
import { AutoSizeInput } from '@grafana/ui'; | ||
|
||
import { SceneComponentProps } from '../../core/types'; | ||
import { TextBoxVariable } from '../variants/TextBoxVariable'; | ||
import { useDebounce } from 'react-use'; | ||
|
||
export function VariableValueInput({ model }: SceneComponentProps<TextBoxVariable>) { | ||
const { value, key, loading } = model.useState(); | ||
const [textValue, setTextValue] = useState(value); | ||
useDebounce( | ||
() => { | ||
model.setValue(textValue); | ||
|
||
const onBlur = useCallback( | ||
(e: React.ChangeEvent<HTMLInputElement>) => { | ||
model.setValue(e.currentTarget.value); | ||
}, | ||
250, | ||
[textValue] | ||
[model] | ||
); | ||
|
||
const onChange = useCallback( | ||
(e: React.ChangeEvent<HTMLInputElement>) => { | ||
setTextValue(e.target.value); | ||
const onKeyDown = useCallback( | ||
(e: React.KeyboardEvent<HTMLInputElement>) => { | ||
if (e.key === 'Enter') { | ||
model.setValue(e.currentTarget.value); | ||
} | ||
}, | ||
[setTextValue] | ||
[model] | ||
); | ||
|
||
return <Input id={key} placeholder="Enter value" value={textValue} loading={loading} onChange={onChange} />; | ||
return ( | ||
<AutoSizeInput | ||
id={key} | ||
placeholder="Enter value" | ||
minWidth={15} | ||
defaultValue={value} | ||
loading={loading} | ||
onBlur={onBlur} | ||
onKeyDown={onKeyDown} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/scenes/src/variables/variants/TextBoxVariable.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from 'react'; | ||
|
||
import { render, screen } from '@testing-library/react'; | ||
|
||
import { TextBoxVariable } from './TextBoxVariable'; | ||
import { SceneVariableSet } from '../sets/SceneVariableSet'; | ||
import { EmbeddedScene } from '../../components/EmbeddedScene'; | ||
import { VariableValueSelectors } from '../components/VariableValueSelectors'; | ||
import { TestObjectWithVariableDependency } from '../TestScene'; | ||
|
||
describe('TextBoxVariable', () => { | ||
it('Should not cause variable change mounted', async () => { | ||
const nestedObj = new TestObjectWithVariableDependency({ title: '$search', variableValueChanged: 0 }); | ||
|
||
const scene = new EmbeddedScene({ | ||
$variables: new SceneVariableSet({ variables: [new TextBoxVariable({ name: 'search' })] }), | ||
controls: [new VariableValueSelectors({})], | ||
body: nestedObj, | ||
}); | ||
|
||
render(<scene.Component model={scene} />); | ||
|
||
// There was a debounce before that fired some time after mount | ||
await new Promise((r) => setTimeout(r, 300)); | ||
|
||
expect(screen.getByText('search')).toBeInTheDocument(); | ||
expect(nestedObj.state.variableValueChanged).toBe(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters