Skip to content

Commit 1307650

Browse files
authored
Merge pull request #15 from matestack/20210603_issue_#9_number_input_for_floats
use step, min, max option for all input type, enabling float number …
2 parents 4275a64 + 82ef123 commit 1307650

File tree

3 files changed

+65
-15
lines changed

3 files changed

+65
-15
lines changed

docs/api/form/input.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,72 +15,77 @@ Renders a Bootstrap input field.
1515
* `browse_button_text` - Expects a string and sets the text to be displayed in the "browse" button for the `:file` input
1616
* `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"
1717
* `variant` - Expects a symbol to change the size of the select menu, you can use either `:sm` or `:lg`
18-
* `min` - Sets the corresponding HTML attribute for the `:range` input type
19-
* `max` - Sets the corresponding HTML attribute for the `:range` input type
20-
* `step` - Sets the corresponding HTML attribute for the `:range` input type
18+
* `min` - Sets the corresponding HTML attribute
19+
* `max` - Sets the corresponding HTML attribute
20+
* `step` - Sets the corresponding HTML attribute
2121
* `show_value` - Expects a boolean. Defaults to `false`, if set to `true` it displays the current value for the corresponding `:range` input type field
2222

2323
## Examples
2424

25-
### Example 1: Basic usage
25+
### Basic usage for text input
2626

2727
```ruby
2828
bs_form_input key: :foo, type: :text
2929
```
3030

31-
### Example 2: Basic usage with label and form text
31+
### Basic usage for float number input
32+
33+
```ruby
34+
bs_form_input key: :foo, type: :number, step: 0.01
35+
```
36+
37+
### Basic usage with label and form text
3238

3339
```ruby
3440
bs_form_input key: :foo, type: :text, label: "Input field", form_text: "some notes"
3541
```
3642

37-
### Example 3: Basic usage as disabled input
43+
### Basic usage as disabled input
3844

3945
```ruby
4046
bs_form_input key: :foo, type: :text, disabled: true
4147
```
4248

4349
returns
4450

45-
### Example 4: Basic usage with custom class
51+
### Basic usage with custom class
4652

4753
```ruby
4854
bs_form_input key: :foo, type: :text, class: "some-class"
4955
```
5056

51-
### Example 5: Basic usage with placeholder
57+
### Basic usage with placeholder
5258

5359
```ruby
5460
bs_form_input key: :foo, type: :text, placeholder: "fill!"
5561
```
5662

57-
### Example 6: Basic usage as range input
63+
### Basic usage as range input
5864

5965
```ruby
6066
bs_form_input key: :some_range_input, type: :range, max: 10
6167
```
6268

63-
### Example 7: Using range input with show\_value and non-default steps
69+
### Using range input with show\_value and non-default steps
6470

6571
```ruby
6672
bs_form_input key: :some_range_input, type: :range, step: 2, max: 10, show_value: true
6773
```
6874

69-
### Example 8: Basic usage as file input
75+
### Basic usage as file input
7076

7177
```ruby
7278
bs_form_input key: :some_single_file_input, type: :file
7379
```
7480

75-
### Example 9: File input with non-default size and optional form\_text
81+
### File input with non-default size and optional form\_text
7682

7783
```ruby
7884
bs_form_input variant: :lg, form_text: "just some notes", key: :some_single_file_input, type: :file
7985
```
8086

81-
### Example 10: Multi-file input with placeholder and browse\_button\_text
87+
### Multi-file input with placeholder and browse\_button\_text
8288

8389
```ruby
8490
bs_form_input placeholder: "Select a file", browse_button_text: "Click", key: :some_multi_file_input, type: :file, multiple: true
8591
```
86-

lib/matestack/ui/bootstrap/form/input.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ def bootstrap_input_attributes
3939
{
4040
id: (options[:id] || attribute_key),
4141
class: (options[:class] || "") << (" form-control"),
42-
disabled: context.disabled
42+
disabled: context.disabled,
43+
min: context.min,
44+
max: context.max,
45+
step: context.step
4346
}
4447
end
4548

spec/test/form/input_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,48 @@
103103
expect(page).to have_xpath('//form//div[@id="form_text_for_foo" and contains(@class, "form-text") and contains(text(), "some notes")]')
104104
end
105105

106+
it 'renders basic bootstrap input field accepting float numbers' do
107+
form_config = get_form_config(path: input_success_form_test_path)
108+
matestack_render do
109+
matestack_form form_config do
110+
bs_form_input key: :foo, type: :number, step: 0.01
111+
bs_form_submit text: "Submit"
112+
end
113+
end
114+
visit example_path
115+
fill_in "foo", with: 1.45
116+
117+
expect_any_instance_of(FormTestController).to receive(:expect_params)
118+
.with(hash_including(wrapper: { foo: 1.45 }))
119+
120+
click_button "Submit"
121+
end
122+
123+
it 'renders basic bootstrap input field accepting float numbers as text with , delimeter via pattern regex' do
124+
form_config = get_form_config(path: input_success_form_test_path)
125+
matestack_render do
126+
matestack_form form_config do
127+
bs_form_input key: :foo, type: :text, pattern: "[0-9]+([,][0-9]+)?"
128+
bs_form_submit text: "Submit"
129+
end
130+
end
131+
visit example_path
132+
133+
fill_in "foo", with: "1.45"
134+
135+
expect_any_instance_of(FormTestController).not_to receive(:expect_params)
136+
.with(hash_including(wrapper: { foo: "1.45" }))
137+
138+
click_button "Submit"
139+
140+
fill_in "foo", with: "1,45"
141+
142+
expect_any_instance_of(FormTestController).to receive(:expect_params)
143+
.with(hash_including(wrapper: { foo: "1,45" }))
144+
145+
click_button "Submit"
146+
end
147+
106148
it 'renders basic bootstrap range input' do
107149
form_config = get_form_config(path: input_success_form_test_path)
108150
matestack_render do

0 commit comments

Comments
 (0)