-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
My take on view_component_reflex with --sidecar nested. #24
Comments
I'll leave application_component.rb class ApplicationComponent < ViewComponentReflex::Component
end & the example_component.rb just in case module My
class ExampleComponent < ApplicationComponent
attr_accessor :count
def initialize
@count = 1
end
def increment
self.count += 1
end
end
end & the html <%= component_controller do %>
<p><%= count %></p>
<%= reflex_tag :increment, :button, "Click" %>
<% end %> |
Following @sebyx07 approach I modified stimulus controller naming resolvers to match following "sidecar" folder structure.
I had to modify const cvContext = require.context("../../components", true, /_controller\.js$/)
function getDefinitionsFromContext(ctx){
return ctx.keys().map((path) => {
const splitPath = path.split("/");
const identifier = splitPath.slice(1, splitPath.length - 1).join("--").replace(/_component/g, "").replace('_', '-');
const obj = ctx(path);
const klassKey = Object.keys(obj)[0];
const controllerConstructor = obj[klassKey];
return {
identifier,
controllerConstructor
};
});
}
application.load(getDefinitionsFromContext(cvContext)) and override ViewComponentReflex::Component.module_eval do
def self.stimulus_controller
name.gsub('Component', '').underscore.dasherize.gsub("/", "--")
end
end This approach allowed ...
<div data-controller="parent" key="123"></div>
...
<div data-controller="parent--first-child" key="456"></div>
... and to be properly initialized by Stimulus framework. |
This is how I use view_component_reflex with the --sidecar layout from view_component.
first the dir structure
Before putting the .js & .scss you must change the application.js to load controllers properly. Here is mine:
My application_controller.js, which is pretty standard
I'll skip example_component.rb, example_component.html.erb because they are standard
Here is the example_component_controller.js, which also links the .scss file
import "./example_component.scss"
import ApplicationController from "../../../javascript/controllers/application_controller";
finally the .scss which is scoped to this component, nothing fancy, but usefull
Would be cool if someone will write a generator for this so we could just
rails g component my_awesome/nested_component
The text was updated successfully, but these errors were encountered: