add: profile edit page

This commit is contained in:
Mahyar Khanbolooki
2025-07-17 17:15:17 +03:30
parent ac5eb68444
commit 77505d5098
11 changed files with 594 additions and 234 deletions
+138 -76
View File
@@ -1,91 +1,153 @@
"use client"
import * as React from "react"
import { Check } from "lucide-react"
import { cn } from "@/lib/utils"
import { Button } from "@/components/ui/button"
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from "@/components/ui/command"
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover"
import ArrowDownIcon from "../icons/ArrowDownIcon"
import React, { useState, useEffect, useRef } from 'react';
import ArrowDownIcon from '../icons/ArrowDownIcon';
import { motion, Variants } from 'framer-motion';
import { SearchNormal } from 'iconsax-react';
export interface DropdownOption {
id: string;
label?: string;
title: string;
label?: string;
}
type Props = {
title: string;
options: Array<DropdownOption>;
value: string;
setValue: React.Dispatch<React.SetStateAction<string>>
expanded?: boolean;
selectedId: string;
searchable?: boolean;
onSelectionChange: (e: React.MouseEvent<HTMLDivElement, MouseEvent>, index: number) => void,
} & React.HTMLAttributes<HTMLDivElement>
export function Combobox({ options, value, title, setValue, }: Props) {
const [open, setOpen] = React.useState(false)
function ComboBox({ title, options, expanded, selectedId, searchable = true, onSelectionChange, ...props }: Props) {
const [expand, setExpand] = useState(expanded);
const [searchValue, setSearchValue] = useState('');
const boxRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (boxRef.current && !boxRef.current.contains(event.target as Node)) {
setExpand(false);
}
};
if (expand) {
document.addEventListener('mousedown', handleClickOutside);
}
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [expand]);
const toggleExpand = () => {
setExpand((prev) => !prev);
setSearchValue('');
};
const searchChanged = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearchValue(e.target.value);
};
const setSelection = (e: React.MouseEvent<HTMLDivElement, MouseEvent>, index: number) => {
e.stopPropagation(); // prevent toggle
onSelectionChange(e, index);
setExpand(false);
};
const variants: Variants = {
collapse: {
opacity: 0,
scale: 0.95,
pointerEvents: 'none', // disable clicks
transition: {
duration: 0.1,
ease: 'easeInOut'
}
},
expand: {
opacity: 1,
scale: 1,
pointerEvents: 'auto', // re-enable clicks
transition: {
duration: 0.1,
ease: 'easeInOut'
}
}
};
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
variant="outline"
role="combobox"
aria-expanded={open}
className="flex font-normal w-full h-11 items-center justify-between gap-2 px-3 py-4 relative bg-white/29 rounded-xl border border-solid border-neutral-200 cursor-pointer focus:outline-none focus:ring-2 focus:ring-black"
<div ref={boxRef} className={`relative ${props.className ?? ''}`} {...props}>
<div
className="flex w-full h-11 items-center justify-end gap-2 px-3 py-4 relative bg-white/29 rounded-xl border border-solid border-neutral-200 cursor-pointer"
tabIndex={0}
onClick={toggleExpand}
role="combobox"
aria-controls=''
aria-expanded={expand}
aria-haspopup="listbox"
aria-label={title}
>
<label
htmlFor="content-select"
className="pointer-events-none inline-flex flex-col items-end justify-center px-1 py-0 absolute -top-6 right-0 z-[2]"
>
{value
? options.find((option) => option.title === value)?.label
: "انتخاب کنید"}
<ArrowDownIcon />
<label
htmlFor='content-select'
className='pointer-events-none inline-flex flex-col items-end justify-center px-1 py-0 absolute -top-6 right-0 z-[2]'>
<span className='relative w-fit mt-[-1px] text-xs tracking-[0] leading-4 whitespace-nowrap'>
{title}
</span>
</label>
</Button>
</PopoverTrigger>
<PopoverContent className="w-full p-0">
<Command>
<CommandInput placeholder="جستجو" className="h-9" />
<CommandList>
<CommandEmpty>نتیجه ای یافت نشد</CommandEmpty>
<CommandGroup>
{options.map((option) => (
<CommandItem
key={option.id}
value={option.title}
onSelect={(currentValue) => {
setValue(currentValue === value ? "" : currentValue)
setOpen(false)
}}
>
<Check
className={cn(
"",
value === option.title ? "opacity-100" : "opacity-0"
)}
/>
{option.label}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
)
<span className="relative w-fit mt-[-1px] text-xs tracking-[0] leading-4 whitespace-nowrap">
{title}
</span>
</label>
<div className="w-full text-sm2">
{options.find((x) => x.id === selectedId)?.title ?? ''}
</div>
<ArrowDownIcon data-expand={expand} className="transition-all duration-200 data-[expand=true]:rotate-x-180" />
</div>
<motion.div
data-expand={expand}
aria-hidden={!expand}
className="absolute top-full left-0 w-full mt-1 bg-white rounded-xl outline outline-solid outline-neutral-200 shadow-lg z-10"
role="listbox"
aria-label={`${title} options`}
onClick={(e) => e.stopPropagation()}
initial="collapse"
animate={expand ? "expand" : "collapse"}
variants={variants}
>
{searchable &&
<div className="w-full flex gap-2 border-b px-3 items-center">
<SearchNormal size={16} className='stroke-gray-400' />
<input
placeholder="جستجو ..."
className="w-full outline-none text-sm2 pb-2 pt-3"
onChange={searchChanged}
value={searchValue}
/>
</div>
}
<div className='p-1'>
{options
.filter((v) => v.title?.includes(searchValue))
.map((v, i) => (
<div
onClick={(e) => setSelection(e, i)}
key={v.id}
data-selected={v.id === selectedId}
className="flex items-center justify-end px-2 py-1.5 cursor-pointer hover:bg-gray-50 rounded-md last:rounded-b-xl"
tabIndex={0}
role="option"
aria-selected
>
<span className="text-sm2 tracking-[0.13px] leading-5 w-full">{v.title}</span>
</div>
))}
</div>
</motion.div>
</div>
);
}
export default ComboBox
-150
View File
@@ -1,150 +0,0 @@
import React, { useState, useEffect, useRef } from 'react';
import ArrowDownIcon from '../icons/ArrowDownIcon';
import { motion, Variants } from 'framer-motion';
import { SearchNormal } from 'iconsax-react';
export interface DropdownOption {
id: string;
title: string;
label?: string;
}
type Props = {
title: string;
options: Array<DropdownOption>;
expanded?: boolean;
selectedId: string;
onSelectionChange: (e: React.MouseEvent<HTMLDivElement, MouseEvent>, index: number) => void,
} & React.HTMLAttributes<HTMLDivElement>
function SearchComboBox({ title, options, expanded, selectedId, onSelectionChange, ...props }: Props) {
const [expand, setExpand] = useState(expanded);
const [searchValue, setSearchValue] = useState('');
const boxRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (boxRef.current && !boxRef.current.contains(event.target as Node)) {
setExpand(false);
}
};
if (expand) {
document.addEventListener('mousedown', handleClickOutside);
}
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [expand]);
const toggleExpand = () => {
setExpand((prev) => !prev);
setSearchValue('');
};
const searchChanged = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearchValue(e.target.value);
};
const setSelection = (e: React.MouseEvent<HTMLDivElement, MouseEvent>, index: number) => {
e.stopPropagation(); // prevent toggle
onSelectionChange(e, index);
setExpand(false);
};
const variants: Variants = {
collapse: {
opacity: 0,
scale: 0.95,
pointerEvents: 'none', // disable clicks
transition: {
duration: 0.1,
ease: 'easeInOut'
}
},
expand: {
opacity: 1,
scale: 1,
pointerEvents: 'auto', // re-enable clicks
transition: {
duration: 0.1,
ease: 'easeInOut'
}
}
};
return (
<div ref={boxRef} className={`relative ${props.className ?? ''}`} {...props}>
<div
className="flex w-full h-11 items-center justify-end gap-2 px-3 py-4 relative bg-white/29 rounded-xl border border-solid border-neutral-200 cursor-pointer"
tabIndex={0}
onClick={toggleExpand}
role="combobox"
aria-controls=''
aria-expanded={expand}
aria-haspopup="listbox"
aria-label={title}
>
<label
htmlFor="content-select"
className="pointer-events-none inline-flex flex-col items-end justify-center px-1 py-0 absolute -top-6 right-0 z-[2]"
>
<span className="relative w-fit mt-[-1px] text-xs tracking-[0] leading-4 whitespace-nowrap">
{title}
</span>
</label>
<div className="w-full text-sm2">
{options.find((x) => x.id === selectedId)?.title ?? ''}
</div>
<ArrowDownIcon data-expand={expand} className="transition-all duration-200 data-[expand=true]:rotate-x-180" />
</div>
<motion.div
data-expand={expand}
aria-hidden={!expand}
className="absolute top-full left-0 w-full mt-1 bg-white rounded-xl outline outline-solid outline-neutral-200 shadow-lg z-10"
role="listbox"
aria-label={`${title} options`}
onClick={(e) => e.stopPropagation()}
initial="collapse"
animate={expand ? "expand" : "collapse"}
variants={variants}
>
<div className="w-full flex gap-2 border-b px-3 items-center">
<SearchNormal size={16} className='stroke-gray-400' />
<input
placeholder="جستجو ..."
className="w-full outline-none text-sm2 pb-2 pt-3"
onChange={searchChanged}
value={searchValue}
/>
</div>
<div className='p-1'>
{options
.filter((v) => v.title?.includes(searchValue))
.map((v, i) => (
<div
onClick={(e) => setSelection(e, i)}
key={v.id}
data-selected={v.id === selectedId}
className="flex items-center justify-end px-2 py-1.5 cursor-pointer hover:bg-gray-50 rounded-md last:rounded-b-xl"
tabIndex={0}
role="option"
aria-selected
>
<span className="text-sm2 tracking-[0.13px] leading-5 w-full">{v.title}</span>
</div>
))}
</div>
</motion.div>
</div>
);
}
export default SearchComboBox
+226
View File
@@ -0,0 +1,226 @@
"use client"
import * as React from "react"
import {
ChevronDownIcon,
ChevronLeftIcon,
ChevronRightIcon,
} from "lucide-react"
import { DayButton, getDefaultClassNames } from "react-day-picker"
import { DayPicker, faIR } from "react-day-picker/persian"
import { cn } from "@/lib/utils"
import { Button, buttonVariants } from "@/components/ui/button"
export function CalendarHijri() {
const [date, setDate] = React.useState<Date | undefined>(
new Date(2025, 5, 12)
)
return (
<Calendar
mode="single"
defaultMonth={date}
selected={date}
onSelect={setDate}
className="rounded-lg border shadow-sm"
/>
)
}
// ----------------------------------------------------------------------------
// The code below is for this example only.
// For your own calendar, you would edit the calendar.tsx component directly.
// ----------------------------------------------------------------------------
export default function Calendar({
className,
classNames,
showOutsideDays = true,
captionLayout = "label",
buttonVariant = "ghost",
components,
...props
}: React.ComponentProps<typeof DayPicker> & {
buttonVariant?: React.ComponentProps<typeof Button>["variant"]
}) {
const defaultClassNames = getDefaultClassNames()
return (
<DayPicker
locale={faIR}
numerals="arabext"
showOutsideDays={showOutsideDays}
className={cn(
"bg-background group/calendar p-3 [--cell-size:--spacing(8)] [[data-slot=card-content]_&]:bg-transparent [[data-slot=popover-content]_&]:bg-transparent",
String.raw`rtl:**:[.rdp-button\_next>svg]:rotate-180`,
String.raw`rtl:**:[.rdp-button\_previous>svg]:rotate-180`,
className
)}
captionLayout={captionLayout}
classNames={{
root: cn("w-fit", defaultClassNames.root),
months: cn(
"flex gap-4 flex-col md:flex-row relative",
defaultClassNames.months
),
month: cn("flex flex-col w-full gap-4", defaultClassNames.month),
nav: cn(
"flex items-center gap-1 w-full absolute top-0 inset-x-0 justify-between",
defaultClassNames.nav
),
button_previous: cn(
buttonVariants({ variant: buttonVariant }),
"size-(--cell-size) aria-disabled:opacity-50 p-0 select-none",
defaultClassNames.button_previous
),
button_next: cn(
buttonVariants({ variant: buttonVariant }),
"size-(--cell-size) aria-disabled:opacity-50 p-0 select-none",
defaultClassNames.button_next
),
month_caption: cn(
"flex items-center justify-center h-(--cell-size) w-full px-(--cell-size)",
defaultClassNames.month_caption
),
dropdowns: cn(
"w-full flex items-center text-sm font-medium justify-center px-3 h-(--cell-size) gap-1.5",
defaultClassNames.dropdowns
),
dropdown_root: cn(
"relative has-focus:border-ring border border-input shadow-xs has-focus:ring-ring/50 has-focus:ring-[3px] rounded-md",
defaultClassNames.dropdown_root
),
dropdown: cn("absolute inset-0 opacity-0 ", defaultClassNames.dropdown),
caption_label: cn(
"select-none font-medium",
captionLayout === "label"
? "text-sm"
: "rounded-md pl-2 pr-1 flex items-center gap-1 text-sm h-8 [&>svg]:text-muted-foreground [&>svg]:size-3.5",
defaultClassNames.caption_label
),
table: "w-full border-collapse",
weekdays: cn("flex", defaultClassNames.weekdays),
weekday: cn(
"text-muted-foreground px-2 rounded-md flex-1 font-normal text-[0.8rem] select-none",
defaultClassNames.weekday
),
week: cn("flex w-full mt-2", defaultClassNames.week),
week_number_header: cn(
"select-none w-(--cell-size)",
defaultClassNames.week_number_header
),
week_number: cn(
"text-[0.8rem] select-none text-muted-foreground",
defaultClassNames.week_number
),
day: cn(
"relative w-full h-full p-0 text-center [&:first-child[data-selected=true]_button]:rounded-l-md [&:last-child[data-selected=true]_button]:rounded-r-md group/day aspect-square select-none",
defaultClassNames.day
),
range_start: cn(
"rounded-l-md bg-accent",
defaultClassNames.range_start
),
range_middle: cn("rounded-none", defaultClassNames.range_middle),
range_end: cn("rounded-r-md bg-accent", defaultClassNames.range_end),
today: cn(
"bg-accent text-accent-foreground rounded-md data-[selected=true]:rounded-none",
defaultClassNames.today
),
outside: cn(
"text-disabled aria-selected:text-muted-foreground",
defaultClassNames.outside
),
disabled: cn(
"bg-red-500 opacity-50",
defaultClassNames.disabled
),
hidden: cn("invisible", defaultClassNames.hidden),
...classNames,
}}
components={{
Root: ({ className, rootRef, ...props }) => {
return (
<div
data-slot="calendar"
ref={rootRef}
className={cn(className)}
{...props}
/>
)
},
Chevron: ({ className, orientation, ...props }) => {
if (orientation === "left") {
return (
<ChevronLeftIcon className={cn("size-4", className)} {...props} />
)
}
if (orientation === "right") {
return (
<ChevronRightIcon
className={cn("size-4", className)}
{...props}
/>
)
}
return (
<ChevronDownIcon className={cn("size-4", className)} {...props} />
)
},
DayButton: CalendarDayButton,
WeekNumber: ({ children, ...props }) => {
return (
<td {...props}>
<div className="flex size-(--cell-size) items-center justify-center text-center">
{children}
</div>
</td>
)
},
...components,
}}
{...props}
/>
)
}
function CalendarDayButton({
className,
day,
modifiers,
...props
}: React.ComponentProps<typeof DayButton>) {
const defaultClassNames = getDefaultClassNames()
const ref = React.useRef<HTMLButtonElement>(null)
React.useEffect(() => {
if (modifiers.focused) ref.current?.focus()
}, [modifiers.focused])
return (
<Button
ref={ref}
variant="ghost"
size="icon"
data-day={day.date.toLocaleDateString()}
data-selected-single={
modifiers.selected &&
!modifiers.range_start &&
!modifiers.range_end &&
!modifiers.range_middle
}
data-range-start={modifiers.range_start}
data-range-end={modifiers.range_end}
data-range-middle={modifiers.range_middle}
className={cn(
"data-[selected-single=true]:bg-primary data-[selected-single=true]:text-primary-foreground data-[range-middle=true]:bg-accent data-[range-middle=true]:text-accent-foreground data-[range-start=true]:bg-primary data-[range-start=true]:text-primary-foreground data-[range-end=true]:bg-primary data-[range-end=true]:text-primary-foreground group-data-[focused=true]/day:border-ring group-data-[focused=true]/day:ring-ring/50 dark:hover:text-accent-foreground flex aspect-square size-auto w-full min-w-(--cell-size) flex-col gap-1 leading-none font-normal group-data-[focused=true]/day:relative group-data-[focused=true]/day:z-10 group-data-[focused=true]/day:ring-[3px] data-[range-end=true]:rounded-md data-[range-end=true]:rounded-r-md data-[range-middle=true]:rounded-none data-[range-start=true]:rounded-md data-[range-start=true]:rounded-l-md [&>span]:text-xs [&>span]:opacity-70",
defaultClassNames.day,
className
)}
{...props}
/>
)
}