Skip to content

Commit

Permalink
fix stale input value
Browse files Browse the repository at this point in the history
  • Loading branch information
mshustov committed Apr 28, 2023
1 parent f4bb147 commit 3132f4f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/components/queryBuilder/Filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,12 @@ const FilterValueNumberItem = (props: { value: number; onChange: (value: number)
};

const FilterValueSingleStringItem = (props: { value: string; onChange: (value: string) => void }) => {
const [value, setValue] = useState(props.value || '');
return (
<div data-testid="query-builder-filters-single-string-value-container">
<Input
type="text"
value={value}
onChange={(e) => setValue(e.currentTarget.value)}
onBlur={() => props.onChange(value)}
defaultValue={props.value}
onBlur={(e) => props.onChange(e.currentTarget.value)}
/>
</div>
);
Expand Down Expand Up @@ -142,7 +140,15 @@ export const FilterValueEditor = (props: {
</div>
);
}
return <FilterValueSingleStringItem value={filter.value} onChange={onStringFilterValueChange} />;

return (
<FilterValueSingleStringItem
value={filter.value}
onChange={onStringFilterValueChange}
// enforce input re-render when filter changes to avoid stale input value
key={filter.value}
/>
);
} else if (utils.isMultiFilter(filter)) {
const onMultiFilterValueChange = (value: string[]) => {
onFilterChange({ ...filter, value });
Expand Down
1 change: 1 addition & 0 deletions src/components/queryBuilder/QueryBuilder.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('QueryBuilder', () => {
database: 'db',
table: 'foo',
fields: [],
filters: [],
}}
onBuilderOptionsChange={() => {}}
datasource={mockDs}
Expand Down

0 comments on commit 3132f4f

Please sign in to comment.