fix(explore): Reset values in TextControl only when datasource changes (#13211)

* Reset values in TextControl only when datasource changes

* Fix undefined error
This commit is contained in:
Kamil Gabryjelski 2021-02-19 14:28:49 +01:00 committed by GitHub
parent 85d1e80544
commit 42ff4fc19a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 6 deletions

View File

@ -247,6 +247,7 @@ export default class SelectControl extends React.PureComponent {
isMulti &&
optionsRemaining &&
Array.isArray(this.state.value) &&
Array.isArray(value) &&
!!value.length
) {
assistiveText = optionRemaingText;

View File

@ -37,6 +37,7 @@ interface TextControlProps {
interface TextControlState {
controlId: string;
currentDatasource?: string;
value?: string | number;
}
@ -55,8 +56,14 @@ export default class TextControl extends React.Component<
props: TextControlProps,
state: TextControlState,
) {
if (props.value !== state.value) {
return { value: props.value };
// reset value when datasource changes
// props.datasource and props.value don't update in the same re-render,
// so we need to synchronize them to update the state with correct values
if (
props.value !== state.value &&
props.datasource !== state.currentDatasource
) {
return { value: props.value, currentDatasource: props.datasource };
}
return null;
}
@ -69,13 +76,10 @@ export default class TextControl extends React.Component<
this.state = {
controlId: generateControlId(props.controlId),
value: props.value,
currentDatasource: props.datasource,
};
}
defaultInput = () => {
this.setState({ value: '' });
};
onChange = (inputValue: string) => {
let parsedValue: string | number = inputValue;
// Validation & casting