JGuang

标签

Chip · Material UI

交互预览
npm install @mui/material@9.4.0 @emotion/react@11.14.0 @emotion/styled@11.14.1

标签

展示紧凑的实体、筛选条件或可删除选项。

本指南覆盖默认 Emotion 环境的 Material UI Core;扩展包与额外示例按清单单独标注。

使用场景与业务边界

可删除 Chip 需要 onDelete 和宿主状态更新;它不是文本输入或持久化服务。

原生组成与范围

Chip

保持 Material UI 原生组合与类型,不替换为 JGuang/Base UI/Radix 同名 API。示例条目不是额外组件。

原生安装与初始化

npm install @mui/material@9.4.0 @emotion/react@11.14.0 @emotion/styled@11.14.1

采用原生 npm 包与 Emotion,不提供虚构的 JGuang UI Registry 地址。将示例放在原生 ThemeProvider 内,按项目选择 CssBaseline 或 ScopedCssBaseline。先检查 React、现有 Provider、锁文件与 SSR 配置;不要覆盖用户文件或忽略 peer 冲突。本批实际验收 React 19.2.8;React 18 及以下须匹配 react-is。完整初始化规则见随附的双语 setup 文档。

关键原生参数

仅解释本指南选出的关键参数,不是平铺的通用 API。原生继承来源、slot 结构、可空值与泛型仍按固定源码保留。

所属组件.属性用途说明声明来源
Chip.label原生可见标签/内容,不一定是底层值。Chip
Chip.onDelete原生 Chip 删除请求,由宿主移除相应数据项。Chip
Chip.onClick原生激活处理函数,连接真实操作,不覆盖禁用行为。React.HTMLAttributes
Chip.clickable让 Chip 展示可点击样式,不提供业务逻辑。Chip
Chip.disabled原生禁用交互状态,不是权限校验。Chip

可运行原生示例

下列机器键只选择示例入口,不会作为通用 variant 属性传入每个组件。原生示例 JSX 与数据保持上游原文;JGuang 编写的验收示例明确标注。

  • 基础标签 · BasicChips · 上游原生示例
  • 可点击 · ClickableChips · 上游原生示例
  • 可删除 · DeletableChips · 上游原生示例
  • 可点击与删除 · ClickableAndDeletableChips · 上游原生示例
  • 可点击链接 · ClickableLinkChips · 上游原生示例
  • 自定义删除图标 · CustomDeleteIconChips · 上游原生示例
  • 头像标签 · AvatarChips · 上游原生示例
  • 图标标签 · IconChips · 上游原生示例
  • 标签颜色 · ColorChips · 上游原生示例
  • 标签尺寸 · SizesChips · 上游原生示例
  • 多行标签 · MultilineChips · 上游原生示例
  • 标签列表 · ChipsArray · 上游原生示例

尚未支持的额外示例

这些条目仍保留在上游清单中,不静默跳过,也不展示为已完成预览。需要补充依赖/框架验收后再纳入可运行列表。

  • 标签演示 · ChipsPlayground: @mui/internal-core-docs/HighlightedCode

真实使用示例

固定版本的未修改原生示例;复制时同时保留所需的局部辅助文件。

import Chip from '@mui/material/Chip';
import Stack from '@mui/material/Stack';

export default function BasicChips() {
  return (
    <Stack direction="row" spacing={1}>
      <Chip label="Chip Filled" />
      <Chip label="Chip Outlined" variant="outlined" />
    </Stack>
  );
}

示例依赖边界

@mui/material/Chip, @mui/material/Stack, @mui/icons-material/Done, @mui/icons-material/Delete, @mui/material/Avatar, @mui/icons-material/Face, @mui/material/Box, react, @mui/material/styles, @mui/material/Paper, @mui/icons-material/TagFaces

直接导入 Icons/System/Utils 的示例需要匹配 9.4.0 包。局部数据与辅助文件属于示例闭包,不是额外组件。远程图片和模拟操作不提供后端。

固定上游 API 元数据参考

以下表格保留上游元数据,不是重新推断的通用类型体系。条件泛型、继承属性与回调细节以以下源码声明为准。主题默认配置可能覆盖元数据默认值。

Chip

import Chip from '@mui/material/Chip';
import { Chip } from '@mui/material';
属性必需上游元数据类型/约束元数据默认值
avatar否element—
children否unsupportedProp—
classes否object—
clickable否bool—
color否'default' | 'primary' | 'secondary' | 'error' | 'info' | 'success' | 'warning' | string'default'
component否elementType—
deleteIcon否element—
disabled否boolfalse
icon否element—
label否node—
nativeButton否bool—
onDelete否func—
size否'medium' | 'small' | string'medium'
skipFocusWhenDisabled否boolfalse
slotProps否{ label?: func | object, root?: func | object }{}
slots否{ label?: elementType, root?: elementType }{}
sx否Array<func | object | bool> | func | object—
variant否'filled' | 'outlined' | string'filled'

原生源码声明

声明与 import 从固定源码提取,不平铺联合类型或外部继承。英文注释/类型属于原始源码参考,不代表上游正文已全文翻译。共享类型仍位于原生包中。

vendor/src/Chip/Chip.d.ts

import * as React from 'react';

import { OverridableStringUnion } from '@mui/types';

import { SxProps } from '@mui/system';

import { CreateSlotsAndSlotProps, SlotProps } from '../utils/types';

import { Theme } from '../styles';

import { OverridableComponent, OverrideProps } from '../OverridableComponent';

import { ChipClasses } from './chipClasses';

export interface ChipSlots {
  /**
   * The component that renders the root.
   * @default div
   */
  root: React.ElementType;
  /**
   * The component that renders the label.
   * @default span
   */
  label: React.ElementType;
}

export type ChipSlotsAndSlotProps = CreateSlotsAndSlotProps<
  ChipSlots,
  {
    /**
     * Props forwarded to the root slot.
     * By default, the available props are based on the div element.
     */
    root: SlotProps<'div', {}, ChipOwnerState>;
    /**
     * Props forwarded to the label slot.
     * By default, the available props are based on the span element.
     */
    label: SlotProps<'span', {}, ChipOwnerState>;
  }
>;

export interface ChipOwnerState extends Omit<ChipProps, 'slots' | 'slotProps'> {}

export interface ChipPropsVariantOverrides {}

export interface ChipPropsSizeOverrides {}

export interface ChipPropsColorOverrides {}

export interface ChipOwnProps {
  /**
   * The Avatar element to display.
   */
  avatar?: React.ReactElement<unknown> | undefined;
  /**
   * This prop isn't supported.
   * Use the `component` prop if you need to change the children structure.
   */
  children?: null | undefined;
  /**
   * Override or extend the styles applied to the component.
   */
  classes?: Partial<ChipClasses> | undefined;
  /**
   * If `true`, the chip will appear clickable, and will raise when pressed,
   * even if the onClick prop is not defined.
   * If `false`, the chip will not appear clickable, even if onClick prop is defined.
   * This can be used, for example,
   * along with the component prop to indicate an anchor Chip is clickable.
   * Note: this controls the UI and does not affect the onClick event.
   */
  clickable?: boolean | undefined;
  /**
   * The color of the component.
   * It supports both default and custom theme colors, which can be added as shown in the
   * [palette customization guide](https://mui.com/material-ui/customization/palette/#custom-colors).
   * @default 'default'
   */
  color?:
    | OverridableStringUnion<
        'default' | 'primary' | 'secondary' | 'error' | 'info' | 'success' | 'warning',
        ChipPropsColorOverrides
      >
    | undefined;
  /**
   * Override the default delete icon element. Shown only if `onDelete` is set.
   */
  deleteIcon?: React.ReactElement<unknown> | undefined;
  /**
   * If `true`, the component is disabled.
   * @default false
   */
  disabled?: boolean | undefined;
  /**
   * Icon element.
   */
  icon?: React.ReactElement<unknown> | undefined;
  /**
   * The content of the component.
   */
  label?: React.ReactNode;
  /**
   * Callback fired when the delete icon is clicked.
   * If set, the delete icon will be shown.
   */
  onDelete?: React.EventHandler<any> | undefined;
  /**
   * The size of the component.
   * @default 'medium'
   */
  size?: OverridableStringUnion<'small' | 'medium', ChipPropsSizeOverrides> | undefined;
  /**
   * If `true`, the component is expected to resolve to a native `<button>` element.
   * When omitted, custom components inherit the default button semantics of the current wrapper.
   * Set to `true` when a custom component resolves to a native `<button>`, or `false`
   * when it resolves to a non-button host.
   */
  nativeButton?: boolean | undefined;
  /**
   * If `true`, allows the disabled chip to escape focus.
   * If `false`, allows the disabled chip to receive focus.
   * @default false
   */
  skipFocusWhenDisabled?: boolean | undefined;
  /**
   * The system prop that allows defining system overrides as well as additional CSS styles.
   */
  sx?: SxProps<Theme> | undefined;
  /**
   *  @ignore
   */
  tabIndex?: number | undefined;
  /**
   * The variant to use.
   * @default 'filled'
   */
  variant?: OverridableStringUnion<'filled' | 'outlined', ChipPropsVariantOverrides> | undefined;
}

export interface ChipTypeMap<
  AdditionalProps = {},
  RootComponent extends React.ElementType = 'div',
> {
  props: AdditionalProps & ChipOwnProps & ChipSlotsAndSlotProps;
  defaultComponent: RootComponent;
}

declare const Chip: OverridableComponent<ChipTypeMap>;

export type ChipProps<
  RootComponent extends React.ElementType = ChipTypeMap['defaultComponent'],
  AdditionalProps = {},
> = OverrideProps<ChipTypeMap<AdditionalProps, RootComponent>, RootComponent> & {
  component?: React.ElementType | undefined;
};

安装后验收

执行严格类型检查、生产构建和实际交互。检查键盘标签/焦点、原生支持的禁用/错误/只读语义、深浅色、窄屏和减少动画。按真实宿主数据检查回调,不把复制提示词当作安装完成。初始化工具还需检查服务端 HTML 与无告警水合。

来源与许可

https://github.com/mui/material-ui/tree/809a7717b4c050ba3f69b75300689f07c050a16e

MIT · @mui/material@9.4.0. 保留上游原始内容与许可证。上游完整原文保留于 vendor/docs,不标为全文中文化。