-
Notifications
You must be signed in to change notification settings - Fork 12
[BUGFIX] Fix variable editing panel's custom all value text field to handle empty string #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zhuje
wants to merge
4
commits into
main
Choose a base branch
from
ou1159-custom-all-varaible
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
114ca07
[BUGFIX] fix edit variable panel's custom all value text field to han…
zhuje fac8185
[BUGFIX] fix edit variable panel's custom all value text field to han…
zhuje 889d936
[BUGFIX] fix edit variable panel's custom all value text field
zhuje c3da52b
[BUGFIX] fix edit variable panel's custom all value text field with S…
zhuje File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,7 +11,7 @@ | |||||||||||||
| // See the License for the specific language governing permissions and | ||||||||||||||
| // limitations under the License. | ||||||||||||||
|
|
||||||||||||||
| import { DispatchWithoutAction, ReactElement, useCallback, useState } from 'react'; | ||||||||||||||
| import { DispatchWithoutAction, ReactElement, useCallback, useState, useEffect } from 'react'; | ||||||||||||||
| import { Box, Typography, Switch, TextField, Grid, FormControlLabel, MenuItem, Stack, Divider } from '@mui/material'; | ||||||||||||||
| import { VariableDefinition, ListVariableDefinition, Action } from '@perses-dev/core'; | ||||||||||||||
| import { DiscardChangesConfirmationDialog, ErrorAlert, ErrorBoundary, FormActions } from '@perses-dev/components'; | ||||||||||||||
|
|
@@ -121,11 +121,24 @@ function ListVariableEditorForm({ action, control }: KindVariableEditorFormProps | |||||||||||||
| name: 'spec.allowAllValue', | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| const _customAllValue = useWatch<VariableDefinition, 'spec.customAllValue'>({ | ||||||||||||||
| control: control, | ||||||||||||||
| name: 'spec.customAllValue', | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| const sortMethod = useWatch<VariableDefinition, 'spec.sort'>({ | ||||||||||||||
| control: control, | ||||||||||||||
| name: 'spec.sort', | ||||||||||||||
| }) as SortMethodName; | ||||||||||||||
|
|
||||||||||||||
| // State for managing whether to use custom all value | ||||||||||||||
| const [useCustomAllValue, setUseCustomAllValue] = useState(_customAllValue !== undefined); | ||||||||||||||
|
|
||||||||||||||
| // Sync switch state when form value changes externally | ||||||||||||||
| useEffect(() => { | ||||||||||||||
| setUseCustomAllValue(_customAllValue !== undefined); | ||||||||||||||
| }, [_customAllValue]); | ||||||||||||||
|
|
||||||||||||||
| // When variable kind is selected we need to provide default values | ||||||||||||||
| // TODO: check if react-hook-form has a better way to do this | ||||||||||||||
| if (values.spec.allowAllValue === undefined) { | ||||||||||||||
|
|
@@ -309,35 +322,53 @@ function ListVariableEditorForm({ action, control }: KindVariableEditorFormProps | |||||||||||||
| Enables an option to include all variable values | ||||||||||||||
| </Typography> | ||||||||||||||
| {_allowAllValue && ( | ||||||||||||||
| <Controller | ||||||||||||||
| control={control} | ||||||||||||||
| name="spec.customAllValue" | ||||||||||||||
| render={({ field, fieldState }) => ( | ||||||||||||||
| <TextField | ||||||||||||||
| {...field} | ||||||||||||||
| fullWidth | ||||||||||||||
| label="Custom All Value" | ||||||||||||||
| InputLabelProps={{ shrink: action === 'read' ? true : undefined }} | ||||||||||||||
| InputProps={{ | ||||||||||||||
| readOnly: action === 'read', | ||||||||||||||
| }} | ||||||||||||||
| error={!!fieldState.error} | ||||||||||||||
| helperText={ | ||||||||||||||
| fieldState.error?.message | ||||||||||||||
| ? fieldState.error.message | ||||||||||||||
| : 'When All is selected, this value will be used' | ||||||||||||||
| } | ||||||||||||||
| value={field.value ?? ''} | ||||||||||||||
| onChange={(event) => { | ||||||||||||||
| if (event.target.value === '') { | ||||||||||||||
| field.onChange(undefined); | ||||||||||||||
| } else { | ||||||||||||||
| field.onChange(event); | ||||||||||||||
| } | ||||||||||||||
| }} | ||||||||||||||
| <Stack spacing={1}> | ||||||||||||||
| <FormControlLabel | ||||||||||||||
| label="Use Custom All Value" | ||||||||||||||
| control={ | ||||||||||||||
| <Switch | ||||||||||||||
| checked={useCustomAllValue} | ||||||||||||||
| readOnly={action === 'read'} | ||||||||||||||
| onChange={(event) => { | ||||||||||||||
| if (action === 'read') return; | ||||||||||||||
| const isEnabled = event.target.checked; | ||||||||||||||
| setUseCustomAllValue(isEnabled); | ||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove the negation here
Suggested change
|
||||||||||||||
| if (!isEnabled) { | ||||||||||||||
| form.setValue('spec.customAllValue', undefined); | ||||||||||||||
| } else { | ||||||||||||||
| form.setValue('spec.customAllValue', ''); | ||||||||||||||
| } | ||||||||||||||
| }} | ||||||||||||||
| /> | ||||||||||||||
| } | ||||||||||||||
| /> | ||||||||||||||
| <Typography variant="caption" sx={{ mt: -0.5 }}> | ||||||||||||||
| Enable to set a custom value when "All" is selected | ||||||||||||||
| </Typography> | ||||||||||||||
| {useCustomAllValue && ( | ||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| <Controller | ||||||||||||||
| control={control} | ||||||||||||||
| name="spec.customAllValue" | ||||||||||||||
| render={({ field, fieldState }) => ( | ||||||||||||||
| <TextField | ||||||||||||||
| {...field} | ||||||||||||||
| fullWidth | ||||||||||||||
| label="Custom All Value" | ||||||||||||||
| InputLabelProps={{ shrink: action === 'read' ? true : undefined }} | ||||||||||||||
| InputProps={{ | ||||||||||||||
| readOnly: action === 'read', | ||||||||||||||
| }} | ||||||||||||||
| error={!!fieldState.error} | ||||||||||||||
| helperText={fieldState.error?.message} | ||||||||||||||
| value={field.value ?? ''} | ||||||||||||||
| onChange={(event) => { | ||||||||||||||
| field.onChange(event.target.value || ''); | ||||||||||||||
| }} | ||||||||||||||
| /> | ||||||||||||||
| )} | ||||||||||||||
| /> | ||||||||||||||
| )} | ||||||||||||||
| /> | ||||||||||||||
| </Stack> | ||||||||||||||
| )} | ||||||||||||||
| </Stack> | ||||||||||||||
| </Stack> | ||||||||||||||
|
|
||||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arf, not fan of this useEffect, that why I think using a
Switchcomponent would be simpler, for enabling or not custom all value.If switch is enable => show text field and default value is empty string.
If false => textfield hidden and value is undefined
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, Switch is probably the simpler approach. Push another commit to update it to the Switch.
Screen.Recording.2026-02-24.at.4.46.26.PM.mov
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks better to me, I think useEffect can be removed, no? 🤔