• 简体中文
  • AgentCharacterSelector

    先暂存、确认后才生效的 CharacterSelector。

    已提交 hex、blue——写入 0 次
    形状
    配色
    import { useState } from 'react';
    import { AgentPortrait, AgentCharacterSelector, type CharacterChoice } from '@aora-bot/react';
    
    export default function Demo() {
      const [committed, setCommitted] = useState<CharacterChoice>({ shape: 'hex', color: 'blue' });
      const [writes, setWrites] = useState(0);
      return (
        <div className="site-demo site-demo--column">
          <div className="site-demo">
            <AgentPortrait agent={{ id: 'nova', ...committed }} size="xl" />
            <span className="site-demo__label">
              已提交 {committed.shape}、{committed.color}——写入 {writes} 次
            </span>
          </div>
          <AgentCharacterSelector
            value={committed}
            shapes={['blob', 'hex', 'cloud', 'star', 'tofu', 'drop']}
            colors={['blue', 'green', 'orange', 'violet', 'red']}
            onCommit={(next) => {
              setCommitted(next);
              setWrites((n) => n + 1);
            }}
          />
        </div>
      );
    }

    一次点击是一个提议

    普通选择器是受控组件:每次点击都由宿主来应用。这在设置表单里没问题,但在对话框里不合适,因为对话框中的点击只是提议,取消必须真正生效。这个组件把提议保存在本地,确认时才提交一个值。这样宿主每个对话框只写一次存储,而不是每次点击写一次,页头的头像也不会在用户犹豫时来回切换五种形状。

    确认时,onCommit 会收到暂存的 CharacterChoice;放弃时触发 onCancel,暂存的修改重置为 value。immediate 会关闭暂存,每次选择都立即提交,适用于设置表单,宿主不必更换组件。

    图片与操作按钮

    hasPicture 表示智能体有自己的照片。此时网格不显示选中环,因为屏幕上显示的是照片,而不是下面的角色;给一个看不见的六边形加高亮,会误导用户。选中状态仍会通知辅助技术。

    actions={false} 会隐藏确认和放弃按钮,适合把按钮放在对话框底栏的宿主。要用自己的按钮提交时,保持 immediate 关闭,并把选择器放进由你控制提交的表单里。

    API

    属性类型默认值说明
    value*CharacterChoice—The committed appearance. Staged edits start from here and reset to it on cancel.
    actionsbooleantrueShow the confirm and discard buttons. Off when a host supplies its own.
    classNamestring——
    colorsreadonly ColorId[]——
    hasPicturebooleanfalseThe agent has a picture of its own. The grid then shows no selection ring, because the character underneath is not what is on screen — highlighting a shape would claim the avatar is something it is not. Picking still stages a choice, so confirming swaps the picture out for the character.
    immediatebooleanfalseCommit on every pick instead of waiting for confirmation.
    layout"grid" | "row"'grid'—
    lineWeightnumber——
    onCancel(() => void)—Called when the staged appearance is discarded.
    onCommit((choice: CharacterChoice) => void)—Called with the staged appearance when it is confirmed.
    shapesreadonly ShapeId[]——
    styleCSSProperties——

    接下来