AncrelDocumentationv0.3.0

Search documentation

    Documentation / frontend

    筛选栏 FilterBar

    从 @ancrel/workspace 导入 FilterBar。输入框每次编辑都触发 onValueChange,父组件更新 value 后才会显示新的查询。给 label 传入可访问名称,例如“搜索项目”。占位文字适合提示可以搜索的内容。

    Live example

    Open playground ↗
    • Atlas
    • Beacon
    • Cedar

    App.tsx

    'use client'
    
    import { useState } from 'react'
    import { Badge, Button, Select } from '@ancrel/ui'
    import { FilterBar } from '@ancrel/workspace'
    import '@ancrel/tokens/css'
    import '@ancrel/ui/styles.css'
    import '@ancrel/workspace/styles.css'
    
    const entries = [
      { name: 'Atlas', region: 'East', pinned: true },
      { name: 'Beacon', region: 'East', pinned: false },
      { name: 'Cedar', region: 'West', pinned: true },
    ]
    const labels = { search: 'Find a project', clear: 'Reset filters', count: (n: number) => `Active filters: ${n}` }
    
    export default function App() {
      const [value, setValue] = useState('')
      const [pinned, setPinned] = useState(false)
      const [region, setRegion] = useState('')
      const count = Number(value !== '') + Number(pinned) + Number(region !== '')
      const results = entries.filter(item => item.name.toLowerCase().includes(value.toLowerCase())
        && (!pinned || item.pinned) && (!region || item.region === region))
    
      return <>
        <FilterBar label={labels.search} value={value} onValueChange={setValue}
          filters={<>
            <div role="group" aria-label="Project selection">
              <Button aria-pressed={!pinned} onClick={() => setPinned(false)}>All</Button>
              <Button aria-pressed={pinned} onClick={() => setPinned(true)}>Pinned</Button>
            </div>
            <Select aria-label="Region" value={region} onChange={e => setRegion(e.currentTarget.value)}>
              <option value="">All regions</option><option>East</option><option>West</option>
            </Select>
          </>}
          actions={<>
            <Badge>{labels.count(count)}</Badge>
            <Button disabled={!count} onClick={() => { setValue(''); setPinned(false); setRegion('') }}>
              {labels.clear}
            </Button>
          </>} />
        <ul aria-label="Projects">{results.map(item => <li key={item.name}>{item.name}</li>)}</ul>
      </>
    }
    

    Expected behavior

    Choose Pinned and East, then type “at”. Atlas is the only project and the active count is three. Reset filters restores all three projects.

    组合筛选条件

    用 filters 放置分段按钮、下拉框等控件,为每个控件提供名称。只有搜索条件时,非空查询会显示英文计数和 Clear 按钮。清除操作调用 onValueChange('')。

    有其他筛选条件时,父组件知道哪些值代表“全部”。示例由父组件计算启用数,通过 actions 显示计数和本地化按钮,并在一次操作中重置全部条件。Tab 按页面中的控件顺序移动焦点。

    如果搜索需要异步加载,可把查询交给同包的 createStaleSafeLoader。输入框可以立即更新,加载器负责延迟请求。

    Props

    PropTypeRequiredDefault
    actionsReactNodeNo—
    classNamestringNo—
    filtersReactNodeNo—
    keyReact.Key | null | undefinedNo—
    labelstringYes—
    onValueChange(value: string) => voidYes—
    placeholderstringNo—
    refReact.Ref<HTMLDivElement> | undefinedNo—
    valuestringYes—

    State matrix

    StateTheme / density
    defaultdark/compact, dark/comfortable, light/compact, light/comfortable

    Constraints

    • Keep the query and criteria in application state.
    • When supplying filters, use actions to report their active count and reset them together.

    Anti-patterns

    • Counting mounted controls as active criteria.