Ordo UI
PatternsApp Patterns

FilterChips

@mox-hub/patterns 模式 · App Patterns · canonical

filter-chips@mox-hub/patternsApp Patternscanonicalsrc/components/filter-chips.tsx
变体演示filter-chips
状态

Props API

Prop类型默认值说明
fields*FilterFieldDef[]——
conditions*FilterCondition[]——
onChange*(conditions: FilterCondition[]) => void——
onSaveToView*() => void——
onSaveAsNewView*(name: string) => void——
aiId*string——
className*string——
translate*PatternTranslate——

源码

packages/patterns/src/components/filter-chips.tsx
/**
 * FilterChipsRow — Linear 风格筛选条件条(工具栏之下单开一行)。
 *
 * 模型:FilterCondition[](字段 + 算子 + 值集),由页面元数据 FilterFieldDef 驱动;
 * 页面用 filterConditionSets / matchesConditionSets 把条件聚合为 包含/排除 集合做谓词判断,
 * 视图快照直接存 conditions 数组(JSON 可序列化,兼容 useToolbarViews)。
 *
 * 结构参照 Linear 筛选设计:[字段图标+名称|算子|值(图标堆叠+文案)|×] 四段拼接 chip,
 * 行尾 + 追加条件(允许同字段多条件),右侧 Clear / Save(保存到当前视图·另存为新视图)。
 */
import { Fragment, useRef, useState, type KeyboardEvent as ReactKeyboardEvent, type ReactNode } from "react";
import type { IconComponent } from '../lib/types';
import { type PatternTranslate } from '../lib/translate';
import { usePatternTranslate } from '../lib/i18n';
import { Check, ChevronDown, Filter, Plus, X } from "lucide-react";
import { cn } from "@mox-hub/ui";
import { RawButton } from "@mox-hub/ui/components/raw/raw-button";
import { AnchoredMenu } from "@mox-hub/ui/components/anchored-menu";
import { Button } from "@mox-hub/ui/components/button";
import { Checkbox } from "@mox-hub/ui/components/checkbox";
import { HeaderActionButton } from "./header-action-button";
import { Input } from "@mox-hub/ui/components/input";
import {
  Menu,
  MenuCheckboxItem,
  MenuPopup,
  MenuSub,
  MenuSubPopup,
  MenuSubTrigger,
  MenuTrigger,
} from "@mox-hub/ui/components/menu";
import {
  Command,
  CommandCollection,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
  CommandSeparator,
} from "@mox-hub/ui/components/command";
import { useAutocompleteFilter } from "@mox-hub/ui/components/autocomplete";
import { MENU_ITEM_CLASS } from "@mox-hub/ui/components/menu-surface";

/* ────────────────────────────── 模型 ────────────────────────────── */

export type FilterOperatorId = "is" | "isNot" | "include" | "notInclude";

export interface FilterValueOption {
  value: string;
  label: string;
  /** 候选值图标(状态图标/色点/头像等,页面自定义) */
  icon?: ReactNode;
  /** 候选值右侧辅助文字(如计数) */
  hint?: string;
}

export interface FilterFieldDef {
  id: string;
  label: string;
  icon?: IconComponent;
  /** 该字段支持的算子,首个为新建条件时的默认值 */
  operators: FilterOperatorId[];
  options: FilterValueOption[];
  /** 值面板是否带搜索框(候选较多时开,如项目) */
  searchable?: boolean;
}

export interface FilterCondition {
  id: string;
  fieldId: string;
  operator: FilterOperatorId;
  values: string[];
}

let conditionSeq = 0;

export function createFilterCondition(fieldId: string, operator?: FilterOperatorId): FilterCondition {
  conditionSeq += 1;
  return {
    id: `cond-${Date.now().toString(36)}-${conditionSeq}`,
    fieldId,
    operator: operator ?? "is",
    values: [],
  };
}

/**
 * 聚合某字段的 包含/排除 集合:is/include → include,isNot/notInclude → exclude。
 * include 为空 = 该字段不限制。
 */
export function filterConditionSets(
  conditions: FilterCondition[],
  fieldId: string,
): { include: string[]; exclude: string[] } {
  const include: string[] = [];
  const exclude: string[] = [];
  for (const condition of conditions) {
    if (condition.fieldId !== fieldId || condition.values.length === 0) continue;
    if (condition.operator === "isNot" || condition.operator === "notInclude") {
      exclude.push(...condition.values);
    } else {
      include.push(...condition.values);
    }
  }
  return { include, exclude };
}

/** 单值字段判定:include 空则不限;命中 exclude 一票否决 */
export function matchesConditionSets(
  value: string | null | undefined,
  sets: { include: string[]; exclude: string[] },
): boolean {
  const v = value ?? "";
  return (sets.include.length === 0 || sets.include.includes(v)) && !sets.exclude.includes(v);
}

/** 按键聚合计数(值菜单 hint 计数用;null/undefined 键跳过) */
export function countBy<T>(
  items: T[],
  keyOf: (item: T) => string | null | undefined,
): Map<string, number> {
  const counts = new Map<string, number>();
  for (const item of items) {
    const key = keyOf(item);
    if (!key) continue;
    counts.set(key, (counts.get(key) ?? 0) + 1);
  }
  return counts;
}

const OPERATOR_I18N_KEY: Record<FilterOperatorId, string> = {
  is: "common.filterOpIs",
  isNot: "common.filterOpIsNot",
  include: "common.filterOpInclude",
  notInclude: "common.filterOpNotInclude",
};

/** 组件内文案缺省(zh-CN;消费方经 translate 注入 i18n t 包装)。 */
const FILTER_CHIPS_LABELS: Record<string, string> = {
  "common.delete": "删除",
  "common.filterAdd": "添加筛选",
  "common.filterClear": "清除",
  "common.filterCreate": "创建",
  "common.filterNewViewName": "视图名称",
  "common.filterValuesSelected": "已选 {{count}} 项",
  "common.filterNoResults": "无匹配结果",
  "common.filterSave": "保存",
  "common.filterSaveAsView": "另存为新视图…",
  "common.filterSaveToView": "保存到当前视图",
  "common.filterSelectValue": "选择值",
  "common.filters": "筛选",
  "common.filterOpIs": "是",
  "common.filterOpIsNot": "不是",
  "common.filterOpInclude": "包含",
  "common.filterOpNotInclude": "不包含",
};


/* ─────────────────────────── 内部子组件 ─────────────────────────── */

/** chip 拼接段的公共底色:首段圆左、末段圆右、中间直角 */
const SEGMENT_CLASS =
  "flex h-control-xs items-center bg-muted text-xs text-muted-foreground transition-colors hover:bg-muted hover:text-foreground pointer-coarse:min-h-11";

function OperatorMenu({
  field,
  operator,
  onOperatorChange,
  translate,
}: {
  field: FilterFieldDef;
  operator: FilterOperatorId;
  onOperatorChange: (operator: FilterOperatorId) => void;
  translate?: PatternTranslate;
}) {
  const providerT = usePatternTranslate('filterChips', FILTER_CHIPS_LABELS);
  const t = translate ?? providerT;
  const [open, setOpen] = useState(false);
  const anchorRef = useRef<HTMLButtonElement>(null);

  return (
    <>
      <RawButton
        ref={anchorRef}
        type="button"
        onClick={() => setOpen((prev) => !prev)}
        aria-haspopup="menu"
        aria-expanded={open}
        className={cn(SEGMENT_CLASS, "px-1.5")}
      >
        {t(OPERATOR_I18N_KEY[operator])}
      </RawButton>
      <AnchoredMenu open={open} onClose={() => setOpen(false)} anchor={anchorRef}>
        <div className="min-w-24">
          {field.operators.map((op) => (
            <RawButton
              key={op}
              type="button"
              className={cn(MENU_ITEM_CLASS, "text-xs")}
              onClick={() => {
                onOperatorChange(op);
                setOpen(false);
              }}
            >
              <span className="flex-1">{t(OPERATOR_I18N_KEY[op])}</span>
              {op === operator ? (
                <Check className="ms-2 size-3.5 shrink-0 text-primary" strokeWidth={2.5} />
              ) : null}
            </RawButton>
          ))}
        </div>
      </AnchoredMenu>
    </>
  );
}

function ValueMenu({
  field,
  values,
  autoOpen,
  onValuesChange,
  translate,
}: {
  field: FilterFieldDef;
  values: string[];
  /** 新建空值条件时首开面板(仅初始态,之后随用户交互) */
  autoOpen?: boolean;
  onValuesChange: (values: string[]) => void;
  translate?: PatternTranslate;
}) {
  const providerT = usePatternTranslate('filterChips', FILTER_CHIPS_LABELS);
  const t = translate ?? providerT;
  const [open, setOpen] = useState(autoOpen ?? false);
  const [query, setQuery] = useState("");
  const anchorRef = useRef<HTMLButtonElement>(null);

  const close = () => {
    setOpen(false);
    setQuery("");
  };
  const toggle = (value: string) =>
    onValuesChange(
      values.includes(value) ? values.filter((v) => v !== value) : [...values, value],
    );

  // coss p-command 引擎同款过滤(accent/大小写不敏感 contains,中文子串可用)
  const { contains } = useAutocompleteFilter({ sensitivity: "base" });
  const trimmedQuery = query.trim();
  const selected = field.options.filter((option) => values.includes(option.value));
  const visibleRest = field.options
    .filter((option) => !values.includes(option.value))
    .filter((option) => !trimmedQuery || contains(option.label, trimmedQuery));

  // 数据驱动 groups:mode="none" 静态渲染(过滤自管),空组不进 items 使空态正确判定
  const menuGroups = [
    ...(selected.length > 0 ? [{ value: "selected", items: selected }] : []),
    ...(visibleRest.length > 0 ? [{ value: "rest", items: visibleRest }] : []),
  ];

  // base-ui 高亮项经 onItemHighlighted 记录,回车切换选中(与旧 cmdk onSelect 语义对齐)
  const highlightedRef = useRef<FilterValueOption | null>(null);
  const handleInputKeyDown = (event: ReactKeyboardEvent<HTMLInputElement>) => {
    if (event.key === "Enter" && highlightedRef.current) {
      event.preventDefault();
      toggle(highlightedRef.current.value);
    }
  };

  const renderOption = (option: FilterValueOption, checked: boolean) => (
    <CommandItem
      key={option.value}
      value={option}
      onClick={() => toggle(option.value)}
      className="text-xs"
    >
      <Checkbox className="me-1" checked={checked} render={<span aria-hidden="true" />} />
      {option.icon}
      <span className="truncate">{option.label}</span>
      {option.hint ? (
        <span className="ms-auto ps-2 text-xs text-muted-foreground">{option.hint}</span>
      ) : null}
    </CommandItem>
  );

  return (
    <>
      <RawButton
        ref={anchorRef}
        type="button"
        onClick={() => setOpen((prev) => !prev)}
        aria-haspopup="listbox"
        aria-expanded={open}
        className={cn(SEGMENT_CLASS, "max-w-60 gap-1.5 px-1.5")}
      >
        {selected.length > 0 ? (
          <>
            <span className="flex items-center -space-x-1">
              {selected.slice(0, 3).map((option) => (
                <span key={option.value} className="inline-flex shrink-0">
                  {option.icon}
                </span>
              ))}
            </span>
            <span className="truncate">
              {selected.length === 1
                ? selected[0].label
                : t("common.filterValuesSelected", { count: selected.length })}
            </span>
          </>
        ) : (
          <span>{t("common.filterSelectValue")}</span>
        )}
      </RawButton>
      <AnchoredMenu open={open} onClose={close} anchor={anchorRef}>
        <div className="w-56">
          <Command
            mode="none"
            items={menuGroups}
            onItemHighlighted={(value) => {
              highlightedRef.current = (value as FilterValueOption | undefined) ?? null;
            }}
          >
            {field.searchable ? (
              <CommandInput
                value={query}
                onChange={(event) => setQuery(event.target.value)}
                onKeyDown={handleInputKeyDown}
                placeholder={field.label}
                className="h-control-sm text-xs"
              />
            ) : null}
            <CommandList scrollAreaClassName="[&_[data-slot=scroll-area-viewport]]:max-h-64">
              <CommandEmpty>{t("common.filterNoResults")}</CommandEmpty>
              {((group: (typeof menuGroups)[number]) => (
                <Fragment key={group.value}>
                  {group.value === "rest" ? <CommandSeparator /> : null}
                  <CommandGroup items={group.items} className="py-1">
                    <CommandCollection>
                      {(option: FilterValueOption) =>
                        renderOption(option, group.value === "selected")
                      }
                    </CommandCollection>
                  </CommandGroup>
                </Fragment>
              )) as unknown as ReactNode}
            </CommandList>
          </Command>
        </div>
      </AnchoredMenu>
    </>
  );
}

function FilterChip({
  field,
  condition,
  onChange,
  onRemove,
  translate,
}: {
  field: FilterFieldDef;
  condition: FilterCondition;
  onChange: (patch: Partial<FilterCondition>) => void;
  onRemove: () => void;
  translate?: PatternTranslate;
}) {
  const providerT = usePatternTranslate('filterChips', FILTER_CHIPS_LABELS);
  const t = translate ?? providerT;
  const FieldIcon = field.icon;

  return (
    <div className="flex shrink-0 items-center" role="group" aria-label={field.label}>
      <span className="flex h-control-xs items-center gap-1.5 rounded-s-md bg-muted px-1.5 text-xs text-foreground">
        {FieldIcon ? <FieldIcon className="size-3.5 text-muted-foreground" strokeWidth={1.75} /> : null}
        {field.label}
      </span>
      <OperatorMenu
        field={field}
        operator={condition.operator}
        onOperatorChange={(operator) => onChange({ operator })}
        translate={translate}
      />
      <ValueMenu
        field={field}
        values={condition.values}
        autoOpen={condition.values.length === 0}
        onValuesChange={(values) => onChange({ values })}
        translate={translate}
      />
      <RawButton
        type="button"
        aria-label={t("common.delete")}
        title={t("common.delete")}
        onClick={onRemove}
        className={cn(SEGMENT_CLASS, "w-6 justify-center rounded-e-md pointer-coarse:w-11")}
      >
        <X className="size-3" />
      </RawButton>
    </div>
  );
}

/* ─────────────────── 二级级联筛选菜单(漏斗按钮,Linear 形态) ─────────────────── */

/**
 * 漏斗按钮 + 两级菜单:顶部「添加筛选」搜索过滤字段,字段项 hover 展开值子菜单
 * (图标 + 计数 hint + 勾选态),勾选/取消直接增删同字段条件的值——与 FilterChipsRow
 * 操作同一份 conditions 状态,可独立使用也可配合条件条调整。
 */
export function FilterCascadeMenu({
  fields,
  conditions,
  onChange,
  badge,
  search,
  aiId,
  translate,
}: {
  fields: FilterFieldDef[];
  conditions: FilterCondition[];
  onChange: (conditions: FilterCondition[]) => void;
  /** 漏斗按钮右上角生效筛选数角标 */
  badge?: number;
  /** 页面内容搜索框(保持旧版漏斗菜单内的搜索能力) */
  search?: { value: string; onChange: (value: string) => void; placeholder?: string };
  aiId?: string;
  /** 文案覆写(默认 zh-CN 词表;主仓 i18n 适配层集中传入 t() 包装)。 */
  translate?: PatternTranslate;
}) {
  const providerT = usePatternTranslate('filterChips', FILTER_CHIPS_LABELS);
  const t = translate ?? providerT;
  const [open, setOpen] = useState(false);
  const [query, setQuery] = useState("");

  const keyword = query.trim().toLowerCase();
  const visibleFields = keyword
    ? fields.filter((field) => field.label.toLowerCase().includes(keyword))
    : fields;

  const toggleFieldValue = (field: FilterFieldDef, value: string) => {
    const existing = conditions.find((c) => c.fieldId === field.id);
    if (!existing) {
      onChange([
        ...conditions,
        { ...createFilterCondition(field.id, field.operators[0]), values: [value] },
      ]);
      return;
    }
    const values = existing.values.includes(value)
      ? existing.values.filter((v) => v !== value)
      : [...existing.values, value];
    onChange(
      values.length === 0
        ? conditions.filter((c) => c.id !== existing.id)
        : conditions.map((c) => (c.id === existing.id ? { ...c, values } : c)),
    );
  };

  return (
    <Menu open={open} onOpenChange={setOpen}>
      <span className="relative inline-flex shrink-0">
        <MenuTrigger
          render={
            <HeaderActionButton
              icon={Filter}
              label={t("common.filters")}
              variant="outline"
              pinned={open}
              aria-haspopup="menu"
              aria-expanded={open}
              data-ai-component={aiId ?? "ui.filter-cascade-menu"}
            />
          }
        />
        {badge && badge > 0 ? (
          <span className="pointer-events-none absolute -top-1 end-0 flex h-4 min-w-4 items-center justify-center rounded-full bg-destructive px-1 text-xs font-medium leading-none text-destructive-foreground">
            {badge > 99 ? "99+" : badge}
          </span>
        ) : null}
      </span>
      <MenuPopup align="start" className="w-60">
        {search ? (
          <div className="p-1 pb-0">
            <Input
              value={search.value}
              onChange={(event) => search.onChange(event.target.value)}
              onKeyDown={(event) => event.stopPropagation()}
              placeholder={search.placeholder}
              className="h-control-sm text-xs"
            />
          </div>
        ) : null}
        <div className="p-1 pb-0">
          <Input
            value={query}
            onChange={(event) => setQuery(event.target.value)}
            onKeyDown={(event) => event.stopPropagation()}
            placeholder={t("common.filterAdd")}
            className="h-control-sm text-xs"
          />
        </div>
        <div className="mt-1">
          {visibleFields.map((field) => {
            const FieldIcon = field.icon;
            const fieldCondition = conditions.find((c) => c.fieldId === field.id);
            return (
              <MenuSub key={field.id}>
                <MenuSubTrigger className="text-xs">
                  {FieldIcon ? <FieldIcon className="text-muted-foreground" strokeWidth={1.75} /> : null}
                  <span className="flex-1 truncate">{field.label}</span>
                  {fieldCondition && fieldCondition.values.length > 0 ? (
                    <span className="ms-1 text-xs text-muted-foreground">
                      {fieldCondition.values.length}
                    </span>
                  ) : null}
                </MenuSubTrigger>
                <MenuSubPopup className="min-w-44">
                  {field.options.map((option) => (
                    <MenuCheckboxItem
                      key={option.value}
                      checked={fieldCondition?.values.includes(option.value) ?? false}
                      closeOnClick={false}
                      onCheckedChange={() => toggleFieldValue(field, option.value)}
                      className="text-xs"
                    >
                      <span className="flex min-w-0 items-center gap-2">
                        {option.icon}
                        <span className="truncate">{option.label}</span>
                        {option.hint ? (
                          <span className="ms-auto ps-2 text-xs text-muted-foreground">
                            {option.hint}
                          </span>
                        ) : null}
                      </span>
                    </MenuCheckboxItem>
                  ))}
                </MenuSubPopup>
              </MenuSub>
            );
          })}
          {visibleFields.length === 0 ? (
            <div className="px-2 py-3 text-xs text-muted-foreground">
              {t("common.filterNoResults")}
            </div>
          ) : null}
        </div>
      </MenuPopup>
    </Menu>
  );
}

/* ─────────────────────── 字段清单(追加条件菜单) ─────────────────────── */

/** 字段清单:供 FilterChipsRow 行尾 + 与页面 ToolbarRow 漏斗按钮的 filterMenu.content 复用 */
export function FilterFieldMenuList({
  fields,
  onSelect,
}: {
  fields: FilterFieldDef[];
  onSelect: (fieldId: string) => void;
}) {
  return (
    <div className="min-w-36">
      {fields.map((field) => {
        const Icon = field.icon;
        return (
          <RawButton
            key={field.id}
            type="button"
            className={cn(MENU_ITEM_CLASS, "text-xs")}
            onClick={() => onSelect(field.id)}
          >
            {Icon ? <Icon className="me-2 size-4 shrink-0" strokeWidth={1.75} /> : null}
            <span className="flex-1 truncate">{field.label}</span>
            <Plus className="ms-2 size-3.5 shrink-0 text-muted-foreground" strokeWidth={1.75} />
          </RawButton>
        );
      })}
    </div>
  );
}

/* ─────────────────────────── FilterChipsRow ─────────────────────────── */

export interface FilterChipsRowProps {
  /** 可筛选字段定义(追加菜单与 chip 渲染的数据源) */
  fields: FilterFieldDef[];
  conditions: FilterCondition[];
  onChange: (conditions: FilterCondition[]) => void;
  /** 保存到当前视图(页面接 useToolbarViews 的当前快照即为最新态) */
  onSaveToView?: () => void;
  /** 另存为新视图,参数为用户输入的视图名 */
  onSaveAsNewView?: (name: string) => void;
  aiId?: string;
  className?: string;
  /** 文案覆写(默认 zh-CN 词表;主仓 i18n 适配层集中传入 t() 包装)。 */
  translate?: PatternTranslate;
}

export function FilterChipsRow({
  fields,
  conditions,
  onChange,
  onSaveToView,
  onSaveAsNewView,
  aiId,
  className,
  translate,
}: FilterChipsRowProps) {
  const providerT = usePatternTranslate('filterChips', FILTER_CHIPS_LABELS);
  const t = translate ?? providerT;
  const addAnchorRef = useRef<HTMLButtonElement>(null);
  const [addOpen, setAddOpen] = useState(false);
  const saveAnchorRef = useRef<HTMLButtonElement>(null);
  const [saveOpen, setSaveOpen] = useState(false);
  const [saveAsOpen, setSaveAsOpen] = useState(false);
  const [viewName, setViewName] = useState("");

  const closeSave = () => {
    setSaveOpen(false);
    setSaveAsOpen(false);
    setViewName("");
  };

  const updateCondition = (id: string, patch: Partial<FilterCondition>) =>
    onChange(conditions.map((c) => (c.id === id ? { ...c, ...patch } : c)));
  const removeCondition = (id: string) => onChange(conditions.filter((c) => c.id !== id));
  const addCondition = (fieldId: string) => {
    const field = fields.find((f) => f.id === fieldId);
    onChange([...conditions, createFilterCondition(fieldId, field?.operators[0])]);
    setAddOpen(false);
  };

  const canSave = Boolean(onSaveToView || onSaveAsNewView);

  return (
    <div
      className={cn(
        "flex items-center gap-1.5 rounded-lg border border-border bg-muted/30 px-2 py-1.5",
        className,
      )}
      data-ai-component={aiId ?? "ui.filter-chips-row"}
      data-ai-role="filter"
    >
      <div className="flex min-w-0 flex-1 items-center gap-1.5 overflow-x-auto">
        {conditions.map((condition) => {
          const field = fields.find((f) => f.id === condition.fieldId);
          if (!field) return null;
          return (
            <FilterChip
              key={condition.id}
              field={field}
              condition={condition}
              onChange={(patch) => updateCondition(condition.id, patch)}
              onRemove={() => removeCondition(condition.id)}
              translate={translate}
            />
          );
        })}
        <span className="relative shrink-0">
          <RawButton
            ref={addAnchorRef}
            type="button"
            aria-label={t("common.filterAdd")}
            title={t("common.filterAdd")}
            aria-haspopup="menu"
            aria-expanded={addOpen}
            onClick={() => setAddOpen((prev) => !prev)}
            className="flex size-control-xs items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-muted hover:text-foreground pointer-coarse:size-11"
          >
            <Plus className="size-3.5" strokeWidth={1.75} />
          </RawButton>
          <AnchoredMenu open={addOpen} onClose={() => setAddOpen(false)} anchor={addAnchorRef}>
            <FilterFieldMenuList fields={fields} onSelect={addCondition} />
          </AnchoredMenu>
        </span>
      </div>

      <div className="flex shrink-0 items-center gap-1">
        <Button
          variant="ghost"
          size="xs"
          className="text-muted-foreground"
          onClick={() => onChange([])}
        >
          {t("common.filterClear")}
        </Button>
        {canSave ? (
          <span className="relative">
            <Button
              ref={saveAnchorRef}
              variant="ghost"
              size="xs"
              className="gap-0.5 text-muted-foreground"
              aria-haspopup="menu"
              aria-expanded={saveOpen}
              onClick={() => setSaveOpen((prev) => !prev)}
            >
              {t("common.filterSave")}
              <ChevronDown className="size-3 opacity-70" />
            </Button>
            <AnchoredMenu open={saveOpen} onClose={closeSave} anchor={saveAnchorRef}>
              {saveAsOpen ? (
                <form
                  className="flex w-56 flex-col gap-2 p-2"
                  onSubmit={(event) => {
                    event.preventDefault();
                    onSaveAsNewView?.(viewName.trim() || "View");
                    closeSave();
                  }}
                >
                  <Input
                    autoFocus
                    value={viewName}
                    onChange={(event) => setViewName(event.target.value)}
                    placeholder={t("common.filterNewViewName")}
                    className="h-control-sm text-xs"
                  />
                  <Button type="submit" size="sm">
                    {t("common.filterCreate")}
                  </Button>
                </form>
              ) : (
                <div className="min-w-40">
                  {onSaveToView ? (
                    <RawButton
                      type="button"
                      className={cn(MENU_ITEM_CLASS, "text-xs")}
                      onClick={() => {
                        onSaveToView();
                        closeSave();
                      }}
                    >
                      <span className="flex-1">{t("common.filterSaveToView")}</span>
                    </RawButton>
                  ) : null}
                  {onSaveAsNewView ? (
                    <RawButton
                      type="button"
                      className={cn(MENU_ITEM_CLASS, "text-xs")}
                      onClick={() => setSaveAsOpen(true)}
                    >
                      <span className="flex-1">{t("common.filterSaveAsView")}</span>
                    </RawButton>
                  ) : null}
                </div>
              )}
            </AnchoredMenu>
          </span>
        ) : null}
      </div>
    </div>
  );
}

可访问性与 RTL

  • 键盘:交互件保留:focus-visible焦点环;浮层支持 Escape 关闭与焦点归还,菜单 / 页签按 Base UI 约定支持方向键。 筛选菜单、拖拽排序、执行树与模型 listbox 等关键键盘流由 Playwright 断言覆盖,见规范示例 · 交互与可访问性。
  • RTL:间距、对齐与方向性图标使用逻辑属性 (ms-* /text-start /rtl:-scale-x-100);通过 ThemeProvider 或I18nProvider 的direction="rtl"驱动,`lint:rtl` 会拦截新增物理类。
  • 文案:优先用组件labels /translate /translator props,其次用I18nProvider目录;内置 zh-CN 默认值,en-US 为参考翻译。见Theming · i18n 与 RTL。
  • 对比度:高对比度 token、2px 控件边框与 3px 焦点环由主题层统一提供,见Foundations · Contrast。

用法说明

Linear 风格筛选条件条:字段+算子+值集条件 chip,支持追加/删除/保存到视图;translate 注入文案,字段元数据由页面注入。