hyperstack-org / hyperstack-org/hyperstack

when processing props detect and handle native objects

Aberta
#311 0 comentários 0 reações 0 responsáveis Ver no GitHub
Linguagem predominante
JavaScript
Estrelas
538
Forks
41
Métricas de merge de PRs
Nenhum PR com merge em 30d

Descrição

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.

Guia de contribuição

Abrir o guia de contribuição

Avaliação

Esta issue ainda não foi avaliada.

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.