Skip to content

Commit b94089a

Browse files
committed
i18n improved plus added dom key and set(:Var) method
1 parent 557d12f commit b94089a

File tree

24 files changed

+345
-81
lines changed

24 files changed

+345
-81
lines changed

ruby/examples/misc/stock-tickers/app/hyperstack/components/app.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def add_symbol
1515
BS::Row(style: { marginTop: 20 }) do
1616
BS::Col(sm: 4) do
1717
BS::InputGroup(class: 'mb-3') do
18-
BS::FormControl(ref: assign_to(:SymbolInput), placeholder: 'New Stock Market Symbol')
18+
BS::FormControl(dom: set(:SymbolInput), placeholder: 'New Stock Market Symbol')
1919
.on(:enter) { add_symbol }
2020
BS::InputGroup::Append() { BS::Button() { 'Add' } }
2121
.on(:click) { add_symbol }

ruby/hyper-component/lib/hyperstack/ext/component/element.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,5 @@ def self.[](selector)
4444
}
4545
Element.expose :mount_components
4646
end
47+
48+
DOM = Element

ruby/hyper-component/lib/hyperstack/internal/component/instance_methods.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ def refs
2727
Hash.new(`#{@__hyperstack_component_native}.refs`)
2828
end
2929

30-
def assign_to(sym)
31-
->(ref) { instance_variable_set("@#{sym}", ::Element[ref]) }
32-
end
33-
3430
def dom_node
3531
`ReactDOM.findDOMNode(#{self}.__hyperstack_component_native)` # react >= v0.15.0
3632
end

ruby/hyper-component/lib/hyperstack/internal/component/react_wrapper.rb

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,19 +244,48 @@ def self.convert_props(args)
244244
props["key"] = value.to_key
245245

246246
elsif key == 'ref'
247-
if value.respond_to?(:call)
248-
props[key] = %x{
249-
function(dom_node){
250-
if (dom_node !== null && dom_node.__opalInstance !== undefined && dom_node.__opalInstance !== null) {
251-
#{ value.call(`dom_node.__opalInstance`) };
252-
} else if(dom_node !== null && ReactDOM.findDOMNode !== undefined && dom_node.nodeType === undefined) {
253-
#{ value.call(`ReactDOM.findDOMNode(dom_node)`) };
254-
} else {
255-
#{ value.call(`dom_node`) };
256-
}
247+
unless value.respond_to?(:call)
248+
raise "The ref and dom params must be given a Proc.\n"\
249+
"If you want to capture the ref in an instance variable use the `set` method.\n"\
250+
"For example `ref: set(:TheRef)` will capture assign the ref to `@TheRef`\n"
251+
end
252+
props[key] = %x{
253+
function(dom_node){
254+
if (dom_node !== null && dom_node.__opalInstance !== undefined && dom_node.__opalInstance !== null) {
255+
#{ Hyperstack::Internal::State::Mapper.ignore_mutations { value.call(`dom_node.__opalInstance`) } };
256+
} else if(dom_node !== null && ReactDOM.findDOMNode !== undefined && dom_node.nodeType === undefined) {
257+
#{ Hyperstack::Internal::State::Mapper.ignore_mutations { value.call(`ReactDOM.findDOMNode(dom_node)`) } };
258+
} else if(dom_node !== null){
259+
#{ Hyperstack::Internal::State::Mapper.ignore_mutations { value.call(`dom_node`) } };
257260
}
258261
}
262+
}
263+
264+
elsif key == 'dom'
265+
unless value.respond_to?(:call)
266+
raise "The ref and dom params must be given a Proc.\n"\
267+
"If you want to capture the dom node in an instance variable use the `set` method.\n"\
268+
"For example `dom: set(:DomNode)` will assign the dom node to `@DomNode`\n"
269+
end
270+
unless Module.const_defined? 'Element'
271+
raise "You must include 'hyperstack/component/jquery' "\
272+
"in your manifest to use the `dom` reference key.\n"\
273+
'For example if using rails include '\
274+
"`config.import 'hyperstack/component/jquery', client_only: true`"\
275+
'in your config/initializer/hyperstack.rb file'
259276
end
277+
props[:ref] = %x{
278+
function(dom_node){
279+
if (dom_node !== null && dom_node.__opalInstance !== undefined && dom_node.__opalInstance !== null) {
280+
#{ Hyperstack::Internal::State::Mapper.ignore_mutations { value.call(::Element[`dom_node.__opalInstance`]) } };
281+
} else if(dom_node !== null && ReactDOM.findDOMNode !== undefined && dom_node.nodeType === undefined) {
282+
#{ Hyperstack::Internal::State::Mapper.ignore_mutations { value.call(::Element[`ReactDOM.findDOMNode(dom_node)`]) } };
283+
} else if(dom_node !== null) {
284+
#{ Hyperstack::Internal::State::Mapper.ignore_mutations { value.call(::Element[`dom_node`]) } };
285+
}
286+
}
287+
}
288+
260289
elsif Hyperstack::Component::ReactAPI::HASH_ATTRIBUTES.include?(key) && value.is_a?(Hash)
261290
value.each { |k, v| props["#{key}-#{k.gsub(/__|_/, '__' => '_', '_' => '-')}"] = v.to_n }
262291
else

ruby/hyper-component/spec/client_features/refs_callback_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,18 @@ def self.rec_cnt
9292
end
9393
expect_evaluate_ruby('Foo.rec_cnt').to eq(2)
9494
end
95+
96+
it 'can get the reference using the state set method' do
97+
mount 'Foo' do
98+
class Foo
99+
render do
100+
DIV(ref: set(:the_ref)) { "I am #{@the_ref}" }
101+
end
102+
after_mount do
103+
force_update!
104+
end
105+
end
106+
end
107+
expect(page).to have_content('I am [object HTMLDivElement]')
108+
end
95109
end

ruby/hyper-i18n/bin/console

Lines changed: 0 additions & 14 deletions
This file was deleted.

ruby/hyper-i18n/bin/setup

Lines changed: 0 additions & 8 deletions
This file was deleted.

ruby/hyper-i18n/hyper-i18n.gemspec

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
lib = File.expand_path('../lib', __FILE__)
44
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5-
require 'hyper-i18n/version'
5+
require 'hyperstack/i18n/version'
66

77
Gem::Specification.new do |spec|
88
spec.name = 'hyper-i18n'
9-
spec.version = HyperI18n::VERSION
9+
spec.version = Hyperstack::I18n::VERSION
1010
spec.authors = ['adamcreekroad']
1111
spec.email = ['[email protected]']
1212

@@ -19,13 +19,13 @@ Gem::Specification.new do |spec|
1919
spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
2020
spec.require_paths = ['lib']
2121

22-
spec.add_dependency 'hyper-operation', HyperI18n::VERSION
22+
spec.add_dependency 'hyper-operation', Hyperstack::I18n::VERSION
2323
spec.add_dependency 'i18n'
2424

2525
spec.add_development_dependency 'bundler', '~> 1.16.0'
2626
spec.add_development_dependency 'chromedriver-helper'
27-
spec.add_development_dependency 'hyper-model', HyperI18n::VERSION
28-
spec.add_development_dependency 'hyper-spec', HyperI18n::VERSION
27+
spec.add_development_dependency 'hyper-model', Hyperstack::I18n::VERSION
28+
spec.add_development_dependency 'hyper-spec', Hyperstack::I18n::VERSION
2929
spec.add_development_dependency 'opal-rails', '~> 0.9.4'
3030
spec.add_development_dependency 'pry'
3131
spec.add_development_dependency 'puma'

ruby/hyper-i18n/lib/hyper-i18n.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55
require 'hyper-operation'
66
require 'hyper-state'
77

8-
require 'hyper-i18n/helper_methods'
9-
require 'hyper-i18n/operations/localize'
10-
require 'hyper-i18n/operations/translate'
11-
require 'hyper-i18n/i18n'
12-
require 'hyper-i18n/version'
8+
require 'hyperstack/internal/i18n/helper_methods'
9+
require 'hyperstack/internal/i18n/localize'
10+
require 'hyperstack/internal/i18n/translate'
11+
require 'hyperstack/internal/i18n'
12+
require 'hyperstack/i18n/version'
1313

1414
if RUBY_ENGINE == 'opal'
15-
require 'hyper-i18n/active_model/name'
16-
require 'hyper-i18n/active_record/class_methods'
17-
require 'hyper-i18n/hyperstack/component'
18-
require 'hyper-i18n/stores/i18n_store'
15+
require 'hyperstack/ext/i18n/active_model/name'
16+
require 'hyperstack/ext/i18n/active_record/class_methods'
17+
require 'hyperstack/internal/i18n/store'
18+
require 'hyperstack/i18n'
19+
require 'hyperstack/i18n/i18n'
1920
else
2021
require 'opal'
2122

ruby/hyper-i18n/lib/hyper-i18n/helper_methods.rb

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)