FormsFilterChoice
Searchastronomic.dev# FilterChoice
For a choice filter: one select, or option chips when several may be picked, such as home types.
Interactive: inline script.
## Usage
```astro
import FilterChoice from "@astronomic-dev/ui/components/FilterChoice.astro";
```
## Examples
### One choice
A field without multiple: one native select, its first option meaning any.
```astro
---
import FilterChoice from "@astronomic-dev/ui/components/FilterChoice.astro";
import { fields } from "../_data/vega.ts";
// One choice: the home type as a select (a field without multiple).
const type = { ...fields.find((field) => field.key === "type")!, multiple: false };
---
<FilterChoice field={type} anyText="Any type" class="max-w-xs" />
```
### Option chips
A field with multiple: one native checkbox per option, drawn as a chip. Check House and Condo to search both.
```astro
---
import FilterChoice from "@astronomic-dev/ui/components/FilterChoice.astro";
import { fields } from "../_data/vega.ts";
// Several choices: the home type with multiple, as option chips.
const type = fields.find((field) => field.key === "type")!;
---
<FilterChoice field={type} value={["house"]} />
```
### Dropdown
With dropdown: several choices are checkboxes in a Popover, one choice is a Select with picker. The trigger says what is picked, as the server draws it.
```astro
---
import FilterChoice from "@astronomic-dev/ui/components/FilterChoice.astro";
import { fields } from "../_data/vega.ts";
const type = fields.find((field) => field.key === "type")!;
---
<div class="grid max-w-md gap-5">
<FilterChoice dropdown field={type} value={["house", "condo"]} />
<FilterChoice dropdown field={{ ...type, multiple: false }} />
</div>
```
## Props
### FilterChoice
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `field` | `ListingField` | None | A choice field (kind "choice": a select, or option chips with multiple) or a toggle (kind "toggle": one checkbox). |
| `mode` | `string` | None | The mode whose label shows (labels per mode). |
| `value` | `string[]` | `[]` | The picked option values. |
| `checked` | `boolean` | `false` | A toggle: checked. |
| `anyText` | `string` | `FILTERS_TEXTS.anyText` | The select's empty first option, which means any ("Any"). |
| `dropdown` | `boolean` | `false` | Draws one choice as a Select with picker, several as checkboxes in a Popover. A toggle stays a checkbox. Same names and values. |
| `countText` | `string` | `"{n} selected"` | With dropdown: the words for two or more choices. The field's own countText wins. |
| `id` | `string` | None | The select's, the first checkbox's or a dropdown trigger's id. Default: a page-unique id. |
| `class` | `string` | None | More classes for the field's root, merged with cn(). |
Any other attribute goes on the field's root. Filters renders one per choice and toggle field.
## Accessibility
| Key | Action |
| --- | --- |
| Space | Checks or unchecks an option chip, or a checkbox in a dropdown |
| Enter or Space | With dropdown, on the trigger: opens and closes the panel |
| Enter | With dropdown, on a checkbox: checks or unchecks it, and does not submit the form |
| Escape | With dropdown: closes the panel and returns focus to the trigger |
- Without multiple it is a native select in a Field. A native multiple select is never used: it is hard to use with a mouse.
- With multiple it is a fieldset of native checkboxes: "Home type, group; House, checkbox, checked". Each chip is its own Tab stop; the focus ring is on the chip.
- A checked chip is filled and shows a check icon, so the state is not color alone. The chips wrap onto new lines and never scroll on their own.
- Each chip is at least 44px on a touch screen.
- With dropdown and multiple, the trigger is a button named by the label and the words ("Home type 2 types"). The checkboxes are the same native controls in a named dialog.
FilterChoice
FreeFor a choice filter: one select, or option chips when several may be picked, such as home types.
- File
- FilterChoice.astro
- Group
- Forms
- Kind
- Interactive: inline script
- Examples
- 3
Examples
Drag the orange handle to set the width. Frames get real breakpoints.
- Static
- Interactive
- Framed
One choice
A field without multiple: one native select, its first option meaning any.
Force statemain.astro
examples/filterchoice/main.astro
---
import FilterChoice from "@astronomic-dev/ui/components/FilterChoice.astro";
import { fields } from "../_data/vega.ts";
// One choice: the home type as a select (a field without multiple).
const type = { ...fields.find((field) => field.key === "type")!, multiple: false };
---
<FilterChoice field={type} anyText="Any type" class="max-w-xs" />Option chips
A field with multiple: one native checkbox per option, drawn as a chip. Check House and Condo to search both.
Force statechips.astro
examples/filterchoice/chips.astro
---
import FilterChoice from "@astronomic-dev/ui/components/FilterChoice.astro";
import { fields } from "../_data/vega.ts";
// Several choices: the home type with multiple, as option chips.
const type = fields.find((field) => field.key === "type")!;
---
<FilterChoice field={type} value={["house"]} />Dropdown
With dropdown: several choices are checkboxes in a Popover, one choice is a Select with picker. The trigger says what is picked, as the server draws it.
This preview did not load.
dropdown.astro
examples/filterchoice/dropdown.astro
---
import FilterChoice from "@astronomic-dev/ui/components/FilterChoice.astro";
import { fields } from "../_data/vega.ts";
const type = fields.find((field) => field.key === "type")!;
---
<div class="grid max-w-md gap-5">
<FilterChoice dropdown field={type} value={["house", "condo"]} />
<FilterChoice dropdown field={{ ...type, multiple: false }} />
</div>Minimal · Light