Sha256: 5f2d8a9cb88a219f3f0250247847f78582d18bd1c7167ca0ec693b9ceb8ec84d

Contents?: true

Size: 1.61 KB

Versions: 9

Compression:

Stored size: 1.61 KB

Contents

# rubocop:disable Style/MethodName

module HyperRouter
  module ComponentMethods
    def Link(to, opts = {}, &children)
      opts[:to] = {}.tap do |hash|
        hash[:pathname] = to
        hash[:search] = opts.delete(:search) if opts[:search]
        hash[:hash] = opts.delete(:hash) if opts[:hash]
      end.to_n
      React::Router::DOM::Link(opts, &children)
    end

    def NavLink(to, opts = {}, &children)
      opts[:to] = to.to_n
      opts[:activeClassName] = opts.delete(:active_class).to_n if opts[:active_class]
      opts[:activeStyle] = opts.delete(:active_style).to_n if opts[:active_style]
      opts[:isActive] = opts.delete(:active).to_n if opts[:active]
      React::Router::DOM::NavLink(opts, &children)
    end

    def Redirect(to, opts = {})
      opts[:to] = to.to_n
      React::Router::Redirect(opts)
    end

    def format_params(e)
      {
        match:    HyperRouter::Match.new(`#{e}.match`),
        location: HyperRouter::Location.new(`#{e}.location`),
        history:  HyperRouter::History.new(`#{e}.history`)
      }
    end

    def Route(to, opts = {}, &block)
      opts[:path] = to.to_n

      if opts[:mounts]
        component = opts.delete(:mounts)

        opts[:component] = lambda do |e|
          route_params = format_params(e)

          React.create_element(component, route_params).to_n
        end
      end

      if block
        opts[:render] = lambda do |e|
          route_params = format_params(e)

          yield(route_params.values).to_n
        end
      end

      React::Router::Route(opts)
    end

    def Switch(&children)
      React::Router::Switch(&children)
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
hyper-router-4.2.6.lap27 lib/hyper-router/component_methods.rb
hyper-router-4.2.6.lap26 lib/hyper-router/component_methods.rb
hyper-router-4.2.6.lap25 lib/hyper-router/component_methods.rb
hyper-router-4.2.6.lap24 lib/hyper-router/component_methods.rb
hyper-router-4.2.6.lap23 lib/hyper-router/component_methods.rb
hyper-router-4.2.6.lap22 lib/hyper-router/component_methods.rb
hyper-router-4.1.1 lib/hyper-router/component_methods.rb
hyper-router-4.0.1 lib/hyper-router/component_methods.rb
hyper-router-4.0.0 lib/hyper-router/component_methods.rb