Skip to content

Commit a14e988

Browse files
authored
Merge pull request #14 from matestack/revert-13-revert-6-textarea
Revert "Revert "Textarea component implementation""
2 parents 7c1e700 + 9617cdf commit a14e988

File tree

5 files changed

+238
-0
lines changed

5 files changed

+238
-0
lines changed

docs/api/form/textarea.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Textarea
2+
3+
The Bootstrap `textarea` form component, implemented in Ruby. Use it like any of the other matestack form components in your apps, pages and components. It offers customizable options to simply achieve what is possible in Bootstrap with this component. See below for more information about the configuration options.
4+
5+
## `bs_form_textarea(*args, &block)`
6+
7+
Renders a Bootstrap textarea field.
8+
9+
**Optional options**
10+
11+
* `label` - Expects a string, gets displayed before the select menu
12+
* `form_text` - Expects a string, gets displayed after the select menu
13+
* `disabled` - If set to `:true`, the input field is set to disabled and clicking them doesn't yield any effec
14+
* `placeholder` - Expects a string to be displayed as a placeholder for whatever the user decides to choose for the `:file` input. Defaults to "Choose file"
15+
* `variant` - Expects a symbol to change the size of the select menu, you can use either `:sm` or `:lg`
16+
* `rows` - Specifies the visible number of lines in a text area.
17+
* `cols` - Specifies the visible width of a textarea.
18+
19+
## Examples
20+
21+
### Example 1: Basic usage
22+
23+
```ruby
24+
bs_form_textarea key: :foo, type: :text
25+
```
26+
27+
### Example 2: Basic usage with label and form text
28+
29+
```ruby
30+
bs_form_textarea key: :foo, type: :text, label: "Input field", form_text: "some notes"
31+
```
32+
33+
### Example 3: Basic usage as disabled input
34+
35+
```ruby
36+
bs_form_textarea key: :foo, type: :text, disabled: true
37+
```
38+
39+
returns
40+
41+
### Example 4: Basic usage with custom class
42+
43+
```ruby
44+
bs_form_textarea key: :foo, type: :text, class: "some-class"
45+
```
46+
47+
### Example 5: Basic usage with placeholder
48+
49+
```ruby
50+
bs_form_textarea key: :foo, type: :text, placeholder: "fill!"
51+
```
52+
53+
### Example 6: Basic usage with rows and cols set
54+
55+
```ruby
56+
bs_form_textarea key: :foo, type: :text, rows: 5, cols: 30
57+
```
58+
59+

lib/matestack/ui/bootstrap.rb

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ module Form
9898
end
9999
require "#{base_path}/form/checkbox"
100100
require "#{base_path}/form/input"
101+
require "#{base_path}/form/textarea"
101102
require "#{base_path}/form/radio"
102103
require "#{base_path}/form/select"
103104
require "#{base_path}/form/submit"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Matestack::Ui::Bootstrap::Form::Textarea < Matestack::Ui::VueJs::Components::Form::Textarea
2+
3+
include Matestack::Ui::Bootstrap::Registry
4+
5+
vue_name "matestack-ui-core-form-textarea"
6+
7+
optional :form_text
8+
optional :disabled
9+
optional :placeholder
10+
optional :rows
11+
optional :cols
12+
13+
def response
14+
div class: "matestack-ui-bootstrap-textarea" do
15+
label input_label, for: attribute_key, class: "form-label" if input_label
16+
textarea options.merge(textarea_attributes).merge(bootstrap_textarea_attributes)
17+
render_errors
18+
render_form_text if context.form_text
19+
end
20+
end
21+
22+
def bootstrap_textarea_attributes
23+
{
24+
id: (options[:id] || attribute_key),
25+
class: (options[:class] || "") << (" form-control"),
26+
rows: context.rows,
27+
cols: context.cols,
28+
disabled: context.disabled
29+
}
30+
end
31+
32+
def render_errors
33+
if display_errors?
34+
div class: 'invalid-feedback', 'v-for': "error in #{error_key}" do
35+
plain '{{ error }}'
36+
end
37+
end
38+
end
39+
40+
def input_error_class
41+
'is-invalid'
42+
end
43+
44+
def render_form_text
45+
div id: "form_text_for_#{attribute_key}", class: "form-text" do
46+
plain context.form_text
47+
end
48+
end
49+
50+
end

lib/matestack/ui/bootstrap/registry.rb

+4
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ def bs_form_input(text=nil, options=nil, &block)
114114
Matestack::Ui::Bootstrap::Form::Input.(text, options, &block)
115115
end
116116

117+
def bs_form_textarea(text=nil, options=nil, &block)
118+
Matestack::Ui::Bootstrap::Form::Textarea.(text, options, &block)
119+
end
120+
117121
def bs_form_select(text=nil, options=nil, &block)
118122
Matestack::Ui::Bootstrap::Form::Select.(text, options, &block)
119123
end

spec/test/form/textarea_spec.rb

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
require 'rails_helper'
2+
require_relative "../../support/form_test_controller"
3+
4+
RSpec.describe "Bootstrap::Form::Textarea", type: :feature, js: true do
5+
include Utils
6+
7+
before :all do
8+
Rails.application.routes.append do
9+
scope "form_text_input_spec" do
10+
post '/textarea_success_form_test', to: 'form_test#success_submit', as: 'textarea_success_form_test'
11+
post '/textarea_failure_form_test', to: 'form_test#failure_submit', as: 'textarea_failure_form_test'
12+
end
13+
end
14+
Rails.application.reload_routes!
15+
end
16+
17+
before :each do
18+
allow_any_instance_of(FormTestController).to receive(:expect_params)
19+
end
20+
21+
it 'renders basic bootstrap textarea field' do
22+
form_config = get_form_config(path: textarea_success_form_test_path)
23+
matestack_render do
24+
matestack_form form_config do
25+
bs_form_textarea key: :foo, type: :text
26+
bs_form_submit text: "Submit"
27+
end
28+
end
29+
visit example_path
30+
expect(page).to have_selector('form > div.matestack-ui-bootstrap-textarea > textarea.form-control#foo')
31+
32+
fill_in "foo", with: "bar"
33+
34+
expect_any_instance_of(FormTestController).to receive(:expect_params)
35+
.with(hash_including(wrapper: { foo: "bar" }))
36+
37+
click_button "Submit"
38+
end
39+
40+
it 'renders basic bootstrap textarea field with server errors' do
41+
form_config = get_form_config(path: textarea_failure_form_test_path)
42+
matestack_render do
43+
matestack_form form_config do
44+
bs_form_textarea key: :foo, type: :text
45+
bs_form_submit text: "Submit"
46+
end
47+
end
48+
visit example_path
49+
50+
click_button "Submit"
51+
52+
expect(page).to have_xpath('//form//textarea[@id="foo" and contains(@class, "form-control") and contains(@class, "is-invalid")]')
53+
54+
expect(page).to have_xpath('//form//div[contains(@class, "invalid-feedback") and contains(text(), "can\'t be blank")]')
55+
expect(page).to have_xpath('//form//div[contains(@class, "invalid-feedback") and contains(text(), "is invalid")]')
56+
end
57+
58+
it 'renders basic bootstrap textarea field with additional custom class' do
59+
form_config = get_form_config(path: textarea_success_form_test_path)
60+
matestack_render do
61+
matestack_form form_config do
62+
bs_form_textarea key: :foo, type: :text, class: "some-class"
63+
bs_form_submit text: "Submit"
64+
end
65+
end
66+
visit example_path
67+
expect(page).to have_xpath('//form//textarea[@id="foo" and contains(@class, "form-control") and contains(@class, "some-class")]')
68+
end
69+
70+
it 'renders basic bootstrap textarea field with label' do
71+
form_config = get_form_config(path: textarea_success_form_test_path)
72+
matestack_render do
73+
matestack_form form_config do
74+
bs_form_textarea key: :foo, type: :text, label: "Some label"
75+
bs_form_submit text: "Submit"
76+
end
77+
end
78+
visit example_path
79+
expect(page).to have_xpath('//form//label[@for="foo" and @class="form-label" and contains(text(), "Some label")]')
80+
end
81+
82+
it 'renders basic bootstrap textarea field with placeholder' do
83+
form_config = get_form_config(path: textarea_success_form_test_path)
84+
matestack_render do
85+
matestack_form form_config do
86+
bs_form_textarea key: :foo, type: :text, placeholder: "fill!"
87+
bs_form_submit text: "Submit"
88+
end
89+
end
90+
visit example_path
91+
expect(page).to have_xpath('//form//textarea[@id="foo" and @placeholder="fill!" and contains(@class, "form-control")]')
92+
end
93+
94+
it 'renders basic bootstrap textarea field with form text' do
95+
form_config = get_form_config(path: textarea_success_form_test_path)
96+
matestack_render do
97+
matestack_form form_config do
98+
bs_form_textarea key: :foo, type: :text, form_text: "some notes"
99+
bs_form_submit text: "Submit"
100+
end
101+
end
102+
visit example_path
103+
expect(page).to have_xpath('//form//div[@id="form_text_for_foo" and contains(@class, "form-text") and contains(text(), "some notes")]')
104+
end
105+
106+
it 'renders basic bootstrap textarea field with form rows and cols' do
107+
form_config = get_form_config(path: textarea_success_form_test_path)
108+
matestack_render do
109+
matestack_form form_config do
110+
bs_form_textarea key: :foo1, id: :foo1, type: :text, rows: 3, cols: 10
111+
bs_form_textarea key: :foo2, id: :foo2, type: :text, rows: 10, cols: 30
112+
bs_form_submit text: "Submit"
113+
end
114+
end
115+
visit example_path
116+
expect(page.find('textarea#foo1')['rows']).to eq '3'
117+
expect(page.find('textarea#foo1')['cols']).to eq '10'
118+
expect(page.find('textarea#foo2')['rows']).to eq '10'
119+
expect(page.find('textarea#foo2')['cols']).to eq '30'
120+
end
121+
122+
123+
124+
end

0 commit comments

Comments
 (0)