-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add searchbar component [gh-36]
- Loading branch information
1 parent
693e32d
commit d83b447
Showing
9 changed files
with
106 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
frontend/src/components/common/ComboboxComponent/ComboboxComponent.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Employee } from '@app/types/common'; | ||
|
||
export interface ComboboxComponentProps { | ||
people?: Employee[]; | ||
setSearchedPerson: (person: Employee[]) => void; | ||
} |
67 changes: 67 additions & 0 deletions
67
frontend/src/components/common/ComboboxComponent/ComboboxComponent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use client'; | ||
|
||
import { Combobox, ComboboxInput, ComboboxOption, ComboboxOptions } from '@headlessui/react'; | ||
import { SearchIcon } from '@app/static/icons/SearchIcon'; | ||
import React, { useEffect, useState } from 'react'; | ||
import { Employee } from '@app/types/common'; | ||
import { ComboboxComponentProps } from './ComboboxComponent.interface'; | ||
|
||
export const ComboboxComponent = ({ people, setSearchedPerson }: ComboboxComponentProps) => { | ||
const [selectedPerson, setSelectedPerson] = useState<Employee | string>(''); | ||
const [query, setQuery] = useState(''); | ||
|
||
const filteredPeople = | ||
query === '' | ||
? people | ||
: people?.filter((person) => { | ||
return person.name.toLowerCase().includes(query.toLowerCase()); | ||
}); | ||
|
||
useEffect(() => { | ||
if (selectedPerson) { | ||
setSearchedPerson([selectedPerson as Employee]); | ||
} | ||
}, [selectedPerson, setSearchedPerson]); | ||
|
||
const resetResults = () => { | ||
setSelectedPerson(''); | ||
setSearchedPerson([]); | ||
}; | ||
|
||
return ( | ||
<Combobox | ||
value={selectedPerson as Employee} | ||
onChange={(value) => value && setSelectedPerson(value)} | ||
onClose={() => !query && resetResults()} | ||
> | ||
<div className="relative flex w-[304px] items-center"> | ||
<div className="absolute left-4"> | ||
<SearchIcon /> | ||
</div> | ||
<ComboboxInput | ||
aria-label="Employee" | ||
displayValue={(selectedPerson: Employee) => query && selectedPerson?.name} | ||
onChange={(event) => setQuery(event.target.value)} | ||
placeholder="Search" | ||
className="h-12 w-full rounded-xl border border-navy-200 pl-[46px] pr-4 text-navy-900 placeholder-navy-600" | ||
/> | ||
</div> | ||
{!!filteredPeople?.length && ( | ||
<ComboboxOptions | ||
anchor="bottom" | ||
className="mt-1 w-[304px] rounded-xl border border-navy-200 bg-white py-[6px] text-navy-600 placeholder-navy-600 shadow-[0_2px_6px_0_#383A441A]" | ||
> | ||
{filteredPeople?.map((person) => ( | ||
<ComboboxOption | ||
key={person.id} | ||
value={person} | ||
className="flex h-11 items-center px-3 text-navy-700 data-[focus]:bg-navy-50" | ||
> | ||
{person.name} | ||
</ComboboxOption> | ||
))} | ||
</ComboboxOptions> | ||
)} | ||
</Combobox> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { ComboboxComponent } from './ComboboxComponent'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { DropdownMenuComponent } from './DropdownMenuComponent'; |
7 changes: 0 additions & 7 deletions
7
frontend/src/components/common/InputField/InputField.interface.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters