Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Merge branch 'fix-input-select-issues' of https://github.com/flornkm/dub into pr/736

Steven Teye12a7593b41c
diff --cc packages/ui/src/input-select.tsx
index 1d4709a7e6,35057c1e3a..e298852298
--- a/packages/ui/src/input-select.tsx
+++ b/packages/ui/src/input-select.tsx
@@@ -74,36 -73,50 +74,48 @@@ export function InputSelect(
const { isMobile } = useMediaQuery();
+ const CommandInput = memo(() => {
+ const isEmpty = useCommandState((state: any) => state.filtered.count === 0);
+
+ return (
+ <Command.Input
+ placeholder={inputAttrs?.placeholder || "Search..."}
+ // hacky focus on the input when the dropdown opens
+ // only do this on desktop
+ autoFocus={openCommandList}
+ onFocus={() => setOpenCommandList(true)}
+ value={inputValue}
+ onValueChange={setInputValue}
+ onKeyDown={(e) => {
+ if (e.key === "Escape") {
+ e.preventDefault();
+ setOpenCommandList(false);
+ // listen for cases where empty results and enter is pressed
+ } else if (e.key === "Enter" && isEmpty) {
+ setOpenCommandList(false);
+ // if it's a letter or a number and there's no meta key pressed, openCommandList dropdown
+ } else if (e.key.match(/^[a-z0-9]$/i) && !e.metaKey) {
+ setOpenCommandList(true);
+ }
+ }}
+ disabled={disabled}
+ className="block w-full truncate rounded-md border-none px-0 text-base text-gray-900 placeholder-gray-400 outline-none outline-0 transition-all duration-300 focus:ring-0 md:text-sm"
+ />
+ );
+ });
+
+ const CloseButton = () => (
+ <button
+ onClick={() => {
+ setSelectedItem(null);
+ setInputValue("");
+ }}
+ className="absolute inset-y-0 right-0 my-auto"
+ >
+ <X className="h-7 w-7 pr-3 text-gray-400" />
+ </button>
+ );
+
- const CommandInput = () => {
- const isEmpty = useCommandState((state: any) => state.filtered.count === 0);
- return (
- <>
- <Command.Input
- placeholder={inputAttrs?.placeholder || "Search..."}
- // hack to focus on the input when the dropdown opens
- autoFocus={openCommandList && !isMobile}
- onFocus={() => setOpenCommandList(true)}
- value={inputValue}
- onValueChange={(value) => {
- setInputValue(value);
- }}
- onKeyDown={(e) => {
- if (e.key === "Escape") {
- e.preventDefault();
- setOpenCommandList(false);
- // listen for cases where empty results and enter is pressed
- } else if (e.key === "Enter" && isEmpty) {
- setOpenCommandList(false);
- // if it's a letter or a number and there's no meta key pressed, openCommandList dropdown
- } else if (e.key.match(/^[a-z0-9]$/i) && !e.metaKey) {
- setOpenCommandList(true);
- }
- }}
- disabled={disabled}
- className="block w-full truncate rounded-md border-none px-0 text-base text-gray-900 placeholder-gray-400 outline-none outline-0 transition-all duration-300 focus:ring-0 md:text-sm"
- />
- </>
- );
- };
-
// renders a reusable list of items
const SelectorList = () =>
items.map((item) => (
@@@ -178,17 -191,8 +190,8 @@@
</div>
<div className="flex h-10 px-8">
<CommandInput />
- {inputValue && selectedItem?.value !== "" ? (
+ {inputValue || selectedItem?.value !== "" ? (
- <button
- onClick={(e) => {
- e.preventDefault();
- setSelectedItem(null);
- setInputValue("");
- }}
- className="absolute inset-y-0 right-0 my-auto"
- >
- <X className="h-7 w-7 pr-3 text-gray-400" />
- </button>
+ <CloseButton />
) : (
<ChevronDown className="absolute inset-y-0 right-0 my-auto h-7 w-7 pr-3 text-gray-400 transition-all" />
)}
@@@ -219,16 -223,8 +222,8 @@@
</div>
<div className="flex h-10 px-8">
<CommandInput />
- {inputValue && selectedItem?.value !== "" ? (
+ {inputValue || selectedItem?.value !== "" ? (
- <button
- onClick={() => {
- setSelectedItem(null);
- setInputValue("");
- }}
- className="absolute inset-y-0 right-0 my-auto"
- >
- <X className="h-7 w-7 pr-3 text-gray-400" />
- </button>
+ <CloseButton />
) : (
<ChevronDown className="absolute inset-y-0 right-0 my-auto h-7 w-7 rotate-180 pl-3 text-gray-400 transition-all" />
)}
@@@ -285,16 -281,8 +280,8 @@@
</div>
<div className="flex h-10 px-8">
<CommandInput />
- {inputValue && selectedItem?.value !== "" ? (
+ {inputValue || selectedItem?.value !== "" ? (
- <button
- onClick={() => {
- setSelectedItem(null);
- setInputValue("");
- }}
- className="absolute inset-y-0 right-0 my-auto"
- >
- <X className="h-7 w-7 pr-3 text-gray-400" />
- </button>
+ <CloseButton />
) : (
<ChevronDown
onClick={() => setOpenCommandList((prev) => !prev)}