Module: HyperRouter::ComponentMethods

Defined in:
lib/hyper-router/component_methods.rb

Instance Method Summary collapse

Instance Method Details

#format_params(e) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/hyper-router/component_methods.rb', line 27

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


5
6
7
8
9
10
11
12
# File 'lib/hyper-router/component_methods.rb', line 5

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


14
15
16
17
18
19
20
# File 'lib/hyper-router/component_methods.rb', line 14

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

#Redirect(to, opts = {}) ⇒ Object



22
23
24
25
# File 'lib/hyper-router/component_methods.rb', line 22

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

#Route(to, opts = {}, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/hyper-router/component_methods.rb', line 35

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

#Switch(&children) ⇒ Object



59
60
61
# File 'lib/hyper-router/component_methods.rb', line 59

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