JGuang

表单布局

FormLayout · Astryx

交互预览
npm install @astryxdesign/core@0.7.1 @astryxdesign/theme-neutral@0.7.1 @stylexjs/stylex@0.19.0

表单布局 · FormLayout

表单布局用于布局场景。下面的官方示例展示原生用法与状态,属性名保持上游 API。

安装与前置条件

npm install @astryxdesign/core@0.7.1 @astryxdesign/theme-neutral@0.7.1 @stylexjs/stylex@0.19.0

需要 React 19+,按顺序导入基础 CSS 和主题,并设置 Theme Provider。参见开始使用。

示例与使用方式

  • 表单布局 · 综合展示 1:首先体验这个示例,再查看代码理解组件的组合方式。
  • 表单布局 · 交互示例 2:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 表单布局 · 交互示例 3:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 表单布局 · 交互示例 4:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 表单布局 · 交互示例 5:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。

API 与上游详细参考

优先使用 0.7.1 官方中文说明;上游尚未提供中文的条目保留英文。属性名、类型及默认值保持原生 API,不把示例状态名称当成组件属性。

Form Layout

A layout container that arranges form fields with consistent spacing and direction. FormLayout handles where fields go, not state or submission. Wrap it in a <form> for that. Supports vertical (default), horizontal, and horizontal-labels directions, and can be nested to mix them.

属性类型默认值说明
direction'vertical' | 'horizontal' | 'horizontal-labels''vertical'控制字段排列方式。vertical 从上到下堆叠,horizontal 从左到右排列且等比弹性增长,horizontal-labels 使用 CSS Grid 将标签放在输入框左侧(在窄视口 <=480px 时折叠为垂直布局)。
defaultOptionality'optional' | 'required'表单视为默认的状态,因此仅例外字段显示可选/必填指示器。设为 "optional" 时,仅标记 isRequired 的字段显示指示器;设为 "required" 时,仅标记 isOptional 的字段显示。与默认一致的字段不显示任何内容。设为 "required" 时,未标记的字段仍会暴露 aria-required,使屏幕阅读器与视觉默认一致——仅作用于 aria-required,不改变原生 required 属性。不设置则保持当前逐字段行为。
childrenReactNode要排列的表单字段。接受 Astryx 输入组件(TextInput、Selector 等)和 Field 包装的自定义控件。
xstyleStyleXStyles用于布局自定义(外边距、定位、尺寸)的 StyleX 样式。必须是 stylex.create() 的值,而非内联样式对象如 style={{}}。

使用建议

  • Stack fields vertically for most forms. It's the easiest to scan top to bottom.
  • Nest a horizontal FormLayout inside a vertical one when fields naturally pair up, like First Name + Last Name or City + State + ZIP.
  • Use horizontal-labels for settings pages where labels sit beside their inputs.
  • 避免:Use FormLayout for form state or submission. It's just layout. Wrap it in a <form> for that.
  • 避免:Put unrelated fields side by side in a horizontal layout. Save it for fields that belong together.
  • 避免:Nest horizontal-labels inside another FormLayout. It uses CSS Grid and needs to be the outermost container.

主题 API

{
  "targets": [
    {
      "className": "astryx-form-layout",
      "visualProps": [
        "direction"
      ]
    }
  ]
}

组件结构

  • Form title: Heading that describes the purpose of the form.
  • Fields: Input components with labels for collecting user data.
  • Footer: Contains confirmation buttons such as Submit or Cancel.