'use client' import type { ReactNode } from 'react' import { useHomeSearch } from '@/components/campaign/dashboard/HomeSearchContext' import { CampaignSearchInput } from '@/components/campaign/shared/CampaignSearchInput' import { homeSearchShouldUnfocusOnBlur } from '@/lib/campaignHomeSearchContract' import { HOME_SEARCH_QUERY_MAX_LENGTH } from '@/lib/schemas/homeSearch' import { cn } from '@/lib/utils' const HOME_SEARCH_INPUT_ID = 'homeSearchQuery' const HOME_SEARCH_LABEL = 'Buscar na campanha' const HOME_SEARCH_PLACEHOLDER = 'Município, liderança, atividade…' export const CampaignHomeSearch = ({ children, resultsBusy, placeholder = HOME_SEARCH_PLACEHOLDER, showResults = true, blurUnfocusPolicy = 'whenInactive', compactStack = false, }: { children?: ReactNode /** When set, must already fold debounce (see `useHomeSearchResultsState.isFetching`). */ resultsBusy?: boolean /** Empty string = discreet peek (collapsed drawer); sr-only label stays. */ placeholder?: string /** When false, keeps the live region mounted but visually hidden (collapsed peek). */ showResults?: boolean /** * Início: unfocus when query inactive. Drawer (B112): unfocus only when raw * is empty so blur-with-text keeps FULL. */ blurUnfocusPolicy?: 'whenInactive' | 'whenEmpty' /** B112 — ~⅓ of the default input→results gap in the bottom drawer. */ compactStack?: boolean }) => { const { query, setRaw, clear, isDebouncing, setInputFocused } = useHomeSearch() const ariaBusy = (resultsBusy ?? isDebouncing) || undefined return (
setRaw(event.target.value)} onFocus={() => setInputFocused(true)} onBlur={() => { if (homeSearchShouldUnfocusOnBlur(query.raw, blurUnfocusPolicy, query.isActive)) { setInputFocused(false) } }} onKeyDown={(event) => { if (event.key === 'Escape') { event.preventDefault() clear() } }} autoComplete="off" enterKeyHint="search" aria-busy={ariaBusy} maxLength={HOME_SEARCH_QUERY_MAX_LENGTH} />
) }