Help us learn about your current experience with the documentation. Take the survey.

HAML

HAML 是 GitLab 使用的 Ruby on Rails 模板语言。

HAML 与我们的 Pajamas 设计系统

GitLab UI 是一个遵循 Pajamas 设计系统 的 Vue 组件库。其中许多组件依赖于 JavaScript,因此只能在 Vue 中使用。

但是,一些更简单的组件(例如按钮、复选框或表单输入)可以在 HAML 中使用:

  • 部分 Pajamas 组件可作为 ViewComponent 使用。请尽可能使用它们。
  • 如果没有 ViewComponent,为什么不直接创建一个呢?如果需要帮助,请联系 设计系统 团队。
  • 作为备选方案,可以通过为元素应用正确的 CSS 类来实现。
  • 存在一个自定义的 Ruby on Rails 表单构建器,用于帮助在 HAML 表单中使用 GitLab UI 组件。

使用 GitLab UI 表单构建器

要使用 GitLab UI 表单构建器:

  1. form_for 更改为 gitlab_ui_form_for
  2. f.check_box 更改为 f.gitlab_ui_checkbox_component
  3. 移除 f.label,并将标签作为第二个参数传递给 f.gitlab_ui_checkbox_component

例如:

  • 修改前:

    = form_for @group do |f|
      .form-group.gl-mb-3
        .gl-form-checkbox.custom-control.custom-checkbox
          = f.check_box :prevent_sharing_groups_outside_hierarchy, disabled: !can_change_prevent_sharing_groups_outside_hierarchy?(@group), class: 'custom-control-input'
          = f.label :prevent_sharing_groups_outside_hierarchy, class: 'custom-control-label' do
            %span
              = safe_format(s_('GroupSettings|Prevent members from sending invitations to groups outside of %{group} and its subgroups.'), group: link_to_group(@group))
            %p.help-text= prevent_sharing_groups_outside_hierarchy_help_text(@group)
    
      .form-group.gl-mb-3
        .gl-form-checkbox.custom-control.custom-checkbox
          = f.check_box :lfs_enabled, checked: @group.lfs_enabled?, class: 'custom-control-input'
          = f.label :lfs_enabled, class: 'custom-control-label' do
            %span
              = _('Allow projects within this group to use Git LFS')
              = link_to sprite_icon('question-o'), help_page_path('topics/git/lfs/_index.md')
            %p.help-text= _('This setting can be overridden in each project.')
  • 修改后:

    = gitlab_ui_form_for @group do |f|
      .form-group.gl-mb-3
        = f.gitlab_ui_checkbox_component :prevent_sharing_groups_outside_hierarchy,
            safe_format(s_('GroupSettings|Prevent members from sending invitations to groups outside of %{group} and its subgroups.'), group: link_to_group(@group)),
            help_text: prevent_sharing_groups_outside_hierarchy_help_text(@group),
            checkbox_options: { disabled: !can_change_prevent_sharing_groups_outside_hierarchy?(@group) }
    
      .form-group.gl-mb-3
        = f.gitlab_ui_checkbox_component :lfs_enabled, checkbox_options: { checked: @group.lfs_enabled? } do |c|
          - c.with_label do
            = _('Allow projects within this group to use Git LFS')
            = link_to sprite_icon('question-o'), help_page_path('topics/git/lfs/_index.md')
          - c.with_help_text do
            = _('This setting can be overridden in each project.')

可用组件

使用 GitLab UI 表单构建器时,以下组件可在 HAML 中使用。

目前仅列出可用组件,但未来计划添加更多。

gitlab_ui_checkbox_component

GitLab UI 文档

参数
参数 类型 必需 (默认值) 描述
method Symbol true 传递给 gitlab_ui_form_for 的对象的属性。
label String false (nil) 复选框标签。如果需要在标签内使用 HTML,可以使用 label 插槽代替此参数。
help_text String false (nil) 显示在复选框下方的帮助文本。如果需要在帮助文本内使用 HTML,可以使用 help_text 插槽代替此参数。
checkbox_options Hash false ({}) 传递给 Rails check_box 方法 的选项。
checked_value String false ('1') 复选框被选中时的值。
unchecked_value String false ('0') 复选框未选中时的值。
label_options Hash false ({}) 传递给 Rails label 方法 的选项。
插槽

此组件支持 ViewComponent 插槽

插槽 描述
label 复选框标签内容。可以使用此插槽代替 label 参数。
help_text 显示在复选框下方的帮助文本内容。可以使用此插槽代替 help_text 参数。

gitlab_ui_radio_component

GitLab UI 文档

参数
参数 类型 必需 (默认值) 描述
method Symbol true 传递给 gitlab_ui_form_for 的对象的属性。
value Symbol true 单选按钮标签的值。
label String false (nil) 单选按钮标签。如果需要在标签内使用 HTML 内容,可以使用 label 插槽代替此参数。
help_text String false (nil) 显示在单选按钮下方的帮助文本。如果需要在帮助文本内使用 HTML 内容,可以使用 help_text 插槽代替此参数。
radio_options Hash false ({}) 传递给 Rails radio_button 方法 的选项。
label_options Hash false ({}) 传递给 Rails label 方法 的选项。
插槽

此组件支持 ViewComponent 插槽

插槽 描述
label 单选按钮标签内容。可以使用此插槽代替 label 参数。
help_text 显示在单选按钮下方的帮助文本内容。可以使用此插槽代替 help_text 参数。