hyperstack-org / hyperstack-org/hyperstack

when processing props detect and handle native objects

Đang mở
#311 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
JavaScript
Star
538
Fork
41
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

Consider this case:

```ruby
MuiLab::Autocomplete(size: :small, options: Model.all)
.on('', &:text)
.on('') { |params| Mui.TextField(params, label: 'Add Item').to_n }
.on(:change) { |_, option| mutate @value = option }
```

Here the js `Autocomplete` component does a call back to `renderInput` passing it a parameter block (which will be a native js object) and expects the callback to return a component built using those params plus other params (such as the label).

The problem is the above code won't work since params is a native JS object.

You could try saying `Mui.TextField(Hash.new(params), ...)` but that won't work either since `Hash.new` does a deep conversion of the params object, **BUT** when params are passed *back* to native components (like `TextField`) the param values are **NOT** converted back.

You could work around this by creating a method that creates a hash from the params just using the keys, but preserving the values as native objects.

However its a lot of work.

Instead the solution is to detect and pass through native objects into the parameters.

The patch is in two parts:

```ruby
module Hyperstack
module Internal
module Component
class RenderingContext
# patched to allow args to be native objects
def self.remove_nodes_from_args(args)
args.each do |arg|
begin
arg.each do |key, value|
value.delete if value.is_a?(Hyperstack::Component::Element)
end if arg.is_a?(Hash)
rescue Exception
# supress all exceptions which will be due to not being able
# to execute is_a? on native objects
end
end
end
end
class ReactWrapper
class << self
alias original_convert_props convert_props

# patch to do a shallow hash conversion on native objects
def convert_props(type, *args)
args = args.collect do |arg|
next arg unless (native_arg = Native(arg)).is_a? Native::Object

# create a shallow hash, with each value unchanged from the JS object
Hash.new(native_arg.to_n).tap { |p| p.each_key { |key| p[key] = `native_arg.native[key]` } }
end
original_convert_props(type, *args)
end
end
end
end
end
end
```

Note the `convert_props` is presented as patch. The actual fix would be to just detect and deal with native objects as part of the props processing that already exists in convert_props.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.