1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-05-08 01:15:49 +02:00

fix: autocomplete bug when changing context field (#4064)

<!-- Thanks for creating a PR! To make it easier for reviewers and
everyone else to understand what your changes relate to, please add some
relevant content to the headings below. Feel free to ignore or delete
sections that you don't think are relevant. Thank you! ❤️ -->
Fixes a bug with the Autocomplete tags not clearing when moving from on
context field with values to another
## About the changes
<!-- Describe the changes introduced. What are they and why are they
being introduced? Feel free to also add screenshots or steps to view the
changes if they're visual. -->

<!-- Does it close an issue? Multiple? -->
Closes #

<!-- (For internal contributors): Does it relate to an issue on public
roadmap? -->
<!--
Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item:
#
-->

### Important files
<!-- PRs can contain a lot of changes, but not all changes are equally
important. Where should a reviewer start looking to get an overview of
the changes? Are any files particularly important? -->


## Discussion points
<!-- Anything about the PR you'd like to discuss before it gets merged?
Got any questions or doubts? -->

---------

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
This commit is contained in:
andreas-unleash 2023-06-22 14:19:41 +03:00 committed by GitHub
parent 8cd89bb5a7
commit fc35f227dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,6 +18,7 @@ import {
Typography,
useTheme,
Autocomplete,
SelectChangeEvent,
} from '@mui/material';
import { debounce } from 'debounce';
@ -97,9 +98,9 @@ export const PlaygroundCodeFieldset: VFC<IPlaygroundCodeFieldsetProps> = ({
(foundContext?.legalValues &&
foundContext.legalValues.length > 0) ||
contextField === 'currentTime'
)
return;
setContextValue('');
) {
return setContextValue('');
}
} catch (error) {
setToastData({
type: 'error',
@ -108,7 +109,7 @@ export const PlaygroundCodeFieldset: VFC<IPlaygroundCodeFieldsetProps> = ({
}
};
const onAutoCompleteChange = (
const changeContextValue = (
e: FormEvent,
newValue: string | (string | string[])[] | null
) => {
@ -123,11 +124,11 @@ export const PlaygroundCodeFieldset: VFC<IPlaygroundCodeFieldsetProps> = ({
setContextValue(newValue);
};
const resolveAutocompleteValue = (): string | string[] | undefined => {
const resolveAutocompleteValue = (): string | string[] | null => {
//This is needed for clearing the Autocomplete Chips when changing the context field
//and the new field also has legal values
if (contextValue === '') {
return undefined;
if (!contextValue || contextValue === '') {
return [];
}
if (isAdvancedPlayground) {
@ -188,9 +189,11 @@ export const PlaygroundCodeFieldset: VFC<IPlaygroundCodeFieldsetProps> = ({
<Autocomplete
disablePortal
id="context-legal-values"
freeSolo
filterSelectedOptions
size="small"
value={resolveAutocompleteValue()}
onChange={onAutoCompleteChange}
onChange={changeContextValue}
options={options}
multiple={isAdvancedPlayground}
sx={{ width: 200, maxWidth: '100%' }}
@ -213,6 +216,16 @@ export const PlaygroundCodeFieldset: VFC<IPlaygroundCodeFieldsetProps> = ({
);
};
const changeContextField = (event: SelectChangeEvent) => {
setContextField(event.target.value || '');
if (event.target.value === 'currentTime') {
return setContextValue(new Date().toISOString());
}
setContextValue('');
};
return (
<Box>
<Box sx={{ display: 'flex', alignItems: 'center', mb: 2 }}>
@ -236,16 +249,7 @@ export const PlaygroundCodeFieldset: VFC<IPlaygroundCodeFieldsetProps> = ({
labelId="context-field-label"
id="context-field"
value={contextField}
onChange={event => {
setContextField(event.target.value || '');
if (event.target.value === 'currentTime') {
return setContextValue(
new Date().toISOString()
);
}
setContextValue('');
}}
onChange={changeContextField}
variant="outlined"
size="small"
sx={{ width: 200, maxWidth: '100%' }}