Skip to content
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

Add gem configuration, with base component customization #160

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ You can use the same approach to inject options, wrap the input in a `<div>`, et

We'll add more use cases to the documentation soon.

### Configuration

| Attribute | Purpose | Default |
| ------------------ | ----------------------------------------------- | --------------------- |
| `parent_component` | Inherited by all ViewComponent::Form components | `ViewComponent::Base` |
joelzwarrington marked this conversation as resolved.
Show resolved Hide resolved

### Building your own components

When building your own ViewComponents for using in forms, it's recommended to inherit from `ViewComponent::Form::FieldComponent`, so you get access to the following helpers:
Expand Down Expand Up @@ -339,6 +345,22 @@ def length_validator
end
```

### Setting up your own base component class

1. Setup some base component from which the form components will inherit from
```rb
class ApplicationFormComponent < ViewComponent::Base
end
```
2. Configure the parent component class
```rb
# config/initializers/vcf.rb

ViewComponent::Form.configure do |config|
config.parent_component = 'ApplicationFormComponent'
end
```

### Using your form components without a backing model

If you want to ensure that your fields display consistently across your app, you'll need to lean on Rails' own helpers. You may be used to using form tag helpers such as `text_field_tag` to generate tags, or even writing out plain HTML tags. These can't be integrated with a form builder, so they won't offer you the benefits of this gem.
Expand Down
2 changes: 1 addition & 1 deletion app/components/view_component/form/base_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module ViewComponent
module Form
class BaseComponent < ViewComponent::Base
class BaseComponent < ViewComponent::Form.configuration.parent_component.constantize
class << self
attr_accessor :default_options
end
Expand Down
9 changes: 9 additions & 0 deletions lib/view_component/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@

module ViewComponent
module Form
class << self
def configuration
@configuration ||= Configuration.new
end

def configure
yield configuration
end
end
end
end

Expand Down
13 changes: 13 additions & 0 deletions lib/view_component/form/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module ViewComponent
module Form
class Configuration
attr_accessor :parent_component

def initialize
@parent_component = "ViewComponent::Base"
end
end
end
end
4 changes: 4 additions & 0 deletions spec/internal/app/components/application_form_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class ApplicationFormComponent < ViewComponent::Base
end
5 changes: 5 additions & 0 deletions spec/internal/config/initializers/view_component_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

ViewComponent::Form.configure do |config|
config.parent_component = "ApplicationFormComponent"
end
8 changes: 8 additions & 0 deletions spec/view_component/form/base_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ def name
it { expect(component.object_errors?).to be(false) }
end
end

describe "parent_component" do
subject { described_class }

context "without configured parent_component" do
it { is_expected.to be < ViewComponent::Base }
end
end
end
8 changes: 8 additions & 0 deletions spec/view_component/form/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,12 @@
it { expect(builder.send(:validation_context)).to eq(:create) }
end
end

describe "base component parent" do
subject(:field) { described_class.new(object_name, object, template, options).send(:component_klass, :text_field) }

it "is configured via initializer" do
expect(field.ancestors).to include(ApplicationFormComponent)
end
end
end
11 changes: 11 additions & 0 deletions spec/view_component/form/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

RSpec.describe ViewComponent::Form::Configuration do
subject(:configuration) { described_class.new }

describe "defaults" do
it do
expect(configuration).to have_attributes(parent_component: "ViewComponent::Base")
end
end
end
4 changes: 4 additions & 0 deletions spec/view_component/form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
expect(ViewComponent::Form::VERSION).not_to be_nil
end

it "is configurable" do
expect { |block| described_class.configure(&block) }.to yield_with_args(ViewComponent::Form::Configuration)
end

if ENV.fetch("VIEW_COMPONENT_FORM_USE_ACTIONTEXT", "false") == "true"
it "loads ActionText" do
expect(defined?(ActionView::Helpers::Tags::ActionText)).to eq("constant")
Expand Down