Skip to content

Commit

Permalink
refactor(dropdown)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeddnyx committed Jul 13, 2024
1 parent 90f607b commit 4353962
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 35 deletions.
3 changes: 0 additions & 3 deletions components/form/dropdown/dropdown-checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import DropDown from "@/components/ui/form/DropdownCheckbox";
import { OPTOONS } from "@/constants/index";

export default function Index() {
const [label, setLabel] = useState("");
const [form, setForm] = useState({
name: "",
});
Expand All @@ -19,8 +18,6 @@ export default function Index() {
<div className="grid gap-4">
<DropDown
options={OPTOONS}
value={label}
setValue={setLabel}
name="name"
onChange={handleChange}
/>
Expand Down
9 changes: 0 additions & 9 deletions components/form/dropdown/dropdown-variant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import DropDown from "@/components/ui/form/Dropdown";
import { OPTOONS } from "@/constants/index";

export default function Index() {
const [label, setLabel] = useState("");
const [form, setForm] = useState({
name: "",
});
Expand All @@ -22,8 +21,6 @@ export default function Index() {
label="Solid"
labelSide="left"
options={OPTOONS}
value={label}
setValue={setLabel}
name="name"
onChange={handleChange}
/>
Expand All @@ -32,8 +29,6 @@ export default function Index() {
labelSide="left"
variant="outline"
options={OPTOONS}
value={label}
setValue={setLabel}
name="name"
onChange={handleChange}
/>
Expand All @@ -42,8 +37,6 @@ export default function Index() {
labelSide="left"
variant="underline"
options={OPTOONS}
value={label}
setValue={setLabel}
name="name"
onChange={handleChange}
/>
Expand All @@ -52,8 +45,6 @@ export default function Index() {
labelSide="left"
variant="none"
options={OPTOONS}
value={label}
setValue={setLabel}
name="name"
onChange={handleChange}
/>
Expand Down
11 changes: 1 addition & 10 deletions components/form/dropdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import DropDown from "@/components/ui/form/Dropdown";
import { OPTOONS } from "@/constants/index";

export default function Index() {
const [label, setLabel] = useState("");
const [form, setForm] = useState({
name: "",
});
Expand All @@ -17,17 +16,9 @@ export default function Index() {

return (
<div className="grid gap-4">
<DropDown options={OPTOONS} name="name" onChange={handleChange} />
<DropDown
options={OPTOONS}
value={label}
setValue={setLabel}
name="name"
onChange={handleChange}
/>
<DropDown
options={OPTOONS}
value={label}
setValue={setLabel}
name="name"
onChange={handleChange}
error="Please select name!"
Expand Down
17 changes: 15 additions & 2 deletions components/form/input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,21 @@ export default function Index() {

return (
<div className="flex flex-col gap-8">
<Input name="name" label="default" value={form.name} onChange={handleChange} placeholder="Name" />
<Input name="name" label="Required" isRequired value={form.name} onChange={handleChange} placeholder="Name" />
<Input
name="name"
label="default"
value={form.name}
onChange={handleChange}
placeholder="Name"
/>
<Input
name="name"
label="Required"
isRequired
value={form.name}
onChange={handleChange}
placeholder="Name"
/>
<Input
name="name"
value={form.name}
Expand Down
9 changes: 4 additions & 5 deletions components/ui/form/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ export default function DropDown({
name,
label,
labelSide = "top",
value,
placeholder = "Select",
variant = "solid",
setValue,
onChange,
error,
isRequired = false,
Expand All @@ -24,10 +22,11 @@ export default function DropDown({
}: IDropDown) {
const [isOpen, setIsOpen] = useState(false);
const dropdownRef = useRef<HTMLDivElement>(null);
const [getLabel, setGetLabel] = useState("");

const handleSelect = (value: string, label: string) => {
onChange({ target: { name, value } });
setValue(label);
setGetLabel(label);
setIsOpen(false);
};

Expand Down Expand Up @@ -57,14 +56,14 @@ export default function DropDown({
className={cn(
`flex justify-between items-center gap-2 truncate w-full ${styles.inputDefault} ${inputClassName} ${variantInput[variant]}`,
{
"!text-light-900": !value,
"!text-light-900": !getLabel,
"border !border-red-500": error,
},
)}
disabled={isDisabled}
onClick={() => setIsOpen(!isOpen)}
>
{value || placeholder}
{getLabel || placeholder}
<IoIosArrowDown
className={`transition-transform ${isOpen ? "rotate-180" : ""}`}
/>
Expand Down
10 changes: 6 additions & 4 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ export const findPath = (path: string, sidebarItems: ISidebar[]) => {

// Flatten and filter out items with isMaintenance true
const flatSidebar: (ISidebar & { isParent?: boolean })[] =
sidebarItems.flatMap((item) =>
item.children
? item.children.filter(child => !child.isMaintenance)
: !item.isMaintenance ? [item] : []
sidebarItems.flatMap((item) =>
item.children
? item.children.filter((child) => !child.isMaintenance)
: !item.isMaintenance
? [item]
: [],
);

currentIndex = flatSidebar.findIndex((item) => item.href === path);
Expand Down
2 changes: 0 additions & 2 deletions types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ declare interface IDropDown {
name: string;
label?: string;
labelSide?: "left" | "top";
value: string;
setValue: any;
placeholder?: string;
onChange: any;
isRequired?: boolean;
Expand Down

0 comments on commit 4353962

Please sign in to comment.