ion-item
Items 是可以包含文本、图标、头像、图像、输入以及任何其他原生或自定义元素的元素。Items 仅应作为 List 中的行与其他 items 一起使用。Items 可以滑动、删除、重新排序、编辑等。
基本用法
Items 默认左对齐文本,并在文本宽于 item 时自动换行。我们可以使用 Ionic Framework 提供的 CSS 实用工具来修改此行为,例如在下面的示例中使用 .ion-text-nowrap。请参阅CSS 实用工具文档了解更多可添加到 item 以转换文本的类。
内容类型
虽然列表中的 items 有多种形式,但它们通常支持 5 种不同的内容类型:辅助视觉元素、文本、元数据、操作和控件。但是,并非所有这些内容类型都应同时使用。以下指南展示了不同的内容类型以及如何在应用中正确使用它们。
辅助视觉元素
辅助视觉元素是 item 的装饰性图标或其他点缀。常见的辅助视觉元素示例包括头像、图标和缩略图。由于这些内容不是理解 item 意图所必需的,因此通常使用 aria-hidden="true" 对屏幕阅读器隐藏。
如果视觉元素是与 item 交互所必需的,例如图标按钮,则该视觉元素是操作而不是辅助视觉元素。
辅助视觉元素应以一致的方式呈现。这使每个 item 中的信息更易于解析。

在列表中将对齐视觉元素放在同一侧

不要在同一列表中呈现不同对齐方式的视觉元素
在下面的示例中,我们创建了两个带有辅助视觉元素的列表。第一个列表使用图标,第二个列表使用头像。视觉元素是装饰性的,因此它们都具有 aria-hidden="true"。此外,它们一致地呈现在 start 插槽中。
文本
文本内容类型包括表单控件标签或其他可见文本。此文本用于指示 item 的意图。尽量保持文本简短扼要。
如果您发现需要多写几句话来阐明 item 的目的,请考虑将附加句子移到列表底部的Note 中。将 item 放在自己的列表中可以清楚表明该文本与哪个 item 相关联。

将长文本移到列表外部

不要试图将长文本塞入 item 中
在下面的示例中,我们创建了一个包含不同类型文本的列表。"First Name" 和 "Last Name" 标签用于指示在文本输入框中输入的内容。
切换开关上的 "Allow Notifications" 标签下方有附加文本,提示用户可以禁用通知。由于这段文本很短,所以放在 item 内部。
该列表下方是另一个列表,包含一个 textarea,其下方有包含长文本的 Note。textarea 被放在自己的列表中,以表明长文本与 textarea 相关联,而不是其他字段。
元数据
元数据为 item 提供额外的上下文,例如状态文本或计数。像 Badge 或 Note 这样的组件是显示元数据的好方法。
限制您包含的元数据量,只保留最相关的信息。

只添加最重要的元数据

不要添加太多元数据,以免让用户感到不知所措或困惑。
开发人员还应考虑元数据的重要性。吸引用户注意元数据可能对用户有帮助,也可能分散他们对更重信息的注意力,具体取决于使用场景。

优先展示最重要的内容。

优先显示的元数据可能会分散对其他重要内容的注意力。
在下面的示例中,我们创建了两个具有不同元数据的列表。第一个列表使用 Note 来显示每个待办事项列表中的任务数量。
第二个列表模拟 iOS Mail 应用来显示收件箱。该列表使用了自定义元数据,包括"起始"插槽中的"未读消息"指示器,以及"结束"插槽中的时间戳和自定义详细信息图标。"未读消息"指示器以蓝色突出显示,以引起用户对未读消息的注意,而时间戳则更为微妙。
操作
操作是交互式元素,激活后会执行某些功能。一个 item 可以在同一行上显示多个操作。但是,开发者应确保每个操作的点击目标足够大以便于使用。
开发人员应避免创建可能破坏屏幕阅读器用户体验的嵌套交互。例如,如果 button 属性设置为 true,开发人员应避免在 Item 的主内容内部添加按钮。
可以使用 Item Sliding 组件添加操作。操作也可以直接放置在 Item 内部而不使用 Item Sliding,但应限制在不超过 2 个操作。

使用 Item Sliding 通过滑动 Item 来显示多个操作。

不要在 Item 中放置超过 2 个操作。
在下面的示例中,我们创建了一个联系人列表。每个项目是一个存根按钮,旨在将您带到该项目的完整联系人页面。每个项目都有关联的额外操作,用户可以通过滑动项目来显示。
控件
控件是表单组件,例如复选框、输入框、单选框等。由于屏幕空间限制,列表中的每个 item 应最多有两个控件。
帮助文本或字符计数等元数据不应在列表视图中的表单控件上使用。如果需要此类元数据,表单控件应放在列表外部。Filled Inputs 是在列表外部直观定义输入容器的好方法。

将带有元数据的输入放在列表外部。

不要在列表中放置输入的元数据。
或者,可以将元数据放在列表底部的 Note 中。

将输入的元数据放在列表末尾。

不要在列表中放置输入的元数据。
Items 通常应不超过两个控件。如果需要更多控件,请考虑将附加控件添加到可从 item 访问的 Modal 中。

将附加控件移到可从 item 访问的子菜单中。

不要在 item 中使用超过两个控件。
在下面的示例中,我们创建了一个待办任务列表。每个项目都有一个复选框和一个输入。复选框让用户将任务标记为完成,输入让用户更改任务名称。
可点击项目
如果 item 设置了 href 或 button 属性,则认为它是"可点击"的。可点击项目有一些视觉差异,表明它们可以进行交互。例如,可点击项目在 md 模式下激活时会有涟漪效果,在 ios 模式下激活时会有高亮效果,并且在 ios 模式下默认有详情箭头。
详情箭头
默认情况下,可点击项目在 ios 模式下会显示一个右箭头图标。要在可点击元素上隐藏右箭头图标,请将 detail 属性设置为 false。要在不自然显示它的项目上显示右箭头图标,请将 detail 属性设置为 true。
项目线条
Items 默认显示嵌入的下边框。边框左侧有内边距,不会出现在 "start" 插槽中的任何内容下方。lines 属性可以修改为 "full" 或 "none",分别显示全宽边框或不显示边框。
Item 中的按钮
按钮在 item 内部比在外部时样式更小。要使按钮大小与 item 外部的按钮匹配,请将 size 属性设置为 "default"。
Item 输入
主题
颜色
CSS Shadow Parts
CSS 自定义属性
指南
以下指南将有助于确保您的列表项易于理解和使用。
- Items 仅应在 Lists 内部使用。
- 列表中的 items 应以一致的格式呈现。例如,如果您的 items 显示装饰性图标,则图标应各 item 之间以相同方式定位。
- Items 绝不应呈现嵌套交互。使用嵌套交互时,屏幕阅读器无法选择正确的交互元素。例如,避免在设置了
button="true"的ion-item内部放置按钮。 - 正确使用内容类型。Item 组件被设计为 List 中的一行,不应作为通用容器使用。
辅助功能
键盘交互
当满足以下任一条件时,<ion-item> 具有以下键盘交互:
button属性设置为"true",渲染原生<button>元素。href属性已设置,渲染原生<a>元素。routerLink属性已设置,渲染原生<a>元素。
| 键 | 描述 |
|---|---|
| Tab | 将焦点移动到下一个可聚焦元素。 |
| Shift + Tab | 将焦点移动到上一个可聚焦元素。 |
按钮
当 <ion-item> 渲染原生 <button> 元素时,键盘交互遵循与 button role 相同的模式:
| 键 | 描述 |
|---|---|
| Enter | 激活项目,触发其 click 事件。如果项目在表单内且 type 设置为 "submit",则提交表单。 |
| Space | 激活项目,触发其 click 事件。即使项目 type 为 "submit",也不会提交表单。 |
锚点
当 <ion-item> 渲染原生 <a> 元素时,键盘交互遵循与 link role 相同的模式:
| 键 | 描述 |
|---|---|
| Enter | 激活项目,导航到链接页面或将焦点移动到页面内目标。 |
属性
button
| 说明 | If true, a button tag will be rendered and the item will be tappable. |
| 属性 | button |
| 类型 | boolean |
| 默认值 | false |
color
| 说明 | The color to use from your application's color palette. Default options are: "primary", "secondary", "tertiary", "success", "warning", "danger", "light", "medium", and "dark". For more information on colors, see theming. |
| 属性 | color |
| 类型 | "danger" | "dark" | "light" | "medium" | "primary" | "secondary" | "success" | "tertiary" | "warning" | string | undefined |
| 默认值 | undefined |
detail
| 说明 | If true, a detail arrow will appear on the item. Defaults to false unless the mode is ios and an href or button property is present. |
| 属性 | detail |
| 类型 | boolean | undefined |
| 默认值 | undefined |
detailIcon
| 说明 | The icon to use when detail is set to true. |
| 属性 | detail-icon |
| 类型 | string |
| 默认值 | chevronForward |
disabled
| 说明 | If true, the user cannot interact with the item. |
| 属性 | disabled |
| 类型 | boolean |
| 默认值 | false |
download
| 说明 | This attribute instructs browsers to download a URL instead of navigating to it, so the user will be prompted to save it as a local file. If the attribute has a value, it is used as the pre-filled file name in the Save prompt (the user can still change the file name if they want). |
| 属性 | download |
| 类型 | string | undefined |
| 默认值 | undefined |
href
| 说明 | Contains a URL or a URL fragment that the hyperlink points to. If this property is set, an anchor tag will be rendered. |
| 属性 | href |
| 类型 | string | undefined |
| 默认值 | undefined |
lines
| 说明 | How the bottom border should be displayed on the item. |
| 属性 | lines |
| 类型 | "full" | "inset" | "none" | undefined |
| 默认值 | undefined |
mode
| 说明 | The mode determines which platform styles to use. 这是一个虚拟属性,在初始化时设置一次,之后更改其值不会更新组件。 |
| 属性 | mode |
| 类型 | "ios" | "md" |
| 默认值 | undefined |
rel
| 说明 | Specifies the relationship of the target object to the link object. The value is a space-separated list of link types. |
| 属性 | rel |
| 类型 | string | undefined |
| 默认值 | undefined |
routerAnimation
| 说明 | When using a router, it specifies the transition animation when navigating to another page using href. |
| 属性 | undefined |
| 类型 | ((baseEl: any, opts?: any) => Animation) | undefined |
| 默认值 | undefined |
routerDirection
| 说明 | When using a router, it specifies the transition direction when navigating to another page using href. |
| 属性 | router-direction |
| 类型 | "back" | "forward" | "root" |
| 默认值 | 'forward' |
target
| 说明 | Specifies where to display the linked URL. Only applies when an href is provided. Special keywords: "_blank", "_self", "_parent", "_top". |
| 属性 | target |
| 类型 | string | undefined |
| 默认值 | undefined |
type
| 说明 | The type of the button. Only used when an onclick or button property is present. |
| 属性 | type |
| 类型 | "button" | "reset" | "submit" |
| 默认值 | 'button' |
事件
该组件没有可用的事件。
方法
该组件没有可用的公共方法。
CSS Shadow Parts
| Name | 说明 |
|---|---|
container | The wrapper element that contains the default slot. |
detail-icon | The chevron icon for the item. Only applies when detail="true". |
inner | The inner wrapper element that arranges the item content. |
native | The native HTML button, anchor or div element that wraps all child elements. |
CSS 自定义属性
- iOS
- MD
| Name | 说明 |
|---|---|
--background | Background of the item |
--background-activated | Background of the item when pressed. Note: setting this will interfere with the Material Design ripple. |
--background-activated-opacity | Opacity of the item background when pressed |
--background-focused | Background of the item when focused with the tab key |
--background-focused-opacity | Opacity of the item background when focused with the tab key |
--background-hover | Background of the item on hover |
--background-hover-opacity | Opacity of the background of the item on hover |
--border-color | Color of the item border |
--border-radius | Radius of the item border |
--border-style | Style of the item border |
--border-width | Width of the item border |
--color | Color of the item |
--color-activated | Color of the item when pressed |
--color-focused | Color of the item when focused with the tab key |
--color-hover | Color of the item on hover |
--detail-icon-color | Color of the item detail icon |
--detail-icon-font-size | Font size of the item detail icon |
--detail-icon-opacity | Opacity of the item detail icon |
--inner-border-width | Width of the item inner border |
--inner-box-shadow | Box shadow of the item inner |
--inner-padding-bottom | Bottom padding of the item inner |
--inner-padding-end | Right padding if direction is left-to-right, and left padding if direction is right-to-left of the item inner |
--inner-padding-start | Left padding if direction is left-to-right, and right padding if direction is right-to-left of the item inner |
--inner-padding-top | Top padding of the item inner |
--min-height | Minimum height of the item |
--padding-bottom | Bottom padding of the item |
--padding-end | Right padding if direction is left-to-right, and left padding if direction is right-to-left of the item |
--padding-start | Left padding if direction is left-to-right, and right padding if direction is right-to-left of the item |
--padding-top | Top padding of the item |
--ripple-color | Color of the item ripple effect |
--transition | Transition of the item |
| Name | 说明 |
|---|---|
--background | Background of the item |
--background-activated | Background of the item when pressed. Note: setting this will interfere with the Material Design ripple. |
--background-activated-opacity | Opacity of the item background when pressed |
--background-focused | Background of the item when focused with the tab key |
--background-focused-opacity | Opacity of the item background when focused with the tab key |
--background-hover | Background of the item on hover |
--background-hover-opacity | Opacity of the background of the item on hover |
--border-color | Color of the item border |
--border-radius | Radius of the item border |
--border-style | Style of the item border |
--border-width | Width of the item border |
--color | Color of the item |
--color-activated | Color of the item when pressed |
--color-focused | Color of the item when focused with the tab key |
--color-hover | Color of the item on hover |
--detail-icon-color | Color of the item detail icon |
--detail-icon-font-size | Font size of the item detail icon |
--detail-icon-opacity | Opacity of the item detail icon |
--inner-border-width | Width of the item inner border |
--inner-box-shadow | Box shadow of the item inner |
--inner-padding-bottom | Bottom padding of the item inner |
--inner-padding-end | Right padding if direction is left-to-right, and left padding if direction is right-to-left of the item inner |
--inner-padding-start | Left padding if direction is left-to-right, and right padding if direction is right-to-left of the item inner |
--inner-padding-top | Top padding of the item inner |
--min-height | Minimum height of the item |
--padding-bottom | Bottom padding of the item |
--padding-end | Right padding if direction is left-to-right, and left padding if direction is right-to-left of the item |
--padding-start | Left padding if direction is left-to-right, and right padding if direction is right-to-left of the item |
--padding-top | Top padding of the item |
--ripple-color | Color of the item ripple effect |
--transition | Transition of the item |
插槽
| Name | 说明 |
|---|---|
| Content is placed between the named slots if provided without a slot. | |
end | Content is placed to the right of the item text in LTR, and to the left in RTL. |
start | Content is placed to the left of the item text in LTR, and to the right in RTL. |