Sha256: b5401d0c1c22fc0d2f5b5d22394a980bf01c0c6e216a17a02332870d75bbc30e

Contents?: true

Size: 1.16 KB

Versions: 4

Compression:

Stored size: 1.16 KB

Contents

require "formalist/element"

module Formalist
  class Elements
    class Group < Element
      attribute :label

      def fill(input: {}, errors: {})
        children = self.children.map { |child|
          child.fill(input: input, errors: errors)
        }

        super(input: input, errors: errors, children: children)
      end

      # Converts the group into an abstract syntax tree.
      #
      # It takes the following format:
      #
      # ```
      # [:group, [params]]
      # ```
      #
      # With the following parameters:
      #
      # 1. Custom form element type (or `:group` otherwise)
      # 2. Form element attributes
      # 3. Child form elements
      #
      # @see Formalist::Element::Attributes#to_ast "Form element attributes" structure
      #
      # @example
      #   group.to_ast
      #   # => [:group, [
      #     :group,
      #     [:object, []],
      #     [...child elements...]
      #   ]]
      #
      # @return [Array] the group as an abstract syntax tree.
      def to_ast
        [:group, [
          type,
          Element::Attributes.new(attributes).to_ast,
          children.map(&:to_ast),
        ]]
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
formalist-0.9.0 lib/formalist/elements/group.rb
formalist-0.8.0 lib/formalist/elements/group.rb
formalist-0.7.0 lib/formalist/elements/group.rb
formalist-0.6.0 lib/formalist/elements/group.rb