提交 d7b60544 编写于 作者: N neumayr

date_select helper with_css_classes option also accept a hash

`date_select` helper `:with_css_classes` option now accepts a hash of strings
for `:year`, `:month`, `:day`, `:hour`, `:minute`, `:second` that will extend
the select type with the given css class value.

```erb
<%= f.date_select :birthday, with_css_classes: { month: "my-month", year: "my-year" } %>
```

```html
<select id="user_birthday_3i" name="user[birthday(3i)]">…</select>
<select id="user_birthday_2i" name="user[birthday(2i)]" class="my-month">…</select>
<select id="user_birthday_1i" name="user[birthday(1i)]" class="my-year">…</select>
```

Optional, add global `html_options` to modify every select tag in the set.

```erb
<%= f.date_select :birthday, with_css_classes: { month: "my-month", year: "my-year" }, { class: "my-date optional" } %>
```

Supported DateHelper methods: `select_day`, `select_month`, `select_year`,
`select_hour`, `select_minute`, `select_second`, `select_datetime`, `select_time`,
`time_select`, `date_select` and `datetime_select`.

`:with_css_classes` option was added to the `date_select` with #7975.
上级 3108e080
* `date_select` helper `:with_css_classes` option now accepts a hash of strings
for `:year`, `:month`, `:day`, `:hour`, `:minute`, `:second` that will extend
the select type with the given css class value.
```erb
<%= f.date_select :birthday, with_css_classes: { month: "my-month", year: "my-year" } %>
```
```html
<select id="user_birthday_3i" name="user[birthday(3i)]">…</select>
<select id="user_birthday_2i" name="user[birthday(2i)]" class="my-month">…</select>
<select id="user_birthday_1i" name="user[birthday(1i)]" class="my-year">…</select>
```
*Matthias Neumayr*
* Deprecate `datetime_field` and `datetime_field_tag` helpers.
Datetime input type was removed from HTML specification.
One can use `datetime_local_field` and `datetime_local_field_tag` instead.
......
......@@ -226,8 +226,10 @@ def time_ago_in_words(from_time, options = {})
# for <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>, <tt>:hour</tt>, <tt>:minute</tt> and <tt>:second</tt>.
# Setting this option prepends a select option with a generic prompt (Day, Month, Year, Hour, Minute, Seconds)
# or the given prompt string.
# * <tt>:with_css_classes</tt> - Set to true if you want assign different styles for 'select' tags. This option
# automatically set classes 'year', 'month', 'day', 'hour', 'minute' and 'second' for your 'select' tags.
# * <tt>:with_css_classes</tt> - Set to true or a hash of strings. Use true if you want assign generic styles for
# select tags. This automatically set classes 'year', 'month', 'day', 'hour', 'minute' and 'second'. A hash of
# strings for <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>, <tt>:hour</tt>, <tt>:minute</tt>, <tt>:second</tt>
# will extend the select type with the given value. Use +html_options+ to modify every select tag in the set.
# * <tt>:use_hidden</tt> - Set to true if you only want to generate hidden input tags.
#
# If anything is passed in the +html_options+ hash it will be applied to every select tag in the set.
......@@ -994,7 +996,7 @@ def build_select(type, select_options_as_html)
:name => input_name_from_type(type)
}.merge!(@html_options)
select_options[:disabled] = 'disabled' if @options[:disabled]
select_options[:class] = [select_options[:class], type].compact.join(' ') if @options[:with_css_classes]
select_options[:class] = css_class_attribute(type, select_options[:class], @options[:with_css_classes]) if @options[:with_css_classes]
select_html = "\n"
select_html << content_tag("option".freeze, '', :value => '') + "\n" if @options[:include_blank]
......@@ -1004,6 +1006,20 @@ def build_select(type, select_options_as_html)
(content_tag("select".freeze, select_html.html_safe, select_options) + "\n").html_safe
end
# Builds the css class value for the select element
# css_class_attribute(:year, 'date optional', with_css_classes: { year: 'my-year' })
# => "date optional my-year"
def css_class_attribute(type, html_options_class, options)
css_class = case options
when Hash
options[type.to_sym]
else
type
end
[html_options_class, css_class].compact.join(' ')
end
# Builds a prompt option tag with supplied options or from default options.
# prompt_option_tag(:month, prompt: 'Select month')
# => "<option value="">Select month</option>"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册