蓝色天空网mbsky.com · 知识 · 实践 · 创造Ionic · 本地阅读预览
教程 / 移动开发 / Ionic

Ionic 单选框

解析 Ionic 1 单选框(item-radio)的标准结构、相同 name 属性的互斥分组机制、radio-icon 选中勾选图标显示原理、label 整行触控体验以及现代 ion-radio-group 对照。

本站独立整理的内容草稿。实例显示的是预期结果;浏览器、服务端运行与技术内容验收尚待完成,本页只用于阅读和图文核对。

学完这一章,你能够

Ionic 单选框结构、name 互斥组与触控设计

Ionic 1 单选框(Radio)基于标准 `<input type="radio">` 打造出贴合移动应用习惯的勾选列表样式。其核心结构为:`<div class="list">` 内每个候选项均使用 `<label class="item item-radio">` 包裹(利用 `<label>` 原生关联机制让整行任意位置都可轻松点击),内部放置共享同一 `name` 属性(如 `name="group"`)的单选框以保证组内互斥,并通过 `checked="checked"` 设定默认选中项(示例涵盖 Go、Python、Ruby、.Net、Java、PHP 六个选项);紧随其后的 `<div class="radio-content">` 负责呈现 `<div class="item-content">` 文本与右侧 `<i class="radio-icon ion-checkmark"></i>` 选中标记(仅当对应 radio 处于选中态时由 CSS 显示该对勾图标)。源页面的内嵌演示 `iframe` 仅做静态登记;现代 Ionic 7/8 则通过 `<ion-radio-group value="go">` 与 `<ion-radio value="...">` 声明式管理单选状态。

运行 Ionic 1 单选框分组 DOM、互斥选中状态机与 label 热区离线核验

python · 阅读源码
"""Offline Ionic 1 Radio group DOM, shared name grouping & exclusive selection verifier for Ch11 (radio)."""
from __future__ import annotations

import json
from lxml import html

VERSION = "CPython 3.12.14 / lxml offline Ionic UI & component verifier"

RADIO_HTML = """
<div class="list">
  <label class="item item-radio">
    <input type="radio" name="group" value="go" checked="checked">
    <div class="radio-content">
      <div class="item-content">Go</div>
      <i class="radio-icon ion-checkmark"></i>
    </div>
  </label>
  <label class="item item-radio">
    <input type="radio" name="group" value="python">
    <div class="radio-content">
      <div class="item-content">Python</div>
      <i class="radio-icon ion-checkmark"></i>
    </div>
  </label>
  <label class="item item-radio">
    <input type="radio" name="group" value="ruby">
    <div class="radio-content">
      <div class="item-content">Ruby</div>
      <i class="radio-icon ion-checkmark"></i>
    </div>
  </label>
  <label class="item item-radio">
    <input type="radio" name="group" value=".net">
    <div class="radio-content">
      <div class="item-content">.Net</div>
      <i class="radio-icon ion-checkmark"></i>
    </div>
  </label>
  <label class="item item-radio">
    <input type="radio" name="group" value="java">
    <div class="radio-content">
      <div class="item-content">Java</div>
      <i class="radio-icon ion-checkmark"></i>
    </div>
  </label>
  <label class="item item-radio">
    <input type="radio" name="group" value="php">
    <div class="radio-content">
      <div class="item-content">PHP</div>
      <i class="radio-icon ion-checkmark"></i>
    </div>
  </label>
</div>
"""


def make_case(name: str, summary: str, expected, actual) -> dict:
    return {
        "name": name,
        "summary": summary,
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_radio_group_dom() -> dict:
    root = html.fragment_fromstring(RADIO_HTML)
    labels = root.xpath("./label[contains(@class,'item-radio')]")
    names = {lbl.xpath("./input/@name")[0] for lbl in labels}
    values = [lbl.xpath("./input/@value")[0] for lbl in labels]
    texts = [lbl.xpath("./div[@class='radio-content']/div[@class='item-content']/text()")[0].strip() for lbl in labels]
    icons = {lbl.xpath(".//i/@class")[0] for lbl in labels}
    initial_checked = [lbl.xpath("./input/@value")[0] for lbl in labels if lbl.xpath("./input/@checked")]
    actual = {
        "option_count": len(labels),
        "shared_name_set": sorted(names),
        "values": values,
        "labels": texts,
        "icon_classes": sorted(icons),
        "initial_checked": initial_checked,
    }
    expected = {
        "option_count": 6,
        "shared_name_set": ["group"],
        "values": ["go", "python", "ruby", ".net", "java", "php"],
        "labels": ["Go", "Python", "Ruby", ".Net", "Java", "PHP"],
        "icon_classes": ["radio-icon ion-checkmark"],
        "initial_checked": ["go"],
    }
    return make_case(
        "radio_group_markup_and_shared_name",
        "Verify 6 label.item.item-radio rows share name='group', .radio-content/.item-content, and .radio-icon",
        expected,
        actual,
    )


def case_radio_mutual_exclusion_machine() -> dict:
    root = html.fragment_fromstring(RADIO_HTML)
    values = [lbl.xpath("./input/@value")[0] for lbl in root.xpath("./label")]
    selected = "go"

    def select(val: str) -> dict:
        nonlocal selected
        assert val in values
        selected = val
        return {v: (v == selected) for v in values}

    state_after_python = select("python")
    state_after_java = select("java")
    actual = {
        "checked_count_after_python": sum(state_after_python.values()),
        "python_visible_checkmark": state_after_python["python"] and not state_after_python["go"],
        "final_selected": selected,
        "checked_count_after_java": sum(state_after_java.values()),
    }
    expected = {
        "checked_count_after_python": 1,
        "python_visible_checkmark": True,
        "final_selected": "java",
        "checked_count_after_java": 1,
    }
    return make_case(
        "radio_mutual_exclusion_and_icon_visibility",
        "Verify mutual exclusion invariant (exactly one checked option and visible checkmark) on selection changes",
        expected,
        actual,
    )


def case_radio_label_ergonomics_and_modern_mapping() -> dict:
    root = html.fragment_fromstring(RADIO_HTML)
    all_wrapped_in_label = all(el.tag == "label" for el in root.xpath("./*"))
    actual = {
        "all_options_use_label_wrapper": all_wrapped_in_label,
        "modern_component": "<ion-radio-group value=\"go\"><ion-radio value=\"go\">Go</ion-radio></ion-radio-group>",
    }
    expected = {
        "all_options_use_label_wrapper": True,
        "modern_component": "<ion-radio-group value=\"go\"><ion-radio value=\"go\">Go</ion-radio></ion-radio-group>",
    }
    return make_case(
        "radio_label_touch_target_and_modern_mapping",
        "Verify <label> full-row touch target wrapper and Ionic 7/8 <ion-radio-group> equivalence",
        expected,
        actual,
    )


def main() -> None:
    cases = [
        case_radio_group_dom(),
        case_radio_mutual_exclusion_machine(),
        case_radio_label_ergonomics_and_modern_mapping(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
预期结果 · 待实际运行核验
  • 验证 6 个选项共享 `name="group"`、默认选中 `go`、切换选项时严格满足唯一选中与 `.radio-icon` 联动,以及 `<label>` 整行包裹与现代 `<ion-radio-group>` 映射。
运行 Ionic 1 单选框分组 DOM、互斥选中状态机与 label 热区离线核验

所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

当前源码 SHA256:b5c8b65fa42eed821aed11025e9e4fc4632859f28fcd55662d353f036a688927

下载本站示例源码

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载Ionic 单选框实验

所需环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

ZIP SHA256:855c3b613ae7c2894d05caf7a0e755fcaf5cefb4f3ca0a359af57a30d856e4b3

常见错误与边界

  • 实际端口、文档资源与应用配置都应和测试条件一致。

练习与解答

如果同一列表里多个 `item-radio` 的 `<input type="radio">` 使用了不同的 `name` 属性,会产生什么问题?

展开参考解答

浏览器仅对相同 `name` 的单选框执行互斥约束;若 `name` 不同,用户将可以同时勾选多个选项,破坏单选语义。

规范与官方依据