Sha256: 7fa7ab032ba8962cd4d552cdf2c79e5d2b90cbdf3b3e144f4652c48da4b87b36

Contents?: true

Size: 1.28 KB

Versions: 62

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

require "webpacker/react/railtie" if defined?(Rails)
require "webpacker/react/helpers"
require "webpacker/react/component"

module Playbook
  module PbKitHelper

    def pb_rails(kit, props: {}, &block)
      previous = prefix_partial_path_with_controller_namespace
      self.prefix_partial_path_with_controller_namespace = false
      kit = build_view_model(kit.to_s, rails_props(props), &block)
      render(partial: kit, as: :object)
    ensure
      self.prefix_partial_path_with_controller_namespace = previous
    end

    def pb_react(kit, props:{dark: react_props}, options: {})
      ::Webpacker::React::Component.new(kit.camelize).render(props, options)
    end

  private
    def rails_props(props)
      if @playbook.nil?
        props
      else
        dark_mode_props(props)
      end
    end  

    def react_props
      if @playbook.nil?
        false
      else
        dark_mode
      end
    end 

    def is_subkit?(kit)
      kit.match(%r{[/\\]})
    end

    def build_view_model(kit, props, &block)
      folder = is_subkit?(kit) ? kit.split("/")[0] : kit
      item = is_subkit?(kit) ? kit.split("/")[-1] : kit
      class_name = "Playbook::Pb#{folder.camelize}::#{item.camelize}"
      class_name.safe_constantize.new(props, &block)
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
playbook_ui-7.10.0 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-7.9.0 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-7.8.4 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-7.8.3 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-7.8.2 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-7.8.1 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-7.8.0 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-7.7.0.pre.alpha1 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-7.7.0 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-7.6.2.pre.alpha1 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-7.6.2 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-7.6.1.pre.alpha1 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-7.6.1 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-7.6.0 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-7.5.1 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-7.5.0 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-7.4.2 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-7.4.1 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-7.4.0.pre.alpha6 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-7.4.0.pre.alpha5 app/helpers/playbook/pb_kit_helper.rb