|
| 1 | +'use client'; |
| 2 | +import type { ListboxRootValueChangeEvent } from '@primereact/types/shared/listbox'; |
| 3 | +import { Button } from '@primereact/ui/button'; |
| 4 | +import { Listbox } from '@primereact/ui/listbox'; |
| 5 | +import * as React from 'react'; |
| 6 | + |
| 7 | +const cities = [ |
| 8 | + { name: 'New York', code: 'NY' }, |
| 9 | + { name: 'Rome', code: 'RM' }, |
| 10 | + { name: 'London', code: 'LDN' }, |
| 11 | + { name: 'Istanbul', code: 'IST' }, |
| 12 | + { name: 'Paris', code: 'PRS' } |
| 13 | +]; |
| 14 | + |
| 15 | +export default function FocusBehaviorDemo() { |
| 16 | + const [selectedCity, setSelectedCity] = React.useState<string | null>(null); |
| 17 | + const [autoOptionFocus, setAutoOptionFocus] = React.useState(true); |
| 18 | + const [selectOnFocus, setSelectOnFocus] = React.useState(false); |
| 19 | + const [focusOnHover, setFocusOnHover] = React.useState(true); |
| 20 | + |
| 21 | + return ( |
| 22 | + <div className="space-y-4"> |
| 23 | + <div className="flex flex-wrap gap-2 justify-center"> |
| 24 | + <Button size="small" severity={autoOptionFocus ? 'primary' : 'secondary'} onClick={() => setAutoOptionFocus((prev) => !prev)}> |
| 25 | + autoOptionFocus: {String(autoOptionFocus)} |
| 26 | + </Button> |
| 27 | + <Button size="small" severity={selectOnFocus ? 'primary' : 'secondary'} onClick={() => setSelectOnFocus((prev) => !prev)}> |
| 28 | + selectOnFocus: {String(selectOnFocus)} |
| 29 | + </Button> |
| 30 | + <Button size="small" severity={focusOnHover ? 'primary' : 'secondary'} onClick={() => setFocusOnHover((prev) => !prev)}> |
| 31 | + focusOnHover: {String(focusOnHover)} |
| 32 | + </Button> |
| 33 | + </div> |
| 34 | + |
| 35 | + <div className="flex justify-center"> |
| 36 | + <Listbox.Root |
| 37 | + value={selectedCity} |
| 38 | + onValueChange={(e: ListboxRootValueChangeEvent) => setSelectedCity(e.value as string | null)} |
| 39 | + options={cities} |
| 40 | + optionLabel="name" |
| 41 | + optionValue="code" |
| 42 | + autoOptionFocus={autoOptionFocus} |
| 43 | + selectOnFocus={selectOnFocus} |
| 44 | + focusOnHover={focusOnHover} |
| 45 | + className="w-full md:w-56" |
| 46 | + > |
| 47 | + <Listbox.List /> |
| 48 | + </Listbox.Root> |
| 49 | + </div> |
| 50 | + </div> |
| 51 | + ); |
| 52 | +} |
0 commit comments