Skip to content

Commit

Permalink
Add custom content errors and rename item form component
Browse files Browse the repository at this point in the history
  • Loading branch information
sfnelson committed Dec 6, 2023
1 parent 0860138 commit 6540d84
Show file tree
Hide file tree
Showing 16 changed files with 137 additions and 121 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
katalyst-koi (4.3.0.pre.beta)
katalyst-koi (4.3.0.beta.2)
bcrypt
importmap-rails
katalyst-content
Expand Down Expand Up @@ -191,7 +191,7 @@ GEM
jwt (2.7.1)
katalyst-basic-auth (0.4.0)
rack
katalyst-content (2.0.0)
katalyst-content (2.0.1)
active_storage_validations
katalyst-html-attributes
view_component
Expand Down
3 changes: 3 additions & 0 deletions app/components/koi/content/editor/errors_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= turbo_frame_tag dom_id(container, :errors) do %>
<%= form_builder.govuk_error_summary(**html_attributes) %>
<% end %>
29 changes: 29 additions & 0 deletions app/components/koi/content/editor/errors_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module Koi
module Content
module Editor
class ErrorsComponent < ViewComponent::Base
include Katalyst::HtmlAttributes
include Katalyst::Tables::TurboReplaceable
include Turbo::FramesHelper

attr_reader :container

def initialize(container:)
super()

@container = container
end

def id
dom_id(container, :errors)
end

def form_builder
Koi::FormBuilder.new(container.model_name.param_key, container, self, {})
end
end
end
end
end
92 changes: 92 additions & 0 deletions app/components/koi/content/editor/item_form_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# frozen_string_literal: true

module Koi
module Content
module Editor
class ItemFormComponent < ViewComponent::Base
attr_reader :model, :url, :builder, :form

def initialize(model:, url:, builder: Koi::FormBuilder)
super()

@model = model
@url = url
@builder = builder
end

def form_with(**options, &block)
view_context.form_with(**options) do |form|
@form = form
form.extend(ContentFormHelper)
capture(form, &block)
end
end

def content
@__vc_content_evaluated = true
return @__vc_content if defined?(@__vc_content)

@__vc_content = view_context.capture(@form, &@__vc_render_in_block)
end

def inspect
"#<#{self.class.name} model: #{model.inspect} url: #{url.inspect}>"
end

module ContentFormHelper
def content_heading_fieldset
govuk_fieldset(legend: { text: "Heading", size: "m" }) do
concat(content_heading_field(label: nil))
concat(content_heading_style_field)
end
end

def content_heading_field(args = {})
govuk_text_field :heading,
**{ label: { text: "Heading", size: "s" } }.deep_merge(args)
end

def content_heading_style_field(args = {})
govuk_collection_radio_buttons :heading_style,
Katalyst::Content.config.heading_styles,
:itself,
:itself,
**{ small: true, legend: { text: "Style", size: "s" } }.deep_merge(args)
end

def content_url_field(args = {})
govuk_text_field :url,
**{ label: { text: "URL", size: "s" } }.deep_merge(args)
end

def content_http_method_field(methods, args = {})
govuk_select :http_method, methods,
**{ label: { text: "HTTP method", size: "s" } }.deep_merge(args)
end

def content_target_field(targets, args = {})
govuk_select :target, targets,
**{ label: { text: "HTTP target", size: "s" } }.deep_merge(args)
end

def content_background_field(args = {})
govuk_select :background, Katalyst::Content.config.backgrounds,
**{ label: { size: "s" } }.deep_merge(args)
end

def content_visible_field(args = {})
govuk_check_box_field :visible,
**{ label: { text: "Visible? ", size: "s" }, small: true }.deep_merge(args)
end

def content_buttons
tag.div(class: "actions") do
concat(admin_save("Done"))
concat(admin_discard)
end
end
end
end
end
end
end
90 changes: 0 additions & 90 deletions app/components/koi/content/editor_component.rb

This file was deleted.

21 changes: 0 additions & 21 deletions app/helpers/katalyst/content/editor/errors.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/katalyst/content/asides/_aside.html+form.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<%= render Koi::Content::EditorComponent.new(model: aside, url: path) do |form| %>
<%= render Koi::Content::Editor::ItemFormComponent.new(model: aside, url: path) do |form| %>
<%= form.content_heading_fieldset %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/katalyst/content/columns/_column.html+form.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<%= render Koi::Content::EditorComponent.new(model: column, url: path) do |form| %>
<%= render Koi::Content::Editor::ItemFormComponent.new(model: column, url: path) do |form| %>
<%= form.content_heading_fieldset %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/katalyst/content/contents/_content.html+form.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= render Koi::Content::EditorComponent.new(model: content, url: path) do |form| %>
<%= render Koi::Content::Editor::ItemFormComponent.new(model: content, url: path) do |form| %>
<%= form.content_heading_fieldset %>
<%= form.govuk_rich_text_area :content,
label: { size: "s" },
Expand Down
2 changes: 1 addition & 1 deletion app/views/katalyst/content/figures/_figure.html+form.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= render Koi::Content::EditorComponent.new(model: figure, url: path) do |form| %>
<%= render Koi::Content::Editor::ItemFormComponent.new(model: figure, url: path) do |form| %>
<%= form.govuk_image_field :image do %>
<%= form.govuk_text_field :heading, label: { text: "Alternate text", size: "s" } %>
<%= form.govuk_text_field :caption, label: { text: "Caption (optional)", size: "s" }, optional: true %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/katalyst/content/groups/_group.html+form.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<%= render Koi::Content::EditorComponent.new(model: group, url: path) do |form| %>
<%= render Koi::Content::Editor::ItemFormComponent.new(model: group, url: path) do |form| %>
<%= form.content_heading_fieldset %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/katalyst/content/items/_item.html+form.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<%= render Koi::Content::EditorComponent.new(model: item, url: path) do |form| %>
<%= render Koi::Content::Editor::ItemFormComponent.new(model: item, url: path) do |form| %>
<%= form.content_heading_fieldset %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/katalyst/content/sections/_section.html+form.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<%= render Koi::Content::EditorComponent.new(model: section, url: path) do |form| %>
<%= render Koi::Content::Editor::ItemFormComponent.new(model: section, url: path) do |form| %>
<%= form.content_heading_fieldset %>
<% end %>
3 changes: 3 additions & 0 deletions config/initializers/content.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

Katalyst::Content.config.errors_component = "Koi::Content::Editor::ErrorsComponent"
2 changes: 1 addition & 1 deletion katalyst-koi.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
s.name = "katalyst-koi"
s.version = "4.3.0-beta"
s.version = "4.3.0.beta.2"
s.authors = ["Katalyst Interactive"]
s.email = ["[email protected]"]

Expand Down

0 comments on commit 6540d84

Please sign in to comment.