class NfgUi::Bootstrap::Components::Base
Base
Component Defines conventional, shared behavior across Bootstrap
components
Attributes
Public Class Methods
# File lib/nfg_ui/bootstrap/components/base.rb, line 20 def initialize(component_options, view_context) self.options = defaults.merge!(component_options) self.view_context = view_context @body = options.fetch(:body, '') utility_initialize component_initialize end
Public Instance Methods
This is used to help identify where to find partials for rendering components.
Set the component family, e.g.: :breadcrumb on any sibling components.
For example: BreadcrumbItem
& Breadcrumb
are members of the :breadcrumb component_family
# File lib/nfg_ui/bootstrap/components/base.rb, line 48 def component_family nil end
# File lib/nfg_ui/bootstrap/components/base.rb, line 52 def data options[:data] || {} end
Use view_context
url_for method to ensure that when objects are passed into href, e.g. `href: @admin` will convert it to the correct path
Likewise that hashes are also parsed correctly example: href: { controller: 'admin', action: 'show', id: 7 }
# File lib/nfg_ui/bootstrap/components/base.rb, line 76 def href options_href = options[:href] return if options_href.blank? view_context.url_for(options_href) end
# File lib/nfg_ui/bootstrap/components/base.rb, line 56 def html_options options.except(*non_html_attribute_options.uniq) .merge!(id: id, class: css_classes, data: data, href: href, style: style, **assistive_html_attributes) .reject { |_k, v| v.blank? } # prevent empty attributes from showing up # Example: <div class>Text</div> end
# File lib/nfg_ui/bootstrap/components/base.rb, line 83 def id options[:id] end
This base render handles many of the components and can be changed to have a different base element by overriding the base_element. in some cases, the child component can also call super with a block to have this render as the wrapping element.
# File lib/nfg_ui/bootstrap/components/base.rb, line 35 def render content_tag(base_element, html_options) do (block_given? ? yield : body) end end
# File lib/nfg_ui/bootstrap/components/base.rb, line 87 def style options[:style] end
Private Instance Methods
Assigned on individual components as needed Ex: { role: 'alert' }
If aria assistive html is needed, see: Bootstrap::Utilities::AriaAssistable avoid passing aria to assistive_html_attributes
directly
# File lib/nfg_ui/bootstrap/components/base.rb, line 99 def assistive_html_attributes @assistive_html_attributes ||= {} end
the base_element
is used in the default render for all components as the outer wrapping element. Typically, this is a div, but can be overriddent as a different static element in a child class or as a dynamic element in the child class. this allows most child components to not have to have their own render statement if their wrapping element is not a div
# File lib/nfg_ui/bootstrap/components/base.rb, line 116 def base_element :div end
# File lib/nfg_ui/bootstrap/components/base.rb, line 128 def component_class_name_string self.class.name.demodulize.to_s end
Fallback component css class name. Overwritten within individual classes for situations like Button's css class is 'btn'… Example: returns 'alert' from NfgUi::Bootstrap::Components::Alert
# File lib/nfg_ui/bootstrap/components/base.rb, line 124 def component_css_class @component_css_class ||= component_class_name_string.underscore.dasherize.downcase end
For components that inherit bootstrap, provide a second layer of initialization, for example: to initialize traits on design system components (which are not available on bootstrap)
# File lib/nfg_ui/bootstrap/components/base.rb, line 107 def component_initialize; end
Manage or adjust the css_classes
of the component by adding a new string of css classes to this method ex: super.push('new-class')
# File lib/nfg_ui/bootstrap/components/base.rb, line 135 def css_classes @css_classes ||= [component_css_class, options[:class]].reject(&:nil?).uniq.join(' ').squish end
# File lib/nfg_ui/bootstrap/components/base.rb, line 139 def defaults { # HTML Defaults class: '', id: nil, # Content # heading: (nil if heading.present?), body: nil, data: {}, # Configuration # traits: ([] if traits.present?) } end
Remove attributes from html_options
that shouldn't show up in the html element, ex: <div body='should not be here'>
# File lib/nfg_ui/bootstrap/components/base.rb, line 156 def non_html_attribute_options @non_html_attribute_options ||= %i[body heading traits] end
# File lib/nfg_ui/bootstrap/components/base.rb, line 108 def utility_initialize; end