From ad525aa347d0cca4853927f7d4b34950e3db5fde Mon Sep 17 00:00:00 2001 From: ogzhanolguncu Date: Fri, 6 Dec 2024 19:20:55 +0300 Subject: [PATCH 1/3] fix: add logic --- apps/dashboard/components/array-input.tsx | 126 +++++++++------------- 1 file changed, 53 insertions(+), 73 deletions(-) diff --git a/apps/dashboard/components/array-input.tsx b/apps/dashboard/components/array-input.tsx index 4481a853ff..50e4ffab31 100644 --- a/apps/dashboard/components/array-input.tsx +++ b/apps/dashboard/components/array-input.tsx @@ -1,11 +1,8 @@ -"use client"; - -import { CornerDownLeft, X } from "lucide-react"; -import * as React from "react"; - import { Badge } from "@/components/ui/badge"; -import { Command } from "@/components/ui/command"; -import { Command as CommandPrimitive } from "cmdk"; +import { CornerDownLeft, X } from "lucide-react"; +import { useCallback, useState } from "react"; +import { Button } from "./ui/button"; +import { Input } from "./ui/input"; type Props = { title?: string; @@ -15,85 +12,68 @@ type Props = { }; export const ArrayInput: React.FC = ({ title, placeholder, selected, setSelected }) => { - const inputRef = React.useRef(null); - const [inputValue, setInputValue] = React.useState(""); + console.log(selected); + const [items, setItems] = useState(selected); + const [inputValue, setInputValue] = useState(""); - const handleUnselect = (o: string) => { - setSelected(selected.filter((s) => s !== o)); + const handleUnselect = (item: string) => { + const newItems = items.filter((i) => i !== item); + setItems(newItems); + setSelected(newItems); }; - const handleKeyDown = React.useCallback( - (e: React.KeyboardEvent) => { - const input = inputRef.current; - if (input) { - if (e.key === "Delete" || e.key === "Backspace") { - if (input.value === "") { - setSelected(selected.slice(0, -1)); - } - } - if (e.key === "Enter" && input.value !== "") { - setSelected(Array.from(new Set([...selected, input.value]))); - setInputValue(""); - } - // This is not a default behaviour of the field - if (e.key === "Escape") { - input.blur(); - } + const handleAdd = () => { + if (inputValue.trim()) { + const newItems = Array.from(new Set([...items, inputValue.trim()])); + setItems(newItems); + setSelected(newItems); + setInputValue(""); + } + }; + + const handleKeyDown = useCallback( + (e: React.KeyboardEvent) => { + if (e.key === "Enter") { + e.preventDefault(); + handleAdd(); } }, - [selected, setSelected], + [inputValue, items], ); return ( - -
-
- {title ? {title}: : null} - {selected.map((o) => { - return ( - - {o} - - - ); - })} - {/* Avoid having the "Search" Icon */} - +
+ {title && {title}:} +
+ {items?.map((item) => ( + + {item} + + + ))} +
+
+
+
+ setInputValue(e.target.value)} + onKeyDown={handleKeyDown} placeholder={placeholder} className="flex-1 w-full bg-transparent outline-none placeholder:text-content-subtle" />
- +
- +
); }; From 97d45484087247e65025ef1630789631ba81ed34 Mon Sep 17 00:00:00 2001 From: Michael Silva Date: Fri, 6 Dec 2024 12:08:10 -0500 Subject: [PATCH 2/3] Style changes --- .../ratelimits/[namespaceId]/filters.tsx | 6 ++--- apps/dashboard/components/array-input.tsx | 22 ++++++++++++------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/apps/dashboard/app/(app)/ratelimits/[namespaceId]/filters.tsx b/apps/dashboard/app/(app)/ratelimits/[namespaceId]/filters.tsx index 1ffe29d7d6..1dc84658f8 100644 --- a/apps/dashboard/app/(app)/ratelimits/[namespaceId]/filters.tsx +++ b/apps/dashboard/app/(app)/ratelimits/[namespaceId]/filters.tsx @@ -46,9 +46,9 @@ export const Filters: React.FC<{ identifier?: boolean; interval?: boolean }> = ( ); return ( -
+
{props.identifier ? ( -
+
= (
) : null}{" "} {props.interval ? ( -
+