Created
November 13, 2019 19:58
-
-
Save humphreyja/6de78228186d4e8eaa69ef3b7277c3a1 to your computer and use it in GitHub Desktop.
Include react components into active admin using React On Rails.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/active_admin.rb | |
#... | |
module ActiveAdmin | |
module Views | |
# Defines a `react_component` active admin component. It allows you | |
# to include react components into active admin using React On Rails. | |
# Example: | |
# | |
# show do | |
# panel 'My React Component' do | |
# react_component 'EmailSignUpResource', props: { | |
# resource_slug: 'test', | |
# crsf_token: form_authenticity_token | |
# } | |
# end | |
# end | |
class React < ActiveAdmin::Component | |
# Internal React on Rails helper class | |
class ReactComponentBuilder | |
include ReactOnRails::Helper | |
include ActionView::Helpers::TagHelper | |
end | |
attr_accessor :component_name, :options | |
builder_method :react_component | |
def to_s | |
ReactComponentBuilder.new.react_component(@component_name, @options) | |
end | |
def build(component_name, options = {}) | |
@component_name = component_name | |
@options = options | |
end | |
end | |
end | |
end | |
# NOTE: If you are have issues getting webpacker content to show up in active admin, add the follow to the intializer. | |
# This will include `app/views/admin/_header_additions.html.erb` into the head of the page. In that view, you can add | |
# your calls to include the webpacker content. | |
module AdminPageLayoutOverride | |
def build_active_admin_head | |
super | |
within head do | |
render '/admin/header_additions' | |
end | |
end | |
end | |
ActiveAdmin::Views::Pages::Base.prepend AdminPageLayoutOverride |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment