improves steppbar, and phone validation, added formatter
This commit is contained in:
@@ -1,95 +1,94 @@
|
||||
import { useState, useRef, useEffect } from 'react'
|
||||
import * as CollapsiblePrimitive from '@radix-ui/react-collapsible'
|
||||
import { Check, ChevronsUpDown, Search, X, Eye, EyeOff, Upload, Hash } from 'lucide-react'
|
||||
import { useState, useRef, useEffect } from "react";
|
||||
import * as CollapsiblePrimitive from "@radix-ui/react-collapsible";
|
||||
import { Check, ChevronsUpDown, Search, X, Eye, EyeOff, Upload, Hash } from "lucide-react";
|
||||
|
||||
import { cn } from '@/lib/utils'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
|
||||
import { Calendar } from '@/components/ui/calendar'
|
||||
import { format, parse, isValid } from 'date-fns'
|
||||
import { CalendarIcon } from 'lucide-react'
|
||||
import { cn } from "@/lib/utils";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
|
||||
import { Calendar } from "@/components/ui/calendar";
|
||||
import { format, parse, isValid } from "date-fns";
|
||||
import { CalendarIcon } from "lucide-react";
|
||||
|
||||
// ─── Collapsible aliases ──────────────────────────────────────────────────────
|
||||
|
||||
const Collapsible = CollapsiblePrimitive.Root
|
||||
const CollapsibleTrigger = CollapsiblePrimitive.Trigger
|
||||
const CollapsibleContent = CollapsiblePrimitive.Content
|
||||
const Collapsible = CollapsiblePrimitive.Root;
|
||||
const CollapsibleTrigger = CollapsiblePrimitive.Trigger;
|
||||
const CollapsibleContent = CollapsiblePrimitive.Content;
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────────────────────
|
||||
|
||||
export type FormInputType =
|
||||
| 'text'
|
||||
| 'number'
|
||||
| 'phone'
|
||||
| 'email'
|
||||
| 'password'
|
||||
| 'date'
|
||||
| 'select'
|
||||
| 'searchable-select'
|
||||
| 'file'
|
||||
| 'textarea'
|
||||
| "text"
|
||||
| "number"
|
||||
| "phone"
|
||||
| "email"
|
||||
| "password"
|
||||
| "date"
|
||||
| "select"
|
||||
| "searchable-select"
|
||||
| "file"
|
||||
| "textarea";
|
||||
|
||||
export interface SelectOption {
|
||||
value: string
|
||||
label: string
|
||||
value: string;
|
||||
label: string;
|
||||
}
|
||||
|
||||
export interface FormInputProps {
|
||||
type?: FormInputType
|
||||
label?: string
|
||||
value?: string
|
||||
onChange?: (value: string) => void
|
||||
placeholder?: string
|
||||
error?: string
|
||||
required?: boolean
|
||||
disabled?: boolean
|
||||
options?: SelectOption[]
|
||||
accept?: string
|
||||
onFileChange?: (file: File | null) => void
|
||||
fileValue?: File | null
|
||||
className?: string
|
||||
phonePrefix?: string
|
||||
rows?: number
|
||||
name?: string
|
||||
readOnly?: boolean
|
||||
type?: FormInputType;
|
||||
label?: string;
|
||||
value?: string;
|
||||
onChange?: (value: string) => void;
|
||||
placeholder?: string;
|
||||
error?: string;
|
||||
required?: boolean;
|
||||
disabled?: boolean;
|
||||
options?: SelectOption[];
|
||||
accept?: string;
|
||||
onFileChange?: (file: File | null) => void;
|
||||
fileValue?: File | null;
|
||||
className?: string;
|
||||
phonePrefix?: string;
|
||||
rows?: number;
|
||||
name?: string;
|
||||
readOnly?: boolean;
|
||||
}
|
||||
|
||||
// ─── FieldLabel ───────────────────────────────────────────────────────────────
|
||||
|
||||
function FieldLabel({ label, required }: { label?: string; required?: boolean }) {
|
||||
if (!label) return null
|
||||
if (!label) return null;
|
||||
return (
|
||||
<label className="block text-xs font-semibold text-muted-foreground uppercase tracking-wider mb-1.5 select-none">
|
||||
{label}
|
||||
{required && <span className="text-destructive ml-0.5">*</span>}
|
||||
</label>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// ─── ErrorMsg ─────────────────────────────────────────────────────────────────
|
||||
|
||||
function ErrorMsg({ error }: { error?: string }) {
|
||||
if (!error) return null
|
||||
if (!error) return null;
|
||||
return (
|
||||
<p className="mt-1.5 text-xs text-destructive font-medium flex items-center gap-1.5">
|
||||
<span className="inline-block w-1 h-1 rounded-full bg-destructive shrink-0" />
|
||||
{error}
|
||||
</p>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// ─── error border helper ──────────────────────────────────────────────────────
|
||||
|
||||
const withError = (error?: string) =>
|
||||
error ? 'border-destructive focus-visible:ring-destructive/30' : ''
|
||||
const withError = (error?: string) => (error ? "border-destructive focus-visible:ring-destructive/30" : "");
|
||||
|
||||
// ─── TextInput ────────────────────────────────────────────────────────────────
|
||||
|
||||
function TextInput({
|
||||
type = 'text',
|
||||
value = '',
|
||||
type = "text",
|
||||
value = "",
|
||||
onChange,
|
||||
placeholder,
|
||||
error,
|
||||
@@ -101,11 +100,11 @@ function TextInput({
|
||||
readOnly,
|
||||
rows = 3,
|
||||
}: FormInputProps) {
|
||||
const [showPass, setShowPass] = useState(false)
|
||||
const [showPass, setShowPass] = useState(false);
|
||||
|
||||
const isPassword = type === 'password'
|
||||
const isTextarea = type === 'textarea'
|
||||
const isNumber = type === 'number'
|
||||
const isPassword = type === "password";
|
||||
const isTextarea = type === "textarea";
|
||||
const isNumber = type === "number";
|
||||
|
||||
return (
|
||||
<div className={className}>
|
||||
@@ -121,35 +120,26 @@ function TextInput({
|
||||
disabled={disabled}
|
||||
readOnly={readOnly}
|
||||
rows={rows}
|
||||
className={cn('resize-none', withError(error))}
|
||||
className={cn("resize-none", withError(error))}
|
||||
/>
|
||||
) : (
|
||||
<Input
|
||||
name={name}
|
||||
type={
|
||||
isPassword ? (showPass ? 'text' : 'password')
|
||||
: isNumber ? 'number'
|
||||
: type === 'email' ? 'email'
|
||||
: 'text'
|
||||
}
|
||||
type={isPassword ? (showPass ? "text" : "password") : isNumber ? "number" : type === "email" ? "email" : "text"}
|
||||
min={isNumber ? 0 : undefined}
|
||||
value={value}
|
||||
onChange={(e) => {
|
||||
if (isNumber) {
|
||||
const num = e.target.value
|
||||
if (num === '' || Number(num) >= 0) onChange?.(num)
|
||||
const num = e.target.value;
|
||||
if (num === "" || Number(num) >= 0) onChange?.(num);
|
||||
} else {
|
||||
onChange?.(e.target.value)
|
||||
onChange?.(e.target.value);
|
||||
}
|
||||
}}
|
||||
placeholder={placeholder}
|
||||
disabled={disabled}
|
||||
readOnly={readOnly}
|
||||
className={cn(
|
||||
isPassword && 'pr-10',
|
||||
isNumber && 'pr-10',
|
||||
withError(error),
|
||||
)}
|
||||
className={cn(isPassword && "pr-10", isNumber && "pr-10", withError(error))}
|
||||
/>
|
||||
)}
|
||||
|
||||
@@ -164,43 +154,48 @@ function TextInput({
|
||||
</button>
|
||||
)}
|
||||
|
||||
{isNumber && (
|
||||
<Hash
|
||||
size={14}
|
||||
className="absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground pointer-events-none"
|
||||
/>
|
||||
)}
|
||||
{isNumber && <Hash size={14} className="absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground pointer-events-none" />}
|
||||
</div>
|
||||
|
||||
<ErrorMsg error={error} />
|
||||
</div>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// ─── PhoneInput ───────────────────────────────────────────────────────────────
|
||||
|
||||
function PhoneInput({
|
||||
value = '',
|
||||
value = "",
|
||||
onChange,
|
||||
placeholder = '61 111213',
|
||||
placeholder = "61 097651",
|
||||
error,
|
||||
disabled,
|
||||
required,
|
||||
label,
|
||||
className,
|
||||
phonePrefix = '+993',
|
||||
phonePrefix = "+993",
|
||||
}: FormInputProps) {
|
||||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const digits = e.target.value.replace(/\D/g, "").slice(0, 8);
|
||||
|
||||
if (digits.length <= 2) {
|
||||
onChange?.(digits);
|
||||
} else {
|
||||
onChange?.(`${digits.slice(0, 2)} ${digits.slice(2)}`);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={className}>
|
||||
<FieldLabel label={label} required={required} />
|
||||
|
||||
<div
|
||||
className={cn(
|
||||
'flex h-8 w-full rounded-md border bg-background overflow-hidden',
|
||||
'ring-offset-background transition-colors duration-150',
|
||||
'focus-within:outline-none focus-within:ring-1 focus-within:ring-ring focus-within:ring-offset-0',
|
||||
error ? 'border-destructive focus-within:ring-destructive/30' : 'border-input',
|
||||
disabled && 'opacity-50 cursor-not-allowed',
|
||||
"flex h-8 w-full rounded-md border bg-background overflow-hidden",
|
||||
"ring-offset-background transition-colors duration-150",
|
||||
"focus-within:outline-none focus-within:ring-1 focus-within:ring-ring focus-within:ring-offset-0",
|
||||
error ? "border-destructive focus-within:ring-destructive/30" : "border-input",
|
||||
disabled && "opacity-50 cursor-not-allowed",
|
||||
)}
|
||||
>
|
||||
<div className="flex items-center px-3 bg-muted/50 border-r border-input text-sm font-medium text-muted-foreground shrink-0 select-none">
|
||||
@@ -209,7 +204,7 @@ function PhoneInput({
|
||||
<input
|
||||
type="tel"
|
||||
value={value}
|
||||
onChange={(e) => onChange?.(e.target.value.replace(/[^\d\s-]/g, ''))}
|
||||
onChange={handleChange}
|
||||
placeholder={placeholder}
|
||||
disabled={disabled}
|
||||
className="flex-1 h-full px-3 bg-transparent text-sm text-foreground placeholder:text-muted-foreground/50 focus:outline-none disabled:cursor-not-allowed"
|
||||
@@ -218,31 +213,22 @@ function PhoneInput({
|
||||
|
||||
<ErrorMsg error={error} />
|
||||
</div>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// ─── DateInput (shadcn Popover + Calendar) ───────────────────────────────────
|
||||
|
||||
function DateInput({
|
||||
value = '',
|
||||
onChange,
|
||||
error,
|
||||
disabled,
|
||||
required,
|
||||
label,
|
||||
className,
|
||||
placeholder = 'Sene saýlaň',
|
||||
}: FormInputProps) {
|
||||
const [open, setOpen] = useState(false)
|
||||
function DateInput({ value = "", onChange, error, disabled, required, label, className, placeholder = "Sene saýlaň" }: FormInputProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
// value is stored as 'yyyy-MM-dd' string
|
||||
const parsed = value ? parse(value, 'yyyy-MM-dd', new Date()) : undefined
|
||||
const selected = parsed && isValid(parsed) ? parsed : undefined
|
||||
const parsed = value ? parse(value, "yyyy-MM-dd", new Date()) : undefined;
|
||||
const selected = parsed && isValid(parsed) ? parsed : undefined;
|
||||
|
||||
const handleSelect = (day: Date | undefined) => {
|
||||
onChange?.(day ? format(day, 'yyyy-MM-dd') : '')
|
||||
setOpen(false)
|
||||
}
|
||||
onChange?.(day ? format(day, "yyyy-MM-dd") : "");
|
||||
setOpen(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={className}>
|
||||
@@ -254,15 +240,15 @@ function DateInput({
|
||||
type="button"
|
||||
disabled={disabled}
|
||||
className={cn(
|
||||
'flex h-8 w-full items-center justify-between rounded-md border bg-background px-3 py-2',
|
||||
'text-sm ring-offset-background transition-colors duration-150',
|
||||
'focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-0',
|
||||
'disabled:cursor-not-allowed disabled:opacity-50',
|
||||
error ? 'border-destructive' : 'border-input',
|
||||
!selected && 'text-muted-foreground/60',
|
||||
"flex h-8 w-full items-center justify-between rounded-md border bg-background px-3 py-2",
|
||||
"text-sm ring-offset-background transition-colors duration-150",
|
||||
"focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-0",
|
||||
"disabled:cursor-not-allowed disabled:opacity-50",
|
||||
error ? "border-destructive" : "border-input",
|
||||
!selected && "text-muted-foreground/60",
|
||||
)}
|
||||
>
|
||||
<span>{selected ? format(selected, 'dd.MM.yyyy') : placeholder}</span>
|
||||
<span>{selected ? format(selected, "dd.MM.yyyy") : placeholder}</span>
|
||||
<CalendarIcon size={15} className="text-muted-foreground shrink-0" />
|
||||
</button>
|
||||
</PopoverTrigger>
|
||||
@@ -282,42 +268,39 @@ function DateInput({
|
||||
|
||||
<ErrorMsg error={error} />
|
||||
</div>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// ─── SelectInput ──────────────────────────────────────────────────────────────
|
||||
|
||||
// ─── SelectInput ──────────────────────────────────────────────────────────────
|
||||
// Artık Collapsible tabanlı — SearchableSelect ile aynı mekanizma
|
||||
|
||||
function SelectInput({
|
||||
value = '',
|
||||
value = "",
|
||||
onChange,
|
||||
options = [],
|
||||
placeholder = '—',
|
||||
placeholder = "—",
|
||||
error,
|
||||
disabled,
|
||||
required,
|
||||
label,
|
||||
className,
|
||||
}: FormInputProps) {
|
||||
const [open, setOpen] = useState(false)
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
const selected = options.find((o) => o.value === value)
|
||||
const selected = options.find((o) => o.value === value);
|
||||
|
||||
const handleSelect = (val: string) => {
|
||||
onChange?.(val)
|
||||
setOpen(false)
|
||||
}
|
||||
onChange?.(val);
|
||||
setOpen(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={cn('relative', className)}>
|
||||
<div className={cn("relative", className)}>
|
||||
<FieldLabel label={label} required={required} />
|
||||
|
||||
<Collapsible
|
||||
open={open}
|
||||
onOpenChange={(next) => {
|
||||
if (!disabled) setOpen(next)
|
||||
if (!disabled) setOpen(next);
|
||||
}}
|
||||
>
|
||||
<CollapsibleTrigger asChild>
|
||||
@@ -325,23 +308,20 @@ function SelectInput({
|
||||
type="button"
|
||||
disabled={disabled}
|
||||
className={cn(
|
||||
'flex h-8 w-full items-center justify-between rounded-md border bg-background px-3 py-2',
|
||||
'text-sm ring-offset-background transition-colors duration-150',
|
||||
'focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-0',
|
||||
'disabled:cursor-not-allowed disabled:opacity-50',
|
||||
error ? 'border-destructive' : 'border-input',
|
||||
open && !error && 'ring-2 ring-ring ring-offset-0 border-transparent',
|
||||
"flex h-8 w-full items-center justify-between rounded-md border bg-background px-3 py-2",
|
||||
"text-sm ring-offset-background transition-colors duration-150",
|
||||
"focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-0",
|
||||
"disabled:cursor-not-allowed disabled:opacity-50",
|
||||
error ? "border-destructive" : "border-input",
|
||||
open && !error && "ring-2 ring-ring ring-offset-0 border-transparent",
|
||||
)}
|
||||
>
|
||||
<span className={cn('truncate text-left', !selected && 'text-muted-foreground/60')}>
|
||||
<span className={cn("truncate text-left", !selected && "text-muted-foreground/60")}>
|
||||
{selected ? selected.label : placeholder}
|
||||
</span>
|
||||
<ChevronsUpDown
|
||||
size={14}
|
||||
className={cn(
|
||||
'text-muted-foreground transition-transform duration-200 shrink-0',
|
||||
open && 'text-foreground',
|
||||
)}
|
||||
className={cn("text-muted-foreground transition-transform duration-200 shrink-0", open && "text-foreground")}
|
||||
/>
|
||||
</button>
|
||||
</CollapsibleTrigger>
|
||||
@@ -349,22 +329,25 @@ function SelectInput({
|
||||
<CollapsibleContent className="mt-1 rounded-md border border-border bg-card shadow-lg overflow-hidden">
|
||||
<div className="max-h-52 overflow-y-auto py-1">
|
||||
{options.map((opt) => {
|
||||
const isSelected = opt.value === value
|
||||
const isSelected = opt.value === value;
|
||||
return (
|
||||
<button
|
||||
key={opt.value}
|
||||
type="button"
|
||||
onMouseDown={(e) => { e.preventDefault(); handleSelect(opt.value) }}
|
||||
onMouseDown={(e) => {
|
||||
e.preventDefault();
|
||||
handleSelect(opt.value);
|
||||
}}
|
||||
className={cn(
|
||||
'flex items-center justify-between w-full text-left px-3 py-2 text-sm transition-colors',
|
||||
'hover:bg-accent hover:text-accent-foreground',
|
||||
isSelected && 'text-primary font-medium bg-primary/10',
|
||||
"flex items-center justify-between w-full text-left px-3 py-2 text-sm transition-colors",
|
||||
"hover:bg-accent hover:text-accent-foreground",
|
||||
isSelected && "text-primary font-medium bg-primary/10",
|
||||
)}
|
||||
>
|
||||
<span>{opt.label}</span>
|
||||
{isSelected && <Check size={13} className="text-primary shrink-0" />}
|
||||
</button>
|
||||
)
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</CollapsibleContent>
|
||||
@@ -372,7 +355,7 @@ function SelectInput({
|
||||
|
||||
<ErrorMsg error={error} />
|
||||
</div>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// ─── SearchableSelect ─────────────────────────────────────────────────────────
|
||||
@@ -381,67 +364,76 @@ function SelectInput({
|
||||
// firing before the selection is registered.
|
||||
|
||||
function SearchableSelect({
|
||||
value = '',
|
||||
value = "",
|
||||
onChange,
|
||||
options = [],
|
||||
placeholder = 'Saýlamak üçin basyň',
|
||||
placeholder = "Saýlamak üçin basyň",
|
||||
error,
|
||||
disabled,
|
||||
required,
|
||||
label,
|
||||
className,
|
||||
}: FormInputProps) {
|
||||
const [open, setOpen] = useState(false)
|
||||
const [query, setQuery] = useState('')
|
||||
const searchRef = useRef<HTMLInputElement>(null)
|
||||
const [open, setOpen] = useState(false);
|
||||
const [query, setQuery] = useState("");
|
||||
const searchRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
const selected = options.find((o) => o.value === value)
|
||||
const filtered = query.trim()
|
||||
? options.filter((o) => o.label.toLowerCase().includes(query.toLowerCase()))
|
||||
: options
|
||||
const selected = options.find((o) => o.value === value);
|
||||
const filtered = query.trim() ? options.filter((o) => o.label.toLowerCase().includes(query.toLowerCase())) : options;
|
||||
|
||||
const openDropdown = () => {
|
||||
setOpen(true)
|
||||
setOpen(true);
|
||||
// Focus search after Collapsible animation frame
|
||||
requestAnimationFrame(() => searchRef.current?.focus())
|
||||
}
|
||||
requestAnimationFrame(() => searchRef.current?.focus());
|
||||
};
|
||||
|
||||
const closeDropdown = () => {
|
||||
setOpen(false)
|
||||
setQuery('')
|
||||
}
|
||||
setOpen(false);
|
||||
setQuery("");
|
||||
};
|
||||
|
||||
const handleSelect = (val: string) => {
|
||||
onChange?.(val)
|
||||
closeDropdown()
|
||||
}
|
||||
onChange?.(val);
|
||||
closeDropdown();
|
||||
};
|
||||
|
||||
const handleClear = (e: React.MouseEvent) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
onChange?.('')
|
||||
}
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
onChange?.("");
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={cn('relative', className)}>
|
||||
<div className={cn("relative", className)}>
|
||||
<FieldLabel label={label} required={required} />
|
||||
|
||||
<Collapsible open={open} onOpenChange={(next) => { if (!disabled) { if (next) { openDropdown() } else { closeDropdown() } } }}>
|
||||
<Collapsible
|
||||
open={open}
|
||||
onOpenChange={(next) => {
|
||||
if (!disabled) {
|
||||
if (next) {
|
||||
openDropdown();
|
||||
} else {
|
||||
closeDropdown();
|
||||
}
|
||||
}
|
||||
}}
|
||||
>
|
||||
{/* Trigger */}
|
||||
<CollapsibleTrigger asChild>
|
||||
<button
|
||||
type="button"
|
||||
disabled={disabled}
|
||||
className={cn(
|
||||
'flex h-8 w-full items-center justify-between rounded-md border bg-background px-3 py-2',
|
||||
'text-sm ring-offset-background transition-colors duration-150',
|
||||
'focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-0',
|
||||
'disabled:cursor-not-allowed disabled:opacity-50',
|
||||
error ? 'border-destructive' : 'border-input',
|
||||
open && !error && 'ring-2 ring-ring ring-offset-0 border-transparent',
|
||||
"flex h-8 w-full items-center justify-between rounded-md border bg-background px-3 py-2",
|
||||
"text-sm ring-offset-background transition-colors duration-150",
|
||||
"focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-0",
|
||||
"disabled:cursor-not-allowed disabled:opacity-50",
|
||||
error ? "border-destructive" : "border-input",
|
||||
open && !error && "ring-2 ring-ring ring-offset-0 border-transparent",
|
||||
)}
|
||||
>
|
||||
<span className={cn('truncate text-left', !selected && 'text-muted-foreground/60')}>
|
||||
<span className={cn("truncate text-left", !selected && "text-muted-foreground/60")}>
|
||||
{selected ? selected.label : placeholder}
|
||||
</span>
|
||||
|
||||
@@ -458,10 +450,7 @@ function SearchableSelect({
|
||||
)}
|
||||
<ChevronsUpDown
|
||||
size={14}
|
||||
className={cn(
|
||||
'text-muted-foreground transition-transform duration-200 shrink-0',
|
||||
open && 'text-foreground',
|
||||
)}
|
||||
className={cn("text-muted-foreground transition-transform duration-200 shrink-0", open && "text-foreground")}
|
||||
/>
|
||||
</div>
|
||||
</button>
|
||||
@@ -484,7 +473,7 @@ function SearchableSelect({
|
||||
<button
|
||||
type="button"
|
||||
tabIndex={-1}
|
||||
onMouseDown={() => setQuery('')}
|
||||
onMouseDown={() => setQuery("")}
|
||||
className="text-muted-foreground hover:text-foreground transition-colors"
|
||||
>
|
||||
<X size={12} />
|
||||
@@ -495,27 +484,28 @@ function SearchableSelect({
|
||||
{/* Options */}
|
||||
<div className="max-h-52 overflow-y-auto py-1">
|
||||
{filtered.length === 0 ? (
|
||||
<p className="px-3 py-4 text-sm text-muted-foreground text-center">
|
||||
Netije tapylmady
|
||||
</p>
|
||||
<p className="px-3 py-4 text-sm text-muted-foreground text-center">Netije tapylmady</p>
|
||||
) : (
|
||||
filtered.map((opt) => {
|
||||
const isSelected = opt.value === value
|
||||
const isSelected = opt.value === value;
|
||||
return (
|
||||
<button
|
||||
key={opt.value}
|
||||
type="button"
|
||||
onMouseDown={(e) => { e.preventDefault(); handleSelect(opt.value) }}
|
||||
onMouseDown={(e) => {
|
||||
e.preventDefault();
|
||||
handleSelect(opt.value);
|
||||
}}
|
||||
className={cn(
|
||||
'flex items-center justify-between w-full text-left px-3 py-2 text-sm transition-colors',
|
||||
'hover:bg-accent hover:text-accent-foreground',
|
||||
isSelected && 'text-primary font-medium bg-primary/10',
|
||||
"flex items-center justify-between w-full text-left px-3 py-2 text-sm transition-colors",
|
||||
"hover:bg-accent hover:text-accent-foreground",
|
||||
isSelected && "text-primary font-medium bg-primary/10",
|
||||
)}
|
||||
>
|
||||
<span>{opt.label}</span>
|
||||
{isSelected && <Check size={13} className="text-primary shrink-0" />}
|
||||
</button>
|
||||
)
|
||||
);
|
||||
})
|
||||
)}
|
||||
</div>
|
||||
@@ -524,50 +514,56 @@ function SearchableSelect({
|
||||
|
||||
<ErrorMsg error={error} />
|
||||
</div>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// ─── FileInput ────────────────────────────────────────────────────────────────
|
||||
|
||||
// ─── FileInput ────────────────────────────────────────────────────────────────
|
||||
|
||||
function FileInput({
|
||||
label,
|
||||
required,
|
||||
error,
|
||||
disabled,
|
||||
accept = 'image/*',
|
||||
accept = "image/*",
|
||||
onFileChange,
|
||||
fileValue,
|
||||
className,
|
||||
placeholder = 'Faýl saýlaň ýa-da salmak üçin basyň',
|
||||
placeholder = "Faýl saýlaň ýa-da salmak üçin basyň",
|
||||
}: FormInputProps) {
|
||||
const inputRef = useRef<HTMLInputElement>(null)
|
||||
const [dragOver, setDragOver] = useState(false)
|
||||
const [preview, setPreview] = useState<string | null>(null)
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
const [dragOver, setDragOver] = useState(false);
|
||||
const [preview, setPreview] = useState<string | null>(null);
|
||||
|
||||
// fileValue prop'u değişince preview'i güncelle + memory leak önle
|
||||
useEffect(() => {
|
||||
if (fileValue?.type.startsWith('image/')) {
|
||||
const url = URL.createObjectURL(fileValue)
|
||||
setPreview(url)
|
||||
return () => URL.revokeObjectURL(url)
|
||||
let url: string | null = null;
|
||||
let timer: ReturnType<typeof setTimeout>;
|
||||
|
||||
if (fileValue?.type.startsWith("image/")) {
|
||||
url = URL.createObjectURL(fileValue);
|
||||
timer = setTimeout(() => setPreview(url!), 0);
|
||||
} else {
|
||||
timer = setTimeout(() => setPreview(null), 0);
|
||||
}
|
||||
setPreview(null)
|
||||
}, [fileValue])
|
||||
|
||||
return () => {
|
||||
if (timer) clearTimeout(timer);
|
||||
if (url) URL.revokeObjectURL(url);
|
||||
};
|
||||
}, [fileValue]);
|
||||
|
||||
// handleFile sadece onFileChange'i çağırıyor — preview useEffect hallediyor
|
||||
const handleFile = (file: File | null) => {
|
||||
onFileChange?.(file)
|
||||
}
|
||||
onFileChange?.(file);
|
||||
};
|
||||
|
||||
const handleDrop = (e: React.DragEvent) => {
|
||||
e.preventDefault()
|
||||
setDragOver(false)
|
||||
handleFile(e.dataTransfer.files[0] ?? null)
|
||||
}
|
||||
e.preventDefault();
|
||||
setDragOver(false);
|
||||
handleFile(e.dataTransfer.files[0] ?? null);
|
||||
};
|
||||
|
||||
const isImage = fileValue?.type.startsWith('image/')
|
||||
const isImage = fileValue?.type.startsWith("image/");
|
||||
|
||||
return (
|
||||
<div className={className}>
|
||||
@@ -577,38 +573,38 @@ function FileInput({
|
||||
role="button"
|
||||
tabIndex={disabled ? -1 : 0}
|
||||
onClick={() => !disabled && inputRef.current?.click()}
|
||||
onKeyDown={(e) => e.key === 'Enter' && !disabled && inputRef.current?.click()}
|
||||
onDragOver={(e) => { e.preventDefault(); setDragOver(true) }}
|
||||
onKeyDown={(e) => e.key === "Enter" && !disabled && inputRef.current?.click()}
|
||||
onDragOver={(e) => {
|
||||
e.preventDefault();
|
||||
setDragOver(true);
|
||||
}}
|
||||
onDragLeave={() => setDragOver(false)}
|
||||
onDrop={handleDrop}
|
||||
className={cn(
|
||||
'relative flex flex-col items-center justify-center',
|
||||
'min-h-[88px] rounded-md border-2 border-dashed cursor-pointer select-none',
|
||||
'transition-all duration-200',
|
||||
'focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-2',
|
||||
dragOver && 'border-primary bg-primary/5 scale-[1.01]',
|
||||
error ? 'border-destructive/60' : 'border-border',
|
||||
!dragOver && !error && 'hover:border-primary/50 hover:bg-muted/20',
|
||||
disabled && 'opacity-50 cursor-not-allowed pointer-events-none',
|
||||
fileValue && isImage && '!border-solid !border-border overflow-hidden',
|
||||
"relative flex flex-col items-center justify-center",
|
||||
"min-h-[88px] rounded-md border-2 border-dashed cursor-pointer select-none",
|
||||
"transition-all duration-200",
|
||||
"focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-2",
|
||||
dragOver && "border-primary bg-primary/5 scale-[1.01]",
|
||||
error ? "border-destructive/60" : "border-border",
|
||||
!dragOver && !error && "hover:border-primary/50 hover:bg-muted/20",
|
||||
disabled && "opacity-50 cursor-not-allowed pointer-events-none",
|
||||
fileValue && isImage && "!border-solid !border-border overflow-hidden",
|
||||
)}
|
||||
>
|
||||
{fileValue && isImage && preview ? (
|
||||
<>
|
||||
<img
|
||||
src={preview}
|
||||
alt="preview"
|
||||
className="w-full max-h-56 object-contain bg-muted/30 p-2"
|
||||
/>
|
||||
<img src={preview} alt="preview" className="w-full max-h-56 object-contain bg-muted/30 p-2" />
|
||||
<div className="w-full flex items-center justify-between px-3 py-2 bg-muted/40 border-t border-border">
|
||||
<span className="text-xs text-muted-foreground truncate max-w-[200px]">
|
||||
{fileValue.name}
|
||||
</span>
|
||||
<span className="text-xs text-muted-foreground truncate max-w-[200px]">{fileValue.name}</span>
|
||||
<Button
|
||||
type="button"
|
||||
size="icon"
|
||||
variant="ghost"
|
||||
onClick={(e) => { e.stopPropagation(); handleFile(null) }}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
handleFile(null);
|
||||
}}
|
||||
className="h-6 w-6 shrink-0 text-destructive hover:text-destructive hover:bg-destructive/10"
|
||||
>
|
||||
<X size={13} />
|
||||
@@ -620,12 +616,13 @@ function FileInput({
|
||||
<div className="w-8 h-8 rounded-md bg-primary/10 flex items-center justify-center">
|
||||
<Upload size={14} className="text-primary" />
|
||||
</div>
|
||||
<p className="text-xs font-medium text-foreground text-center truncate max-w-[180px]">
|
||||
{fileValue.name}
|
||||
</p>
|
||||
<p className="text-xs font-medium text-foreground text-center truncate max-w-[180px]">{fileValue.name}</p>
|
||||
<button
|
||||
type="button"
|
||||
onClick={(e) => { e.stopPropagation(); handleFile(null) }}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
handleFile(null);
|
||||
}}
|
||||
className="text-xs text-destructive hover:text-destructive/80 transition-colors"
|
||||
>
|
||||
Aýyr
|
||||
@@ -636,9 +633,7 @@ function FileInput({
|
||||
<div className="w-8 h-8 rounded-md bg-muted flex items-center justify-center">
|
||||
<Upload size={14} className="text-muted-foreground" />
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground text-center leading-relaxed">
|
||||
{placeholder}
|
||||
</p>
|
||||
<p className="text-xs text-muted-foreground text-center leading-relaxed">{placeholder}</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
@@ -654,20 +649,19 @@ function FileInput({
|
||||
|
||||
<ErrorMsg error={error} />
|
||||
</div>
|
||||
)
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
// ─── FormInput (router) ───────────────────────────────────────────────────────
|
||||
|
||||
export function FormInput(props: FormInputProps) {
|
||||
const { type = 'text' } = props
|
||||
const { type = "text" } = props;
|
||||
|
||||
if (type === 'phone') return <PhoneInput {...props} />
|
||||
if (type === 'date') return <DateInput {...props} />
|
||||
if (type === 'select') return <SelectInput {...props} />
|
||||
if (type === 'searchable-select') return <SearchableSelect {...props} />
|
||||
if (type === 'file') return <FileInput {...props} />
|
||||
if (type === 'textarea') return <TextInput {...props} type="textarea" />
|
||||
return <TextInput {...props} />
|
||||
}
|
||||
if (type === "phone") return <PhoneInput {...props} />;
|
||||
if (type === "date") return <DateInput {...props} />;
|
||||
if (type === "select") return <SelectInput {...props} />;
|
||||
if (type === "searchable-select") return <SearchableSelect {...props} />;
|
||||
if (type === "file") return <FileInput {...props} />;
|
||||
if (type === "textarea") return <TextInput {...props} type="textarea" />;
|
||||
return <TextInput {...props} />;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
import {
|
||||
type LucideIcon,
|
||||
ChevronRight,
|
||||
Check,
|
||||
AlertTriangle,
|
||||
} from "lucide-react";
|
||||
import { useRef, useEffect } from "react";
|
||||
import { type LucideIcon, ChevronRight, Check, AlertTriangle } from "lucide-react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import type { StepStatus } from "./stepBar";
|
||||
|
||||
@@ -21,95 +17,151 @@ interface StepBarCardsProps {
|
||||
}
|
||||
|
||||
export function StepBarCards({ steps, onGoTo }: StepBarCardsProps) {
|
||||
return (
|
||||
<nav
|
||||
role="tablist"
|
||||
aria-label="Form adımları"
|
||||
className={cn(
|
||||
"flex items-center gap-2 overflow-x-auto",
|
||||
"[&::-webkit-scrollbar]:hidden [-ms-overflow-style:none] [scrollbar-width:none]",
|
||||
)}
|
||||
>
|
||||
{steps.map((step, i) => {
|
||||
const Icon = step.icon;
|
||||
const isActive = step.status === "active";
|
||||
const isDone = step.status === "done";
|
||||
const isError = step.status === "error";
|
||||
const isIdle = step.status === "idle";
|
||||
const reachable = !isIdle || (i > 0 && steps[i - 1].status === "done");
|
||||
const wrapRef = useRef<HTMLDivElement>(null);
|
||||
const isDragging = useRef(false);
|
||||
const startX = useRef(0);
|
||||
const scrollX = useRef(0);
|
||||
const moved = useRef(false);
|
||||
|
||||
return (
|
||||
<div key={step.id} className="flex items-center gap-2 shrink-0">
|
||||
<button
|
||||
type="button"
|
||||
role="tab"
|
||||
aria-selected={isActive}
|
||||
aria-current={isActive ? "step" : undefined}
|
||||
disabled={!reachable}
|
||||
onClick={() => reachable && onGoTo(i)}
|
||||
className={cn(
|
||||
"group flex items-center gap-3 rounded-lg px-3 py-2.5 transition-all duration-200",
|
||||
"outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
|
||||
"disabled:cursor-not-allowed disabled:opacity-40",
|
||||
reachable && !isActive && "hover:bg-muted/40 cursor-pointer",
|
||||
isActive && "bg-muted/60",
|
||||
)}
|
||||
>
|
||||
{/* Icon disc */}
|
||||
<span
|
||||
// Auto-scroll active step to center
|
||||
useEffect(() => {
|
||||
const wrap = wrapRef.current;
|
||||
if (!wrap) return;
|
||||
const active = wrap.querySelector<HTMLElement>('[aria-current="step"]');
|
||||
if (!active) return;
|
||||
const wrapRect = wrap.getBoundingClientRect();
|
||||
const btnRect = active.getBoundingClientRect();
|
||||
wrap.scrollTo({
|
||||
left: wrap.scrollLeft + (btnRect.left - wrapRect.left) - wrapRect.width / 2 + btnRect.width / 2,
|
||||
behavior: "smooth",
|
||||
});
|
||||
}, [steps]);
|
||||
|
||||
// Mouse drag-to-scroll
|
||||
useEffect(() => {
|
||||
const wrap = wrapRef.current;
|
||||
if (!wrap) return;
|
||||
|
||||
const onDown = (e: MouseEvent) => {
|
||||
isDragging.current = true;
|
||||
moved.current = false;
|
||||
startX.current = e.pageX - wrap.offsetLeft;
|
||||
scrollX.current = wrap.scrollLeft;
|
||||
wrap.style.cursor = "grabbing";
|
||||
};
|
||||
|
||||
const onUp = () => {
|
||||
isDragging.current = false;
|
||||
wrap.style.cursor = "grab";
|
||||
};
|
||||
|
||||
const onMove = (e: MouseEvent) => {
|
||||
if (!isDragging.current) return;
|
||||
e.preventDefault();
|
||||
const dist = e.pageX - wrap.offsetLeft - startX.current;
|
||||
if (Math.abs(dist) > 4) moved.current = true;
|
||||
wrap.scrollLeft = scrollX.current - dist;
|
||||
};
|
||||
|
||||
// Block click after a real drag so step navigation doesn't fire
|
||||
const onClickCapture = (e: MouseEvent) => {
|
||||
if (moved.current) {
|
||||
e.stopPropagation();
|
||||
moved.current = false;
|
||||
}
|
||||
};
|
||||
|
||||
wrap.addEventListener("mousedown", onDown);
|
||||
wrap.addEventListener("mouseup", onUp);
|
||||
wrap.addEventListener("mouseleave", onUp);
|
||||
wrap.addEventListener("mousemove", onMove);
|
||||
wrap.addEventListener("click", onClickCapture, true);
|
||||
|
||||
return () => {
|
||||
wrap.removeEventListener("mousedown", onDown);
|
||||
wrap.removeEventListener("mouseup", onUp);
|
||||
wrap.removeEventListener("mouseleave", onUp);
|
||||
wrap.removeEventListener("mousemove", onMove);
|
||||
wrap.removeEventListener("click", onClickCapture, true);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={wrapRef}
|
||||
style={{ cursor: "grab" }}
|
||||
className="overflow-x-auto [&::-webkit-scrollbar]:hidden [-ms-overflow-style:none] [scrollbar-width:none]"
|
||||
>
|
||||
<nav role="tablist" aria-label="Form adımları" className="flex items-center gap-2 w-max">
|
||||
{steps.map((step, i) => {
|
||||
const Icon = step.icon;
|
||||
const isActive = step.status === "active";
|
||||
const isDone = step.status === "done";
|
||||
const isError = step.status === "error";
|
||||
const isIdle = step.status === "idle";
|
||||
const reachable = !isIdle || (i > 0 && steps[i - 1].status === "done");
|
||||
|
||||
return (
|
||||
<div key={step.id} className="flex items-center gap-2 shrink-0">
|
||||
<button
|
||||
type="button"
|
||||
role="tab"
|
||||
aria-selected={isActive}
|
||||
aria-current={isActive ? "step" : undefined}
|
||||
disabled={!reachable}
|
||||
onClick={() => reachable && onGoTo(i)}
|
||||
className={cn(
|
||||
"relative flex items-center justify-center rounded-full w-11 h-11 shrink-0 transition-all duration-300",
|
||||
isActive &&
|
||||
"bg-[#005f78] text-white ring-4 ring-[#005f78]/15 dark:bg-primary dark:text-primary-foreground dark:ring-primary/15",
|
||||
isDone &&
|
||||
"bg-[#005f78]/10 text-[#005f78] dark:bg-muted dark:text-foreground",
|
||||
isError &&
|
||||
"bg-destructive/10 text-destructive ring-2 ring-destructive/30",
|
||||
isIdle && "bg-muted text-muted-foreground",
|
||||
reachable && !isActive && "group-hover:scale-105",
|
||||
"group flex items-center gap-3 rounded-lg px-3 py-2.5 transition-all duration-200",
|
||||
"outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
|
||||
"disabled:cursor-not-allowed disabled:opacity-40",
|
||||
reachable && !isActive && "hover:bg-muted/40 cursor-pointer",
|
||||
isActive && "bg-muted/60",
|
||||
)}
|
||||
>
|
||||
{isDone ? (
|
||||
<Check
|
||||
className="w-5 h-5 animate-scale-in"
|
||||
strokeWidth={2.5}
|
||||
/>
|
||||
) : isError ? (
|
||||
<AlertTriangle className="w-5 h-5 animate-scale-in" />
|
||||
) : (
|
||||
<Icon className="w-5 h-5" strokeWidth={2} />
|
||||
)}
|
||||
</span>
|
||||
|
||||
{/* Texts */}
|
||||
<span className="flex flex-col items-start text-left leading-tight">
|
||||
{/* Icon disc */}
|
||||
<span
|
||||
className={cn(
|
||||
"text-sm font-semibold tracking-tight",
|
||||
isActive && "text-[#005f78] dark:text-foreground",
|
||||
isDone && "text-[#005f78] dark:text-foreground",
|
||||
!isActive && !isDone && !isError && "text-foreground/90",
|
||||
isError && "text-destructive",
|
||||
"relative flex items-center justify-center rounded-full w-11 h-11 shrink-0 transition-all duration-300",
|
||||
isActive &&
|
||||
"bg-[#005f78] text-white ring-4 ring-[#005f78]/15 dark:bg-primary dark:text-primary-foreground dark:ring-primary/15",
|
||||
isDone && "bg-[#005f78]/10 text-[#005f78] dark:bg-muted dark:text-foreground",
|
||||
isError && "bg-destructive/10 text-destructive ring-2 ring-destructive/30",
|
||||
isIdle && "bg-muted text-muted-foreground",
|
||||
reachable && !isActive && "group-hover:scale-105",
|
||||
)}
|
||||
>
|
||||
{step.title}
|
||||
{isDone ? (
|
||||
<Check className="w-5 h-5 animate-scale-in" strokeWidth={2.5} />
|
||||
) : isError ? (
|
||||
<AlertTriangle className="w-5 h-5 animate-scale-in" />
|
||||
) : (
|
||||
<Icon className="w-5 h-5" strokeWidth={2} />
|
||||
)}
|
||||
</span>
|
||||
<span className="text-xs text-muted-foreground mt-0.5">
|
||||
{step.subtitle}
|
||||
</span>
|
||||
</span>
|
||||
</button>
|
||||
|
||||
{/* Chevron separator */}
|
||||
{i < steps.length - 1 && (
|
||||
<ChevronRight
|
||||
className="w-4 h-4 text-muted-foreground/60 shrink-0 mx-1"
|
||||
aria-hidden
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</nav>
|
||||
{/* Texts */}
|
||||
<span className="flex flex-col items-start text-left leading-tight">
|
||||
<span
|
||||
className={cn(
|
||||
"text-sm font-semibold tracking-tight",
|
||||
isActive && "text-[#005f78] dark:text-foreground",
|
||||
isDone && "text-[#005f78] dark:text-foreground",
|
||||
!isActive && !isDone && !isError && "text-foreground/90",
|
||||
isError && "text-destructive",
|
||||
)}
|
||||
>
|
||||
{step.title}
|
||||
</span>
|
||||
<span className="text-xs text-muted-foreground mt-0.5">{step.subtitle}</span>
|
||||
</span>
|
||||
</button>
|
||||
|
||||
{/* Chevron separator */}
|
||||
{i < steps.length - 1 && <ChevronRight className="w-4 h-4 text-muted-foreground/60 shrink-0 mx-1" aria-hidden />}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</nav>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user