react_component_helper.rb 540 B

1234567891011121314151617181920212223
  1. # frozen_string_literal: true
  2. module ReactComponentHelper
  3. def react_component(name, props = {}, &block)
  4. data = { component: name.to_s.camelcase, props: Oj.dump(props) }
  5. if block.nil?
  6. div_tag_with_data(data)
  7. else
  8. content_tag(:div, data: data, &block)
  9. end
  10. end
  11. def react_admin_component(name, props = {})
  12. data = { 'admin-component': name.to_s.camelcase, props: Oj.dump(props) }
  13. div_tag_with_data(data)
  14. end
  15. private
  16. def div_tag_with_data(data)
  17. content_tag(:div, nil, data: data)
  18. end
  19. end