update search command
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { useRequest, useToggle } from 'ahooks'
|
import { useBoolean, useRequest, useToggle } from 'ahooks'
|
||||||
import { CheckIcon, UserIcon, XIcon, AlertTriangleIcon } from 'lucide-react'
|
import { CheckIcon, UserIcon, XIcon, AlertTriangleIcon } from 'lucide-react'
|
||||||
import { formatCPF } from '@brazilian-utils/brazilian-utils'
|
import { formatCPF } from '@brazilian-utils/brazilian-utils'
|
||||||
|
|
||||||
@@ -29,18 +29,21 @@ import { Button } from '@repo/ui/components/ui/button'
|
|||||||
interface AsyncComboboxProps {
|
interface AsyncComboboxProps {
|
||||||
value: any
|
value: any
|
||||||
title: string
|
title: string
|
||||||
|
placeholder?: string
|
||||||
onChange: (props: any) => void
|
onChange: (props: any) => void
|
||||||
onSearch: (search: string) => Promise<any[]>
|
onSearch: (search: string) => Promise<any[]>
|
||||||
error?: any
|
error?: any
|
||||||
}
|
}
|
||||||
export function AsyncCombobox({
|
export function AsyncCombobox({
|
||||||
title,
|
title,
|
||||||
|
placeholder,
|
||||||
value,
|
value,
|
||||||
onSearch,
|
onSearch,
|
||||||
onChange,
|
onChange,
|
||||||
error
|
error
|
||||||
}: AsyncComboboxProps) {
|
}: AsyncComboboxProps) {
|
||||||
const [open, { set }] = useToggle()
|
const [open, { set }] = useToggle()
|
||||||
|
const [searched, { setTrue }] = useBoolean()
|
||||||
const {
|
const {
|
||||||
data = [],
|
data = [],
|
||||||
loading,
|
loading,
|
||||||
@@ -48,7 +51,8 @@ export function AsyncCombobox({
|
|||||||
} = useRequest(onSearch, {
|
} = useRequest(onSearch, {
|
||||||
manual: true,
|
manual: true,
|
||||||
debounceWait: 300,
|
debounceWait: 300,
|
||||||
defaultParams: ['']
|
defaultParams: [''],
|
||||||
|
onSuccess: setTrue
|
||||||
})
|
})
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -72,7 +76,7 @@ export function AsyncCombobox({
|
|||||||
<Button
|
<Button
|
||||||
variant="link"
|
variant="link"
|
||||||
size="icon-sm"
|
size="icon-sm"
|
||||||
className="cursor-pointer text-black dark:text-white"
|
className="cursor-pointer text-muted-foreground"
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
onChange?.(undefined)
|
onChange?.(undefined)
|
||||||
@@ -93,66 +97,73 @@ export function AsyncCombobox({
|
|||||||
</PopoverTrigger>
|
</PopoverTrigger>
|
||||||
|
|
||||||
<PopoverContent className="lg:w-84 p-0" align="start">
|
<PopoverContent className="lg:w-84 p-0" align="start">
|
||||||
<Command shouldFilter={false}>
|
<Command
|
||||||
|
shouldFilter={false}
|
||||||
|
className={cn(
|
||||||
|
!searched && '**:data-[slot=command-input-wrapper]:border-b-0'
|
||||||
|
)}
|
||||||
|
>
|
||||||
<CommandInput
|
<CommandInput
|
||||||
placeholder={title}
|
placeholder={placeholder}
|
||||||
autoComplete="off"
|
autoComplete="off"
|
||||||
onValueChange={runAsync}
|
onValueChange={runAsync}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<CommandList>
|
{searched ? (
|
||||||
<CommandEmpty>Nenhum resultado encontrado.</CommandEmpty>
|
<CommandList>
|
||||||
|
<CommandEmpty>Nenhum resultado encontrado.</CommandEmpty>
|
||||||
|
|
||||||
<CommandGroup>
|
<CommandGroup>
|
||||||
{data.map(({ id, name, email, cpf }) => (
|
{data.map(({ id, name, email, cpf }) => (
|
||||||
<CommandItem
|
<CommandItem
|
||||||
key={id}
|
key={id}
|
||||||
value={id}
|
value={id}
|
||||||
className="cursor-pointer"
|
className="cursor-pointer"
|
||||||
disabled={!cpf}
|
disabled={!cpf}
|
||||||
onSelect={() => {
|
onSelect={() => {
|
||||||
onChange?.({ id, name, email, cpf })
|
onChange?.({ id, name, email, cpf })
|
||||||
set(false)
|
set(false)
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<div className="flex gap-2.5 items-start">
|
<div className="flex gap-2.5 items-start">
|
||||||
<Avatar className="size-10 hidden lg:block">
|
<Avatar className="size-10 hidden lg:block">
|
||||||
<AvatarFallback className="border">
|
<AvatarFallback className="border">
|
||||||
{initials(name)}
|
{initials(name)}
|
||||||
</AvatarFallback>
|
</AvatarFallback>
|
||||||
</Avatar>
|
</Avatar>
|
||||||
|
|
||||||
<ul>
|
<ul>
|
||||||
<li className="font-bold">
|
<li className="font-bold">
|
||||||
<Abbr>{name}</Abbr>
|
<Abbr>{name}</Abbr>
|
||||||
</li>
|
</li>
|
||||||
<li className="text-muted-foreground text-sm">
|
|
||||||
<Abbr>{email}</Abbr>
|
|
||||||
</li>
|
|
||||||
|
|
||||||
{cpf ? (
|
|
||||||
<li className="text-muted-foreground text-sm">
|
<li className="text-muted-foreground text-sm">
|
||||||
{formatCPF(cpf)}
|
<Abbr>{email}</Abbr>
|
||||||
</li>
|
</li>
|
||||||
) : (
|
|
||||||
<li className="flex gap-1 items-center text-red-400">
|
|
||||||
<AlertTriangleIcon className="text-red-400" />
|
|
||||||
Inelegível
|
|
||||||
</li>
|
|
||||||
)}
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<CheckIcon
|
{cpf ? (
|
||||||
className={cn(
|
<li className="text-muted-foreground text-sm">
|
||||||
'ml-auto',
|
{formatCPF(cpf)}
|
||||||
value?.id === id ? 'opacity-100' : 'opacity-0'
|
</li>
|
||||||
)}
|
) : (
|
||||||
/>
|
<li className="flex gap-1 items-center text-red-400">
|
||||||
</CommandItem>
|
<AlertTriangleIcon className="text-red-400" />
|
||||||
))}
|
Inelegível
|
||||||
</CommandGroup>
|
</li>
|
||||||
</CommandList>
|
)}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<CheckIcon
|
||||||
|
className={cn(
|
||||||
|
'ml-auto',
|
||||||
|
value?.id === id ? 'opacity-100' : 'opacity-0'
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</CommandItem>
|
||||||
|
))}
|
||||||
|
</CommandGroup>
|
||||||
|
</CommandList>
|
||||||
|
) : null}
|
||||||
</Command>
|
</Command>
|
||||||
</PopoverContent>
|
</PopoverContent>
|
||||||
</Popover>
|
</Popover>
|
||||||
|
|||||||
@@ -11,11 +11,13 @@ import {
|
|||||||
XIcon,
|
XIcon,
|
||||||
ChevronsUpDownIcon,
|
ChevronsUpDownIcon,
|
||||||
CheckIcon,
|
CheckIcon,
|
||||||
BookIcon
|
BookIcon,
|
||||||
|
ArrowDownAZIcon,
|
||||||
|
ArrowDownZAIcon
|
||||||
} from 'lucide-react'
|
} from 'lucide-react'
|
||||||
import { Link, useParams, useFetcher } from 'react-router'
|
import { Link, useParams, useFetcher } from 'react-router'
|
||||||
import { Controller, useFieldArray, useForm } from 'react-hook-form'
|
import { Controller, useFieldArray, useForm } from 'react-hook-form'
|
||||||
import { Fragment, use, useEffect, useMemo, useState } from 'react'
|
import { Fragment, use, useEffect, useMemo, useRef, useState } from 'react'
|
||||||
import { format } from 'date-fns'
|
import { format } from 'date-fns'
|
||||||
import { ptBR } from 'react-day-picker/locale'
|
import { ptBR } from 'react-day-picker/locale'
|
||||||
import { zodResolver } from '@hookform/resolvers/zod'
|
import { zodResolver } from '@hookform/resolvers/zod'
|
||||||
@@ -348,7 +350,9 @@ function FacetedFilter({
|
|||||||
}: FacetedFilterProps) {
|
}: FacetedFilterProps) {
|
||||||
const [search, setSearch] = useState<string>('')
|
const [search, setSearch] = useState<string>('')
|
||||||
const [open, { set }] = useToggle()
|
const [open, { set }] = useToggle()
|
||||||
|
const [sort, { toggle }] = useToggle('a-z', 'z-a')
|
||||||
const { hits } = use(options)
|
const { hits } = use(options)
|
||||||
|
const listRef = useRef<HTMLDivElement | null>(null)
|
||||||
const fuse = useMemo(() => {
|
const fuse = useMemo(() => {
|
||||||
return new Fuse(hits, {
|
return new Fuse(hits, {
|
||||||
keys: ['name'],
|
keys: ['name'],
|
||||||
@@ -358,12 +362,13 @@ function FacetedFilter({
|
|||||||
}, [hits])
|
}, [hits])
|
||||||
|
|
||||||
const filtered = useMemo(() => {
|
const filtered = useMemo(() => {
|
||||||
if (!search) {
|
const results = !search ? hits : fuse.search(search).map(({ item }) => item)
|
||||||
return hits
|
|
||||||
}
|
|
||||||
|
|
||||||
return fuse.search(search).map(({ item }) => item)
|
return results.sort((a, b) => {
|
||||||
}, [search, fuse, hits])
|
const comparison = a.name.localeCompare(b.name)
|
||||||
|
return sort === 'a-z' ? comparison : -comparison
|
||||||
|
})
|
||||||
|
}, [search, fuse, hits, sort])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Popover open={open} onOpenChange={set}>
|
<Popover open={open} onOpenChange={set}>
|
||||||
@@ -386,12 +391,27 @@ function FacetedFilter({
|
|||||||
|
|
||||||
<PopoverContent className="lg:w-84 p-0" align="start">
|
<PopoverContent className="lg:w-84 p-0" align="start">
|
||||||
<Command shouldFilter={false}>
|
<Command shouldFilter={false}>
|
||||||
<CommandInput
|
<div className="flex">
|
||||||
placeholder="Curso"
|
<div className="flex-1">
|
||||||
autoComplete="off"
|
<CommandInput
|
||||||
onValueChange={setSearch}
|
placeholder="Curso"
|
||||||
/>
|
autoComplete="off"
|
||||||
<CommandList>
|
onValueChange={setSearch}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="border-b flex items-center justify-end">
|
||||||
|
<Button
|
||||||
|
variant="link"
|
||||||
|
size="icon-sm"
|
||||||
|
className="cursor-pointer text-muted-foreground"
|
||||||
|
onClick={toggle}
|
||||||
|
>
|
||||||
|
{sort == 'a-z' ? <ArrowDownAZIcon /> : <ArrowDownZAIcon />}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{/* Force rerender to reset the scroll position */}
|
||||||
|
<CommandList key={sort}>
|
||||||
<CommandEmpty>Nenhum resultado encontrado.</CommandEmpty>
|
<CommandEmpty>Nenhum resultado encontrado.</CommandEmpty>
|
||||||
<CommandGroup>
|
<CommandGroup>
|
||||||
{filtered
|
{filtered
|
||||||
@@ -439,7 +459,7 @@ function FacetedFilter({
|
|||||||
|
|
||||||
interface ScheduledForInputProps {
|
interface ScheduledForInputProps {
|
||||||
value?: Date
|
value?: Date
|
||||||
onChange?: (value: any) => void
|
onChange?: (value: Date | undefined) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
function ScheduledForInput({ value, onChange }: ScheduledForInputProps) {
|
function ScheduledForInput({ value, onChange }: ScheduledForInputProps) {
|
||||||
@@ -459,15 +479,17 @@ function ScheduledForInput({ value, onChange }: ScheduledForInputProps) {
|
|||||||
placeholder="Imediatamente"
|
placeholder="Imediatamente"
|
||||||
value={displayValue}
|
value={displayValue}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<InputGroupAddon>
|
<InputGroupAddon>
|
||||||
<CalendarIcon />
|
<CalendarIcon />
|
||||||
</InputGroupAddon>
|
</InputGroupAddon>
|
||||||
|
|
||||||
{selected && (
|
{selected && (
|
||||||
<InputGroupAddon align="inline-end" className="mr-0">
|
<InputGroupAddon align="inline-end" className="mr-0">
|
||||||
<Button
|
<Button
|
||||||
variant="link"
|
variant="link"
|
||||||
size="icon-sm"
|
size="icon-sm"
|
||||||
className="cursor-pointer text-black dark:text-white"
|
className="cursor-pointer text-muted-foreground"
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
setDate(undefined)
|
setDate(undefined)
|
||||||
@@ -481,11 +503,12 @@ function ScheduledForInput({ value, onChange }: ScheduledForInputProps) {
|
|||||||
)}
|
)}
|
||||||
</InputGroup>
|
</InputGroup>
|
||||||
</PopoverTrigger>
|
</PopoverTrigger>
|
||||||
|
|
||||||
<PopoverContent className="w-full p-0" align="start">
|
<PopoverContent className="w-full p-0" align="start">
|
||||||
<Calendar
|
<Calendar
|
||||||
mode="single"
|
mode="single"
|
||||||
selected={selected}
|
selected={selected}
|
||||||
onSelect={(date: Date) => {
|
onSelect={(date: Date | undefined) => {
|
||||||
setDate(date)
|
setDate(date)
|
||||||
onChange?.(date)
|
onChange?.(date)
|
||||||
set(false)
|
set(false)
|
||||||
|
|||||||
Reference in New Issue
Block a user