-
Notifications
You must be signed in to change notification settings - Fork 21
internal(browser): Add Select Option action to Browser Test Editor #1164
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4e17c1c
internal(browser): Add Select Option action to Browser Test Editor
going-confetti f9e95a6
wip
going-confetti 9365eca
remove validation
going-confetti 4954d42
add ellipsis
going-confetti 17d35df
Merge remote-tracking branch 'origin/main' into internal/select-optio…
going-confetti 691b1a4
dedupe values
going-confetti 51ba3a7
add validation; remove filtering
going-confetti 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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
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
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
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
190 changes: 190 additions & 0 deletions
190
src/views/BrowserTestEditor/ActionForms/forms/SelectOptionValuesForm.tsx
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 |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| import { css } from '@emotion/react' | ||
| import { | ||
| Button, | ||
| Flex, | ||
| IconButton, | ||
| Popover, | ||
| ScrollArea, | ||
| Select, | ||
| TextField, | ||
| } from '@radix-ui/themes' | ||
| import { PlusIcon, XIcon } from 'lucide-react' | ||
| import { Fragment, useState } from 'react' | ||
|
|
||
| import { SelectOptions } from '@/components/Browser/SelectOptions' | ||
| import { FieldGroup } from '@/components/Form' | ||
| import { LocatorSelectOptionAction } from '@/main/runner/schema' | ||
|
|
||
| import { ValuePopoverBadge } from '../components' | ||
|
|
||
| type SelectOptionValue = LocatorSelectOptionAction['values'][number] | ||
|
|
||
| type MatchType = 'value' | 'label' | 'index' | ||
|
|
||
| function isNonEmptyValue(entry: SelectOptionValue): boolean { | ||
| if (entry.label !== undefined) return entry.label !== '' | ||
| if (entry.index !== undefined) return true | ||
| return (entry.value ?? '') !== '' | ||
| } | ||
|
|
||
| function getMatchType(entry: SelectOptionValue): MatchType { | ||
| if (entry.label !== undefined) return 'label' | ||
| if (entry.index !== undefined) return 'index' | ||
| return 'value' | ||
| } | ||
|
|
||
| function getMatchInput(entry: SelectOptionValue): string { | ||
| if (entry.label !== undefined) return entry.label | ||
| if (entry.index !== undefined) return String(entry.index) | ||
| return entry.value ?? '' | ||
| } | ||
|
|
||
| function toEntry(type: MatchType, input: string): SelectOptionValue { | ||
| switch (type) { | ||
| case 'value': | ||
| return { value: input } | ||
| case 'label': | ||
| return { label: input } | ||
| case 'index': | ||
| return { index: input === '' ? 0 : Number(input) } | ||
| } | ||
| } | ||
|
|
||
| interface SelectOptionValuesFormProps { | ||
| values: SelectOptionValue[] | ||
| onChange: (values: SelectOptionValue[]) => void | ||
| } | ||
|
|
||
| export function SelectOptionValuesForm({ | ||
| values, | ||
| onChange, | ||
| }: SelectOptionValuesFormProps) { | ||
| const [isPopoverOpen, setIsPopoverOpen] = useState(false) | ||
|
|
||
| const handleChangeType = (index: number, type: MatchType) => { | ||
| const current = values[index] | ||
| if (!current) return | ||
| const input = getMatchInput(current) | ||
| const next = [...values] | ||
| next[index] = toEntry(type, input) | ||
| onChange(next) | ||
| } | ||
|
|
||
| const handleChangeInput = (index: number, input: string) => { | ||
| const current = values[index] | ||
| if (!current) return | ||
| const type = getMatchType(current) | ||
| const next = [...values] | ||
| next[index] = toEntry(type, input) | ||
| onChange(next) | ||
| } | ||
|
|
||
| const handleAdd = () => { | ||
| onChange([...values, { value: '' }]) | ||
| } | ||
|
|
||
| const handleRemove = (index: number) => { | ||
| onChange(values.filter((_, i) => i !== index)) | ||
| } | ||
|
|
||
| const nonEmptyValues = values.filter(isNonEmptyValue) | ||
|
|
||
| return ( | ||
| <Popover.Root open={isPopoverOpen} onOpenChange={setIsPopoverOpen}> | ||
| <Popover.Trigger> | ||
| <ValuePopoverBadge | ||
| displayValue={ | ||
| nonEmptyValues.length === 0 ? ( | ||
| 'option(s)' | ||
| ) : ( | ||
| <SelectOptions options={nonEmptyValues} /> | ||
| ) | ||
| } | ||
| error={null} | ||
| /> | ||
| </Popover.Trigger> | ||
| <Popover.Content align="start" size="1" width="360px"> | ||
| <FieldGroup | ||
| name="options" | ||
| label="Options to select" | ||
| labelSize="1" | ||
| mb="0" | ||
| > | ||
| <Flex direction="column" gap="2"> | ||
| <ScrollArea | ||
| css={css` | ||
| max-height: 200px; | ||
| `} | ||
| > | ||
| <Flex direction="column" gap="2" pr="3"> | ||
| {values.map((entry, index) => ( | ||
| <Fragment key={index}> | ||
| <Flex gap="2" align="center"> | ||
| <Select.Root | ||
| size="1" | ||
| value={getMatchType(entry)} | ||
| onValueChange={(type: MatchType) => | ||
| handleChangeType(index, type) | ||
| } | ||
| > | ||
| <Select.Trigger | ||
| css={css` | ||
| min-width: 80px; | ||
| `} | ||
| /> | ||
| <Select.Content> | ||
| <Select.Item value="value">Value</Select.Item> | ||
| <Select.Item value="label">Label</Select.Item> | ||
| <Select.Item value="index">Index</Select.Item> | ||
| </Select.Content> | ||
| </Select.Root> | ||
| <TextField.Root | ||
| size="1" | ||
| type={ | ||
| getMatchType(entry) === 'index' ? 'number' : 'text' | ||
| } | ||
| {...(getMatchType(entry) === 'index' ? { min: 0 } : {})} | ||
| value={getMatchInput(entry)} | ||
| onChange={(e) => | ||
| handleChangeInput(index, e.target.value) | ||
| } | ||
| placeholder={ | ||
| getMatchType(entry) === 'index' | ||
| ? '0' | ||
| : 'Enter text...' | ||
| } | ||
| css={css` | ||
| flex: 1; | ||
| `} | ||
| /> | ||
| <IconButton | ||
| aria-label="Remove option" | ||
| size="1" | ||
| variant="ghost" | ||
| color="gray" | ||
| onClick={() => handleRemove(index)} | ||
| > | ||
| <XIcon /> | ||
| </IconButton> | ||
| </Flex> | ||
| </Fragment> | ||
| ))} | ||
| </Flex> | ||
| </ScrollArea> | ||
| <Button | ||
| size="1" | ||
| variant="ghost" | ||
| color="gray" | ||
| onClick={handleAdd} | ||
| css={css` | ||
| align-self: flex-start; | ||
| `} | ||
| > | ||
| <PlusIcon /> Add option | ||
| </Button> | ||
| </Flex> | ||
| </FieldGroup> | ||
| </Popover.Content> | ||
| </Popover.Root> | ||
| ) | ||
| } |
48 changes: 48 additions & 0 deletions
48
src/views/BrowserTestEditor/Actions/SelectOptionAction/SelectOptionActionBody.tsx
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 |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { Grid } from '@radix-ui/themes' | ||
|
|
||
| import { LocatorSelectOptionAction } from '@/main/runner/schema' | ||
|
|
||
| import { LocatorForm } from '../../ActionForms/forms/LocatorForm' | ||
| import { SelectOptionValuesForm } from '../../ActionForms/forms/SelectOptionValuesForm' | ||
| import { WithEditorMetadata } from '../../types' | ||
|
|
||
| interface SelectOptionActionBodyProps { | ||
| action: WithEditorMetadata<LocatorSelectOptionAction> | ||
| onChange: (action: WithEditorMetadata<LocatorSelectOptionAction>) => void | ||
| } | ||
|
|
||
| export function SelectOptionActionBody({ | ||
| action, | ||
| onChange, | ||
| }: SelectOptionActionBodyProps) { | ||
| const handleChangeLocator = ( | ||
| locator: WithEditorMetadata<LocatorSelectOptionAction>['locator'] | ||
| ) => { | ||
| onChange({ ...action, locator }) | ||
| } | ||
|
|
||
| const handleChangeValues = (values: LocatorSelectOptionAction['values']) => { | ||
| onChange({ ...action, values }) | ||
| } | ||
|
|
||
| return ( | ||
| <Grid | ||
| columns="max-content minmax(0, max-content) max-content minmax(0, max-content) 1fr" | ||
| gap="2" | ||
| align="center" | ||
| width="100%" | ||
| > | ||
| Select | ||
| <SelectOptionValuesForm | ||
| values={action.values} | ||
| onChange={handleChangeValues} | ||
| /> | ||
| in | ||
|
Comment on lines
+35
to
+40
Collaborator
Author
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. This order is different from other actions, but feels more natural/easier to read IMO |
||
| <LocatorForm | ||
| state={action.locator} | ||
| onChange={handleChangeLocator} | ||
| suggestedRoles={['combobox', 'listbox', 'menu', 'radiogroup']} | ||
| /> | ||
| </Grid> | ||
| ) | ||
| } | ||
1 change: 1 addition & 0 deletions
1
src/views/BrowserTestEditor/Actions/SelectOptionAction/index.ts
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './SelectOptionActionBody' |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
valueis allowed to be an empty string according to the HTML specification (butlabelis not).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.
Maybe this is a dumb optimization, but we could dedupe the values for the user:
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.
No, I think it's a good suggestion 👍