Sha256: eaf9d4a6b209fe9ecc697f4e8d53e07182a4fbebf2f560d21ac5f563eb088b4a

Contents?: true

Size: 1.42 KB

Versions: 16

Compression:

Stored size: 1.42 KB

Contents

require "react_on_rails/utils"

module ReactOnRails
  module ReactComponent
    class Options
      include Utils::Required

      NO_PROPS = {}.freeze
      HIDDEN = "display:none".freeze

      def initialize(name: required("name"), options: required("options"))
        @name = name
        @options = options
      end

      def props
        options.fetch(:props) { NO_PROPS }
      end

      def name
        @name.camelize
      end

      def dom_id
        @dom_id ||= options.fetch(:id) { generate_unique_dom_id }
      end

      def html_options
        options[:html_options].to_h
      end

      def prerender
        retrieve_key(:prerender)
      end

      def trace
        retrieve_key(:trace)
      end

      def replay_console
        retrieve_key(:replay_console)
      end

      def raise_on_prerender_error
        retrieve_key(:raise_on_prerender_error)
      end

      def data
        {
          component_name: name,
          props: props,
          trace: trace,
          dom_id: dom_id
        }
      end

      def style
        return nil if ReactOnRails.configuration.skip_display_none
        HIDDEN
      end

      private

      attr_reader :options

      def generate_unique_dom_id
        "#{@name}-react-component-#{SecureRandom.uuid}"
      end

      def retrieve_key(key)
        options.fetch(key) do
          ReactOnRails.configuration.public_send(key)
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
react_on_rails-6.8.2 lib/react_on_rails/react_component/options.rb
react_on_rails-6.8.1 lib/react_on_rails/react_component/options.rb
react_on_rails-6.8.0 lib/react_on_rails/react_component/options.rb
react_on_rails-6.7.2 lib/react_on_rails/react_component/options.rb
react_on_rails-6.7.1 lib/react_on_rails/react_component/options.rb
react_on_rails-6.6.0 lib/react_on_rails/react_component/options.rb
react_on_rails-6.6.0.alpha.1 lib/react_on_rails/react_component/options.rb
react_on_rails-6.5.1 lib/react_on_rails/react_component/options.rb
react_on_rails-6.5.0 lib/react_on_rails/react_component/options.rb
react_on_rails-6.4.2 lib/react_on_rails/react_component/options.rb
react_on_rails-6.5.0.beta.1 lib/react_on_rails/react_component/options.rb
react_on_rails-6.4.1 lib/react_on_rails/react_component/options.rb
react_on_rails-6.4.0 lib/react_on_rails/react_component/options.rb
react_on_rails-6.3.5 lib/react_on_rails/react_component/options.rb
react_on_rails-6.3.4 lib/react_on_rails/react_component/options.rb
react_on_rails-6.3.3 lib/react_on_rails/react_component/options.rb