Skip to content

Commit

Permalink
fix(bulma): Add Bulma CSS generator
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanVqz committed Jun 28, 2024
1 parent 1117a2b commit 7da1157
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 3 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# SimpleForm::Theme
Short description and motivation.

### Supported CSS Frameworks
* [Tailwind CSS](https://tailwindcss.com/docs/installation)
* [Bulma](https://bulma.io/documentation/start/installation)

## Installation
Add this line to your application's Gemfile:

Expand All @@ -26,8 +30,7 @@ However, if you install the gem, you will get the latest updates and improvement
## Requirements

* Make sure you have installed [simple_form](https://github.com/heartcombo/simple_form) gem.
* Make sure you have installed the decired css framework. (Installing the CSS Framework is out of the scope of this gem).
* [Tailwindcss](https://tailwindcss.com/docs/installation)
* Make sure you have installed the decired css framework.

## Usage

Expand All @@ -37,8 +40,14 @@ However, if you install the gem, you will get the latest updates and improvement
bin/rails generate simple_form:theme:tailwind install
```

### Install Bulma CSS initializer

```bash
bin/rails generate simple_form:theme:bulma install
```

## Contributing
Contribution directions go here.
Bug reports and pull requests are welcome on GitHub at https://github.com/JuanVqz/simple_form-theme

## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
8 changes: 8 additions & 0 deletions lib/generators/simple_form/theme/bulma/USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Description:
Install the simple form bulma config file in your app.

Example:
bin/rails generate simple_form:theme:bulma install

This will create:
config/initializers/simple_form_bulma.rb
10 changes: 10 additions & 0 deletions lib/generators/simple_form/theme/bulma/bulma_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class SimpleForm::Theme::BulmaGenerator < Rails::Generators::NamedBase
source_root File.expand_path('templates', __dir__)

desc 'Copy the bulma initializer file for simple_form'
def copy_initializer_file
template 'config/initializers/simple_form_bulma.rb', 'config/initializers/simple_form_bulma.rb'
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# frozen_string_literal: true

# Use this setup block to configure all options available in SimpleForm.
SimpleForm.setup do |config|
# Default class for buttons
config.button_class = 'button'

# Define the default class of the input wrapper of the boolean input.
config.boolean_label_class = 'checkbox'

# How the label text should be generated altogether with the required text.
config.label_text = ->(label, required, _explicit_label) { "#{label} #{required}" }

# Define the way to render check boxes / radio buttons with labels.
config.boolean_style = :inline

# You can wrap each item in a collection of radio/check boxes with a tag
config.item_wrapper_tag = :div

# Defines if the default input wrapper class should be included in radio
# collection wrappers.
config.include_default_input_wrapper_class = false

# CSS class to add for error notification helper.
config.error_notification_class = 'notification is-danger'

# Method used to tidy up errors. Specify any Rails Array method.
# :first lists the first message for each field.
# :to_sentence to list all errors for each field.
config.error_method = :to_sentence

# add validation classes to `input_field`
config.input_field_error_class = 'is-danger'
config.input_field_valid_class = 'is-success'

# vertical forms
#
# bulma vertical default_wrapper
config.wrappers :vertical_form, tag: 'div', class: 'field' do |b|
b.use :html5
b.use :placeholder
b.optional :maxlength
b.optional :minlength
b.optional :pattern
b.optional :min_max
b.optional :readonly
b.use :label, class: 'label'
b.use :input, class: 'input', wrap_with: { tag: 'div', class: 'control' }, error_class: 'is-danger', valid_class: 'is-success'
b.use :full_error, wrap_with: { tag: 'div', class: 'help is-danger' }
b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' }
end

# bulma vertical select_form
config.wrappers :select_form, tag: 'div', class: 'control' do |b|
b.use :html5
b.use :placeholder
b.optional :pattern
b.optional :readonly
b.use :input, wrap_with: { tag: 'div', class: 'select' }
b.use :full_error, wrap_with: { tag: 'div', class: 'help is-danger' }
b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' }
end

# bulma extension vertical input for boolean
config.wrappers :vertical_boolean, tag: 'div', class: 'field' do |b|
b.use :html5
b.optional :readonly
b.use :input, class: 'is-checkradio is-info'
b.use :label
# b.wrapper :form_check_wrapper, tag: 'div', class: 'control' do |bb|
# bb.use :input, wrap_with: { tag: 'label', class: 'checkbox' }
# bb.use :label, class: 'checkbox'
# bb.use :full_error, wrap_with: { tag: 'div', class: 'help is-danger' }
# bb.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' }
# end
end

## vertical input for radio buttons and check boxes
config.wrappers :vertical_collection, item_wrapper_class: 'form-check', tag: 'fieldset', class: 'form-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b|
b.use :html5
b.optional :readonly
b.wrapper :legend_tag, tag: 'legend', class: 'col-form-label pt-0' do |ba|
ba.use :label_text
end
b.use :input, class: 'form-check-input', error_class: 'is-invalid', valid_class: 'is-valid'
b.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback d-block' }
b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' }
end

## bulma vertical file input
config.wrappers :vertical_file, tag: 'div', class: 'file' do |b|
b.use :html5
b.optional :readonly
b.use :input, class: 'file-input', wrap_with: { tag: 'label', class: 'file-label' }
b.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback d-block' }
b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' }
end

## bulma vertical multi select
config.wrappers :vertical_multi_select, tag: 'div', class: 'field' do |b|
b.use :html5
b.optional :readonly
b.use :label, class: 'label'
b.wrapper tag: 'div', class: 'control' do |ba|
ba.use :input, class: 'input', error_class: 'is-danger', valid_class: 'is-success'
end
b.use :full_error, wrap_with: { tag: 'div', class: 'is-danger' }
b.use :hint, wrap_with: { tag: 'small', class: 'help' }
end

# The default wrapper to be used by the FormBuilder.
config.default_wrapper = :vertical_form

# Custom wrappers for input types. This should be a hash containing an input
# type as key and the wrapper that will be used for all inputs with specified type.
config.wrapper_mappings = {
boolean: :vertical_boolean,
check_boxes: :vertical_collection,
date: :vertical_multi_select,
datetime: :vertical_multi_select,
file: :vertical_file,
radio_buttons: :vertical_collection,
range: :vertical_range,
time: :vertical_multi_select
}
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require 'test_helper'
require 'generators/simple_form/theme/bulma/bulma_generator'

module SimpleForm::Theme
class SimpleForm::Theme::BulmaGeneratorTest < Rails::Generators::TestCase
tests SimpleForm::Theme::BulmaGenerator
destination Rails.root.join('tmp/generators')
setup :prepare_destination

test 'generator runs without errors' do
assert_nothing_raised do
run_generator ['arguments']
end
end
end
end

0 comments on commit 7da1157

Please sign in to comment.