feat: enhance ProfileForm with team search and dropdown functionality
- Added search functionality to filter teams by name or direction in the ProfileForm component. - Implemented a custom dropdown for team selection, including dynamic positioning based on available space. - Integrated click outside detection to close the dropdown when interacting outside of it. - Updated navigation component to use a Link for user info display, improving accessibility and interaction.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useState, useMemo, useRef, useEffect } from "react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Card,
|
||||
@@ -11,14 +11,8 @@ import {
|
||||
} from "@/components/ui/card";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
import { UserProfile, Team } from "@/lib/types";
|
||||
import { Search, Building2, ChevronDown, Check } from "lucide-react";
|
||||
|
||||
interface ProfileFormProps {
|
||||
teams: Team[];
|
||||
@@ -34,6 +28,12 @@ export function ProfileForm({
|
||||
const [firstName, setFirstName] = useState(initialProfile?.firstName || "");
|
||||
const [lastName, setLastName] = useState(initialProfile?.lastName || "");
|
||||
const [teamId, setTeamId] = useState(initialProfile?.teamId || "");
|
||||
const [searchTerm, setSearchTerm] = useState("");
|
||||
const [isTeamDropdownOpen, setIsTeamDropdownOpen] = useState(false);
|
||||
const teamDropdownRef = useRef<HTMLDivElement>(null);
|
||||
const [dropdownPosition, setDropdownPosition] = useState<"below" | "above">(
|
||||
"below"
|
||||
);
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
@@ -45,14 +45,57 @@ export function ProfileForm({
|
||||
const isValid =
|
||||
firstName.length > 0 && lastName.length > 0 && teamId.length > 0;
|
||||
|
||||
// Group teams by direction
|
||||
const teamsByDirection = teams.reduce((acc, team) => {
|
||||
if (!acc[team.direction]) {
|
||||
acc[team.direction] = [];
|
||||
// Group teams by direction and filter by search term
|
||||
const teamsByDirection = useMemo(() => {
|
||||
const filteredTeams = teams.filter(
|
||||
(team) =>
|
||||
team.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
|
||||
team.direction.toLowerCase().includes(searchTerm.toLowerCase())
|
||||
);
|
||||
|
||||
return filteredTeams.reduce((acc, team) => {
|
||||
if (!acc[team.direction]) {
|
||||
acc[team.direction] = [];
|
||||
}
|
||||
acc[team.direction].push(team);
|
||||
return acc;
|
||||
}, {} as Record<string, Team[]>);
|
||||
}, [teams, searchTerm]);
|
||||
|
||||
// Calculate dropdown position when opening
|
||||
const handleDropdownToggle = () => {
|
||||
if (!isTeamDropdownOpen && teamDropdownRef.current) {
|
||||
const rect = teamDropdownRef.current.getBoundingClientRect();
|
||||
const viewportHeight = window.innerHeight;
|
||||
const spaceBelow = viewportHeight - rect.bottom;
|
||||
const spaceAbove = rect.top;
|
||||
const dropdownHeight = 300; // max-height of dropdown
|
||||
|
||||
setDropdownPosition(
|
||||
spaceBelow >= dropdownHeight || spaceBelow > spaceAbove
|
||||
? "below"
|
||||
: "above"
|
||||
);
|
||||
}
|
||||
acc[team.direction].push(team);
|
||||
return acc;
|
||||
}, {} as Record<string, Team[]>);
|
||||
setIsTeamDropdownOpen(!isTeamDropdownOpen);
|
||||
};
|
||||
|
||||
// Close dropdown when clicking outside
|
||||
useEffect(() => {
|
||||
const handleClickOutside = (event: MouseEvent) => {
|
||||
if (
|
||||
teamDropdownRef.current &&
|
||||
!teamDropdownRef.current.contains(event.target as Node)
|
||||
) {
|
||||
setIsTeamDropdownOpen(false);
|
||||
}
|
||||
};
|
||||
|
||||
document.addEventListener("mousedown", handleClickOutside);
|
||||
return () => document.removeEventListener("mousedown", handleClickOutside);
|
||||
}, []);
|
||||
|
||||
const selectedTeam = teams.find((team) => team.id === teamId);
|
||||
|
||||
return (
|
||||
<Card className="w-full max-w-md mx-auto">
|
||||
@@ -88,27 +131,89 @@ export function ProfileForm({
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="team">Équipe</Label>
|
||||
<Select value={teamId} onValueChange={setTeamId} required>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Sélectionnez votre équipe" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{Object.entries(teamsByDirection).map(
|
||||
([direction, directionTeams]) => (
|
||||
<div key={direction}>
|
||||
<div className="px-2 py-1.5 text-sm font-semibold text-muted-foreground">
|
||||
{direction}
|
||||
</div>
|
||||
{directionTeams.map((team) => (
|
||||
<SelectItem key={team.id} value={team.id}>
|
||||
{team.name}
|
||||
</SelectItem>
|
||||
))}
|
||||
<div className="relative" ref={teamDropdownRef}>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
className="w-full justify-between"
|
||||
onClick={handleDropdownToggle}
|
||||
>
|
||||
<span
|
||||
className={
|
||||
selectedTeam ? "text-foreground" : "text-muted-foreground"
|
||||
}
|
||||
>
|
||||
{selectedTeam
|
||||
? selectedTeam.name
|
||||
: "Sélectionnez votre équipe"}
|
||||
</span>
|
||||
<ChevronDown className="h-4 w-4" />
|
||||
</Button>
|
||||
|
||||
{isTeamDropdownOpen && (
|
||||
<div
|
||||
className={`absolute left-0 right-0 bg-background border border-border rounded-md shadow-lg z-50 max-h-[300px] overflow-y-auto ${
|
||||
dropdownPosition === "below"
|
||||
? "top-full mt-1"
|
||||
: "bottom-full mb-1"
|
||||
}`}
|
||||
>
|
||||
{/* Barre de recherche */}
|
||||
<div className="sticky top-0 bg-background border-b p-2">
|
||||
<div className="relative">
|
||||
<Search className="absolute left-2 top-2.5 h-4 w-4 text-muted-foreground" />
|
||||
<Input
|
||||
placeholder="Rechercher une équipe ou direction..."
|
||||
value={searchTerm}
|
||||
onChange={(e) => setSearchTerm(e.target.value)}
|
||||
className="pl-8 h-8 text-sm"
|
||||
autoFocus
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
)}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
{/* Liste des équipes groupées par direction */}
|
||||
{Object.entries(teamsByDirection).map(
|
||||
([direction, directionTeams]) => (
|
||||
<div key={direction}>
|
||||
{/* En-tête de direction (non-cliquable) */}
|
||||
<div className="px-3 py-2 text-sm font-semibold text-muted-foreground bg-muted/30 border-b border-border flex items-center gap-2">
|
||||
<Building2 className="h-3 w-3" />
|
||||
{direction}
|
||||
</div>
|
||||
{/* Équipes de cette direction */}
|
||||
{directionTeams.map((team) => (
|
||||
<button
|
||||
key={team.id}
|
||||
type="button"
|
||||
className={`w-full px-3 py-2 text-left hover:bg-muted/50 flex items-center justify-between ${
|
||||
team.id === teamId ? "bg-muted" : ""
|
||||
}`}
|
||||
onClick={() => {
|
||||
setTeamId(team.id);
|
||||
setIsTeamDropdownOpen(false);
|
||||
setSearchTerm("");
|
||||
}}
|
||||
>
|
||||
<span>{team.name}</span>
|
||||
{team.id === teamId && (
|
||||
<Check className="h-4 w-4 text-primary" />
|
||||
)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
)}
|
||||
|
||||
{/* Message si aucune équipe trouvée */}
|
||||
{Object.keys(teamsByDirection).length === 0 && (
|
||||
<div className="px-3 py-4 text-center text-sm text-muted-foreground">
|
||||
Aucune équipe trouvée pour "{searchTerm}"
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Button type="submit" className="w-full" disabled={!isValid}>
|
||||
|
||||
Reference in New Issue
Block a user