Class: HakumiComponents::Tree::Component

Inherits:
BaseComponent
  • Object
show all
Extended by:
T::Sig
Includes:
HakumiComponents::Tree::Concerns::NodeRendering
Defined in:
app/components/hakumi_components/tree/component.rb

Defined Under Namespace

Classes: NodeState

Constant Summary collapse

DEFAULT_INDENT =
T.let(24, Integer)
RawNode =
T.type_alias { HakumiComponents::Tree::Node::RawInput }
NodeInput =
T.type_alias { T.any(HakumiComponents::Tree::Node, RawNode) }
NodeKey =
T.type_alias { Types::ValidationComparable }
NodeKeyList =
T.type_alias { T::Array[NodeKey] }
IconInput =
T.type_alias { BaseComponent::SymbolInput }
HeightInput =
T.type_alias { T.nilable(BaseComponent::DimensionInput) }
ControllerLocalValue =
T.type_alias { T.nilable(T.any(Types::HtmlPrimitive, T::Array[HakumiComponents::Tree::Node], NodeKeyList)) }

Constants included from HakumiComponents::Tree::Concerns::NodeRendering

HakumiComponents::Tree::Concerns::NodeRendering::AriaAttributes, HakumiComponents::Tree::Concerns::NodeRendering::NodeState

Constants inherited from BaseComponent

BaseComponent::ControllerOptions, BaseComponent::DateInput, BaseComponent::DateLikeValue, BaseComponent::DimensionInput, BaseComponent::HtmlPayloadInput, BaseComponent::I18nOptionValue, BaseComponent::PresenceArray, BaseComponent::PresenceScalar, BaseComponent::PresenceValue, BaseComponent::RawHtmlInput, BaseComponent::SIZES, BaseComponent::SizeValue, BaseComponent::SymbolInput

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HakumiComponents::Tree::Concerns::NodeRendering

#render_indent, #render_node_template, #render_nodes

Methods inherited from BaseComponent

#append_data_token, boolean_html_param, #build_inline_style, cast_boolean, #cast_boolean, #class_names, #component_classes, #data_attributes_from, #dimension_to_css, #ensure_dom_id!, float_html_param, #generate_id, #html_classes, html_param, html_primitive_param, #html_style, #i18n_scope, integer_html_param, #merge_attributes, #render_value, #size_to_pixels, #stimulus_attrs, string_html_param, string_or_symbol_array_html_param, symbol_html_param, #t_default, #translate_with_default, #validate_inclusion!, #validate_required!, #value_present?

Constructor Details

#initialize(nodes:, checkable: false, selectable: true, disabled: false, default_expand_keys: [], default_selected_keys: [], default_checked_keys: [], expanded_keys: nil, selected_keys: nil, checked_keys: nil, show_line: false, switcher_icon: nil, icon: nil, directory: false, multiple: false, draggable: false, block_node: false, searchable: false, search_placeholder: nil, height: nil, check_strictly: false, indent: DEFAULT_INDENT, **html_options) ⇒ Component

Returns a new instance of Component.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/components/hakumi_components/tree/component.rb', line 84

def initialize(
  nodes:,
  checkable: false,
  selectable: true,
  disabled: false,
  default_expand_keys: [],
  default_selected_keys: [],
  default_checked_keys: [],
  expanded_keys: nil,
  selected_keys: nil,
  checked_keys: nil,
  show_line: false,
  switcher_icon: nil,
  icon: nil,
  directory: false,
  multiple: false,
  draggable: false,
  block_node: false,
  searchable: false,
  search_placeholder: nil,
  height: nil,
  check_strictly: false,
  indent: DEFAULT_INDENT,
  **html_options
)
  @checkable = T.let(checkable, T::Boolean)
  @selectable = T.let(selectable, T::Boolean)
  @disabled = T.let(disabled, T::Boolean)
  @default_expand_keys = T.let(default_expand_keys.map(&:to_s), T::Array[String])
  @default_selected_keys = T.let(default_selected_keys.map(&:to_s), T::Array[String])
  @default_checked_keys = T.let(default_checked_keys.map(&:to_s), T::Array[String])
  @expanded_keys = T.let(expanded_keys&.map(&:to_s), T.nilable(T::Array[String]))
  @selected_keys = T.let(selected_keys&.map(&:to_s), T.nilable(T::Array[String]))
  @checked_keys = T.let(checked_keys&.map(&:to_s), T.nilable(T::Array[String]))
  @show_line = T.let(show_line, T::Boolean)
  @switcher_icon = T.let(switcher_icon, IconInput)
  @icon = T.let(icon, IconInput)
  @directory = T.let(directory, T::Boolean)
  @multiple = T.let(multiple, T::Boolean)
  @draggable = T.let(draggable, T::Boolean)
  @block_node = T.let(block_node, T::Boolean)
  @searchable = T.let(searchable, T::Boolean)
  @search_placeholder = T.let(search_placeholder || t_default(:search_placeholder, default: "Search"), String)
  @height = T.let(height, HeightInput)
  @check_strictly = T.let(check_strictly, T::Boolean)
  @indent = T.let(indent, Integer)
  @html_options = T.let(html_options, Types::HtmlAttributes)
  @normalized_nodes = T.let(
    HakumiComponents::Tree::Node.coerce_all(
      nodes,
      default_disabled: @disabled,
      default_icon: @icon,
      default_switcher_icon: @switcher_icon
    ),
    T::Array[HakumiComponents::Tree::Node]
  )

  validate_props!
end

Instance Attribute Details

#normalized_nodesObject (readonly)

Returns the value of attribute normalized_nodes.



145
146
147
# File 'app/components/hakumi_components/tree/component.rb', line 145

def normalized_nodes
  @normalized_nodes
end

Class Method Details

.extract_controller_locals(params) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/components/hakumi_components/tree/component.rb', line 30

def self.extract_controller_locals(params)
  raw_nodes = parse_raw_nodes(html_param(params, :nodes) || html_param(params, :tree_data))

  {
    nodes: HakumiComponents::Tree::Node.coerce_all(raw_nodes),
    checkable: boolean_html_param(html_param(params, :checkable)) == true,
    selectable: boolean_html_param(html_param(params, :selectable)) != false,
    disabled: boolean_html_param(html_param(params, :disabled)) == true,
    default_expand_keys: parse_json_array(html_param(params, :default_expand_keys)),
    default_selected_keys: parse_json_array(html_param(params, :default_selected_keys)),
    default_checked_keys: parse_json_array(html_param(params, :default_checked_keys)),
    expanded_keys: parse_json_array(html_param(params, :expanded_keys), allow_nil: true),
    selected_keys: parse_json_array(html_param(params, :selected_keys), allow_nil: true),
    checked_keys: parse_json_array(html_param(params, :checked_keys), allow_nil: true),
    show_line: boolean_html_param(html_param(params, :show_line)) == true,
    switcher_icon: symbol_html_param(html_param(params, :switcher_icon)),
    icon: symbol_html_param(html_param(params, :icon)),
    directory: boolean_html_param(html_param(params, :directory)) == true,
    multiple: boolean_html_param(html_param(params, :multiple)) == true,
    draggable: boolean_html_param(html_param(params, :draggable)) == true,
    block_node: boolean_html_param(html_param(params, :block_node)) == true,
    searchable: boolean_html_param(html_param(params, :searchable)) == true,
    height: html_primitive_param(html_param(params, :height)),
    check_strictly: boolean_html_param(html_param(params, :check_strictly)) == true
  }
end

.parse_json_array(value, allow_nil: false) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'app/components/hakumi_components/tree/component.rb', line 296

def self.parse_json_array(value, allow_nil: false)
  return nil if allow_nil && value.nil?
  return [] if value.nil?

  array = if value.is_a?(Array)
    value
  elsif value.is_a?(String)
    parsed = JSON.parse(value)
    parsed.is_a?(Array) ? parsed : []
  else
    []
  end

  array.filter_map do |entry|
    case entry
    when String, Symbol, Numeric
      entry
    else
      nil
    end
  end
rescue JSON::ParserError
  []
end

.parse_raw_nodes(value) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'app/components/hakumi_components/tree/component.rb', line 268

def self.parse_raw_nodes(value)
  return [] if value.nil?

  array = if value.is_a?(Array)
    value
  elsif value.is_a?(String)
    parsed = JSON.parse(value)
    parsed.is_a?(Array) ? parsed : []
  else
    []
  end

  array.filter_map do |entry|
    next unless entry.is_a?(Hash)
    typed_entry = T.let({}, RawNode)
    entry.each do |key, item|
      next unless key.is_a?(String) || key.is_a?(Symbol)
      next unless item.nil? || item.is_a?(String) || item.is_a?(Symbol) || item.is_a?(Numeric) || item == true || item == false || item.is_a?(Array) || item.is_a?(Hash)

      typed_entry[key] = item
    end
    typed_entry
  end
rescue JSON::ParserError
  []
end

Instance Method Details

#list_attributesObject



159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/components/hakumi_components/tree/component.rb', line 159

def list_attributes
  attrs = T.let(
    {
      class: list_classes,
      role: "tree",
      data: { hakumi__tree_target: "list" }
    },
    Types::HtmlAttributes
  )
  attrs[:style] = "max-height: #{@height}px;" if @height
  attrs
end

#search_placeholderObject



178
179
180
# File 'app/components/hakumi_components/tree/component.rb', line 178

def search_placeholder
  @search_placeholder
end

#searchable?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'app/components/hakumi_components/tree/component.rb', line 173

def searchable?
  @searchable
end

#wrapper_attributesObject



148
149
150
151
152
153
154
155
156
# File 'app/components/hakumi_components/tree/component.rb', line 148

def wrapper_attributes
  merge_attributes(
    {
      class: wrapper_classes,
      data: data_attributes
    },
    @html_options
  )
end