-
Notifications
You must be signed in to change notification settings - Fork 8
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
Replace Text Fields with Dropdowns for Enum Parameters in API Sandbox UI #306
Open
arian0zen
wants to merge
15
commits into
zuplo:main
Choose a base branch
from
arian0zen:ari/enum-string-dropdown
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
15 commits
Select commit
Hold shift + click to select a range
8e8dfa4
fix: prevent path.join from creating malformed URL for client entry path
arian0zen 0ff0ffd
fix: normalize path separators in proxied entry client path
arian0zen 75bacbe
fix: prevent path.join from creating malformed URL for client entry path
arian0zen 07189e1
fix: normalize path separators in proxied entry client path
arian0zen 950def5
Merge branch 'ari/zudoku-1' of https://github.com/arian0zen/zudoku-ar…
arian0zen f4b796f
Refactor PlaygroundDialogWrapper and PlaygroundForm components
arian0zen 84e300f
Refactor OpenAPI JSON schema for pet search endpoint
arian0zen aefd30a
Merge branch 'main' of https://github.com/arian0zen/zudoku-ari into a…
arian0zen 5a9ae14
Refactor dev-server.ts to use the 'path' module for importing
arian0zen d422f7a
Multi-select dropdown for array type enums!
arian0zen ed3ff6e
Refactor OpenAPI JSON schema for pet search endpoint
arian0zen 65f95c6
Merge branch 'main' into ari/enum-string-dropdown
dan-lee 50e66ea
Merge branch 'main' into ari/enum-string-dropdown
mosch e1b63a7
Merge branch 'main' into ari/enum-string-dropdown
dan-lee ad34be8
Merge branch 'main' into ari/enum-string-dropdown
mosch 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 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,131 @@ | ||
import * as SelectPrimitive from "@radix-ui/react-select"; | ||
import { Check, ChevronDown } from "lucide-react"; | ||
import * as React from "react"; | ||
import { cn } from "../util/cn.js"; | ||
|
||
import { | ||
SelectContent, | ||
SelectGroup, | ||
SelectScrollDownButton, | ||
SelectScrollUpButton, | ||
} from "./Select.js"; | ||
|
||
type MultiSelectProps = { | ||
options: string[]; | ||
value?: string; // this value will only be used to clear the selected items | ||
onValueChange?: (value: string[]) => void; | ||
placeholder?: string; | ||
className?: string; | ||
}; | ||
|
||
const MultiSelectTrigger = React.forwardRef< | ||
React.ElementRef<typeof SelectPrimitive.Trigger>, | ||
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Trigger> & { | ||
selectedItems: string[]; | ||
placeholder?: string; | ||
} | ||
>( | ||
( | ||
{ className, selectedItems, placeholder = "Select options", ...props }, | ||
ref, | ||
) => ( | ||
<SelectPrimitive.Trigger | ||
ref={ref} | ||
className={cn( | ||
"flex h-10 w-full items-center justify-between rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50", | ||
className, | ||
)} | ||
{...props} | ||
> | ||
<span | ||
className={`truncate ${selectedItems.length === 0 ? "text-muted-foreground" : ""}`} | ||
> | ||
{selectedItems.length > 0 ? selectedItems.join(", ") : placeholder} | ||
</span> | ||
<SelectPrimitive.Icon asChild> | ||
<ChevronDown className="flex-shrink-0 h-4 w-4 opacity-50" /> | ||
</SelectPrimitive.Icon> | ||
</SelectPrimitive.Trigger> | ||
), | ||
); | ||
MultiSelectTrigger.displayName = SelectPrimitive.Trigger.displayName; | ||
|
||
const MultiSelectItem = React.forwardRef< | ||
React.ElementRef<typeof SelectPrimitive.Item>, | ||
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Item> & { | ||
value: string; | ||
selectedItems: string[]; | ||
} | ||
>(({ className, children, value, selectedItems, ...props }, ref) => ( | ||
<SelectPrimitive.Item | ||
ref={ref} | ||
value={value} | ||
className={cn( | ||
"relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-sm outline-none focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50 my-1", | ||
className, | ||
`${selectedItems.includes(value) && "bg-accent text-accent-foreground"}`, | ||
)} | ||
{...props} | ||
> | ||
<span className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center"> | ||
{selectedItems.includes(value) && <Check className="h-4 w-4" />} | ||
</span> | ||
<SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText> | ||
</SelectPrimitive.Item> | ||
)); | ||
MultiSelectItem.displayName = SelectPrimitive.Item.displayName; | ||
|
||
const MultiSelect: React.FC<MultiSelectProps> = ({ | ||
options, | ||
value, | ||
onValueChange, | ||
placeholder, | ||
className, | ||
}) => { | ||
const [selectedItems, setSelectedItems] = React.useState<string[]>([]); | ||
|
||
React.useEffect(() => { | ||
if (value === "") setSelectedItems([]); | ||
}, [value]); | ||
|
||
const handleValueChange = (newValue: string) => { | ||
if (newValue === "") return; // Prevent adding empty string | ||
|
||
const updatedValues = selectedItems.includes(newValue) | ||
? selectedItems.filter((item) => item !== newValue) // Remove if selected | ||
: [...selectedItems, newValue]; // Add if not selected | ||
|
||
setSelectedItems(updatedValues); | ||
if (onValueChange) { | ||
onValueChange(updatedValues); | ||
} | ||
}; | ||
|
||
return ( | ||
<SelectPrimitive.Root value={value} onValueChange={handleValueChange}> | ||
<MultiSelectTrigger | ||
selectedItems={selectedItems} | ||
placeholder={placeholder} | ||
/> | ||
<SelectContent> | ||
<SelectScrollUpButton /> | ||
<SelectPrimitive.Viewport className="p-1"> | ||
<SelectGroup> | ||
{options.map((option) => ( | ||
<MultiSelectItem | ||
key={option} | ||
value={option} | ||
selectedItems={selectedItems} | ||
> | ||
{option} | ||
</MultiSelectItem> | ||
))} | ||
</SelectGroup> | ||
</SelectPrimitive.Viewport> | ||
<SelectScrollDownButton /> | ||
</SelectContent> | ||
</SelectPrimitive.Root> | ||
); | ||
}; | ||
|
||
export default MultiSelect; |
This file contains 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 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 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 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 | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -5,6 +5,14 @@ import { | |||||||||||||||||||||||||||
useFieldArray, | ||||||||||||||||||||||||||||
useFormContext, | ||||||||||||||||||||||||||||
} from "react-hook-form"; | ||||||||||||||||||||||||||||
import MultiSelect from "../../../components/MultiSelect.js"; | ||||||||||||||||||||||||||||
import { | ||||||||||||||||||||||||||||
Select, | ||||||||||||||||||||||||||||
SelectContent, | ||||||||||||||||||||||||||||
SelectItem, | ||||||||||||||||||||||||||||
SelectTrigger, | ||||||||||||||||||||||||||||
SelectValue, | ||||||||||||||||||||||||||||
} from "../../../components/Select.js"; | ||||||||||||||||||||||||||||
import { Button } from "../../../ui/Button.js"; | ||||||||||||||||||||||||||||
import { Input } from "../../../ui/Input.js"; | ||||||||||||||||||||||||||||
import { cn } from "../../../util/cn.js"; | ||||||||||||||||||||||||||||
|
@@ -82,19 +90,67 @@ export const QueryParams = ({ | |||||||||||||||||||||||||||
<div className="flex justify-between items-center"> | ||||||||||||||||||||||||||||
<Controller | ||||||||||||||||||||||||||||
control={control} | ||||||||||||||||||||||||||||
render={({ field }) => ( | ||||||||||||||||||||||||||||
<Input | ||||||||||||||||||||||||||||
{...field} | ||||||||||||||||||||||||||||
onChange={(e) => { | ||||||||||||||||||||||||||||
field.onChange(e.target.value); | ||||||||||||||||||||||||||||
if (e.target.value.length > 0) { | ||||||||||||||||||||||||||||
form.setValue(`queryParams.${i}.active`, true); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
}} | ||||||||||||||||||||||||||||
placeholder="Enter value" | ||||||||||||||||||||||||||||
className="w-full border-0 shadow-none text-xs font-mono" | ||||||||||||||||||||||||||||
/> | ||||||||||||||||||||||||||||
)} | ||||||||||||||||||||||||||||
render={({ field }) => { | ||||||||||||||||||||||||||||
const hasEnum = | ||||||||||||||||||||||||||||
queryParams[i].enum && queryParams[i].enum.length > 0; | ||||||||||||||||||||||||||||
const typeOfField = queryParams[i].type; | ||||||||||||||||||||||||||||
return hasEnum ? ( | ||||||||||||||||||||||||||||
typeOfField === "array" ? ( | ||||||||||||||||||||||||||||
<MultiSelect | ||||||||||||||||||||||||||||
options={queryParams[i].enum ?? []} | ||||||||||||||||||||||||||||
onValueChange={(value) => { | ||||||||||||||||||||||||||||
if ( | ||||||||||||||||||||||||||||
Array.isArray(value) && | ||||||||||||||||||||||||||||
value.length === 0 | ||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||
field.onChange(""); | ||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||
field.onChange(value); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
}} | ||||||||||||||||||||||||||||
placeholder="Choose options" | ||||||||||||||||||||||||||||
value={ | ||||||||||||||||||||||||||||
Array.isArray(field.value) | ||||||||||||||||||||||||||||
? field.value.join(",") | ||||||||||||||||||||||||||||
: field.value | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
/> | ||||||||||||||||||||||||||||
) : ( | ||||||||||||||||||||||||||||
<Select | ||||||||||||||||||||||||||||
onValueChange={(value) => field.onChange(value)} | ||||||||||||||||||||||||||||
value={field.value as string} | ||||||||||||||||||||||||||||
defaultValue={field.value as string} | ||||||||||||||||||||||||||||
> | ||||||||||||||||||||||||||||
<SelectTrigger | ||||||||||||||||||||||||||||
className="w-full flex" | ||||||||||||||||||||||||||||
placeholder="Choose an option" | ||||||||||||||||||||||||||||
value={field.value} | ||||||||||||||||||||||||||||
> | ||||||||||||||||||||||||||||
<SelectValue /> | ||||||||||||||||||||||||||||
</SelectTrigger> | ||||||||||||||||||||||||||||
Comment on lines
+124
to
+130
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. Let's just use
Suggested change
|
||||||||||||||||||||||||||||
<SelectContent align="center"> | ||||||||||||||||||||||||||||
{queryParams[i].enum?.map((enumValue) => ( | ||||||||||||||||||||||||||||
<SelectItem key={enumValue} value={enumValue}> | ||||||||||||||||||||||||||||
{enumValue} | ||||||||||||||||||||||||||||
</SelectItem> | ||||||||||||||||||||||||||||
))} | ||||||||||||||||||||||||||||
</SelectContent> | ||||||||||||||||||||||||||||
</Select> | ||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||
) : ( | ||||||||||||||||||||||||||||
<Input | ||||||||||||||||||||||||||||
{...field} | ||||||||||||||||||||||||||||
onChange={(e) => { | ||||||||||||||||||||||||||||
field.onChange(e.target.value); | ||||||||||||||||||||||||||||
if (e.target.value.length > 0) { | ||||||||||||||||||||||||||||
form.setValue(`queryParams.${i}.active`, true); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
}} | ||||||||||||||||||||||||||||
placeholder="Enter value" | ||||||||||||||||||||||||||||
className="w-full border-0 shadow-none text-xs font-mono" | ||||||||||||||||||||||||||||
/> | ||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||
}} | ||||||||||||||||||||||||||||
name={`queryParams.${i}.value`} | ||||||||||||||||||||||||||||
/> | ||||||||||||||||||||||||||||
<Controller | ||||||||||||||||||||||||||||
|
This file contains 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 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
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.
I think we should leave the replacement for
%20
to+
in.