This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5591bd
commit afd9dcf
Showing
14 changed files
with
471 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
module SexyForm | ||
module Themes | ||
module Bootstrap5Base | ||
|
||
def input_html_attributes(html_attrs:, field_type:, has_errors:) | ||
html_attrs["class"] ||= "" | ||
html_attrs["class"].concat(" is-invalid").strip! if has_errors | ||
|
||
case field_type | ||
when "checkbox", "radio" | ||
html_attrs["class"].concat(" form-check-input").strip! | ||
when "select" | ||
html_attrs["class"].concat(" form-select").strip! | ||
else | ||
html_attrs["class"].concat(" form-control").strip! | ||
end | ||
|
||
html_attrs | ||
end | ||
|
||
def label_html_attributes(html_attrs:, field_type:, has_errors:) | ||
if ["checkbox", "radio"].include?(field_type) | ||
html_attrs["class"] = "form-check-label #{html_attrs['class']}".strip | ||
end | ||
|
||
html_attrs | ||
end | ||
|
||
def form_html_attributes(html_attrs:) | ||
html_attrs | ||
end | ||
|
||
def build_html_help_text(help_text:, html_attrs:, field_type:) | ||
html_attrs["class"] = "form-text #{html_attrs['class']}".strip | ||
html_attrs["style"] = "display:block; #{html_attrs['style']}".strip | ||
|
||
s = "" | ||
s << SexyForm.build_html_element(:small, html_attrs) | ||
s << "#{help_text}" | ||
s << "</small>" | ||
s | ||
end | ||
|
||
def build_html_error(error:, html_attrs:, field_type:) | ||
html_attrs["class"] = "invalid-feedback #{html_attrs['class']}".strip | ||
|
||
s = "" | ||
s << "<div #{SexyForm.build_html_attr_string(html_attrs)}>" | ||
s << "#{error}" | ||
s << "</div>" | ||
s | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module SexyForm | ||
module Themes | ||
class Bootstrap5Horizontal < BaseTheme | ||
include Bootstrap5Base | ||
|
||
def self.theme_name | ||
"bootstrap_5_horizontal" | ||
end | ||
|
||
def initialize(column_classes: ["col-sm-3", "col-sm-9"]) | ||
@column_classes = column_classes.first(2) | ||
|
||
s = "#{@column_classes[0]}" | ||
@offset_class = (i = s.index(/-\d/)) ? s.insert(i+1, "offset-") : "" | ||
end | ||
|
||
def wrap_field(field_type:, html_field:, html_label:, html_help_text: nil, html_errors: nil, wrapper_html_attributes:) | ||
s = "" | ||
|
||
s << SexyForm.build_html_element(:div, wrapper_html_attributes) | ||
|
||
if ["checkbox", "radio"].include?(field_type) | ||
s << %Q(<div class="row">) | ||
|
||
s << %Q(<div class="#{@column_classes[0]}"></div>) | ||
|
||
s << %Q(<div class="#{@column_classes[1]}">) | ||
s << %Q(<div class="form-check">) | ||
s << "#{html_field}" | ||
s << "#{html_label}" | ||
s << "#{html_help_text}" | ||
s << html_errors.join if html_errors | ||
s << "</div>" | ||
s << "</div>" | ||
|
||
s << "</div>" | ||
else | ||
s << %Q(<div class="row">) | ||
|
||
s << %Q(<div class="#{@column_classes[0]}">) | ||
s << "#{html_label}" | ||
s << "</div>" | ||
|
||
s << %Q(<div class="#{@column_classes[1]}">) | ||
s << "#{html_field}" | ||
s << "#{html_help_text}" | ||
s << html_errors.join if html_errors | ||
s << "</div>" | ||
|
||
s << "</div>" | ||
end | ||
|
||
s << "</div>" | ||
end | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module SexyForm | ||
module Themes | ||
class Bootstrap5Inline < BaseTheme | ||
include Bootstrap5Base | ||
|
||
def self.theme_name | ||
"bootstrap_5_inline" | ||
end | ||
|
||
def wrap_field(field_type:, html_field:, html_label:, html_help_text: nil, html_errors: nil, wrapper_html_attributes:) | ||
s = "" | ||
|
||
if ["checkbox", "radio"].include?(field_type) | ||
wrapper_html_attributes["class"] = "col-auto form-check #{wrapper_html_attributes['class']}".strip | ||
|
||
s << SexyForm.build_html_element(:div, wrapper_html_attributes) | ||
s << html_field | ||
s << html_label | ||
else | ||
s << %Q(<div class="col-auto">#{html_label}</div>) | ||
s << %Q(<div class="col-auto">) | ||
s << html_field | ||
end | ||
|
||
s << "#{html_help_text}" | ||
s << html_errors.join if html_errors | ||
s << "</div>" | ||
|
||
s | ||
end | ||
|
||
def form_html_attributes(html_attrs:) | ||
html_attrs["class"] = "row #{html_attrs['class']}".strip | ||
super(html_attrs: html_attrs) | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module SexyForm | ||
module Themes | ||
class Bootstrap5Vertical < BaseTheme | ||
include Bootstrap5Base | ||
|
||
def self.theme_name | ||
"bootstrap_5_vertical" | ||
end | ||
|
||
def wrap_field(field_type:, html_field:, html_label:, html_help_text: nil, html_errors: nil, wrapper_html_attributes:) | ||
s = "" | ||
|
||
if ["checkbox", "radio"].include?(field_type) | ||
wrapper_html_attributes["class"] = "form-check #{wrapper_html_attributes['class']}".strip | ||
end | ||
|
||
s << SexyForm.build_html_element(:div, wrapper_html_attributes) | ||
|
||
if ["checkbox", "radio"].include?(field_type) | ||
s << "#{html_field}" | ||
s << "#{html_label}" | ||
else | ||
s << "#{html_label}" | ||
s << "#{html_field}" | ||
end | ||
s << "#{html_help_text}" | ||
s << html_errors.join if html_errors | ||
|
||
s << "</div>" | ||
|
||
s | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
require_relative "../../spec_helper" | ||
require_relative "theme_spec_helper" | ||
|
||
base_klass = SexyForm::Themes::Bootstrap5Base | ||
theme_klass = SexyForm::Themes::Bootstrap5Horizontal | ||
theme = theme_klass.new | ||
|
||
describe theme do | ||
|
||
it "is not a valid theme" do | ||
expect(base_klass < SexyForm::Themes::BaseTheme).to eq(nil) | ||
expect{ SexyForm.form(theme: base_klass) }.to raise_error(ArgumentError) | ||
end | ||
|
||
describe "form_html_attributes" do | ||
it "returns correct attributes" do | ||
attrs = {} | ||
|
||
theme.form_html_attributes(html_attrs: {}).should eq(attrs) | ||
end | ||
end | ||
|
||
TESTED_FIELD_TYPES.each do |field_type| | ||
describe "input_html_attributes" do | ||
it "returns correct #{field_type} attributes" do | ||
attrs = {} | ||
|
||
case field_type | ||
when "checkbox", "radio" | ||
attrs["class"] = "form-check-input" | ||
when "select" | ||
attrs["class"] = "form-select" | ||
else | ||
attrs["class"] = "form-control" | ||
end | ||
|
||
theme.input_html_attributes(html_attrs: {}, field_type: field_type, has_errors: false).should eq(attrs) | ||
end | ||
end | ||
|
||
describe "label_html_attributes" do | ||
it "returns correct #{field_type} attributes" do | ||
attrs = {} | ||
|
||
if ["checkbox", "radio"].include?(field_type) | ||
attrs["class"] = "form-check-label" | ||
end | ||
|
||
theme.label_html_attributes(html_attrs: {}, field_type: field_type, has_errors: false).should eq(attrs) | ||
end | ||
end | ||
|
||
describe "build_html_help_text" do | ||
it "returns correct #{field_type} attributes" do | ||
expected = %Q(<small class="form-text" style="display:block;">foobar</small>) | ||
|
||
attrs = {} | ||
|
||
theme.build_html_help_text(html_attrs: attrs, field_type: field_type, help_text: "foobar").should eq(expected) | ||
end | ||
end | ||
|
||
describe "build_html_error" do | ||
it "returns correct #{field_type} attributes" do | ||
expected = "<div class=\"invalid-feedback\">foobar</div>" | ||
|
||
attrs = {} | ||
|
||
theme.build_html_error(html_attrs: attrs, field_type: field_type, error: "foobar").should eq(expected) | ||
end | ||
end | ||
end | ||
|
||
end |
Oops, something went wrong.