Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

date_select helper with_css_classes option also accept a hash #24225

Conversation

neumayr
Copy link
Contributor

@neumayr neumayr commented Mar 17, 2016

Hey! It's my first PR to rails. Please, be nice 😉

Research/History

With #7975 the :with_css_classes option was added to the date_select helper.
This option automatically set generic classes 'year', 'month', 'day', 'hour', 'minute' and 'second' for your 'select' tags. For example:

<%= f.date_select :birthday, with_css_classes: true %>
<select id="user_birthday_3i" name="user[birthday(3i)]" class="day"></select>
<select id="user_birthday_2i" name="user[birthday(2i)]" class="month"></select>
<select id="user_birthday_1i" name="user[birthday(1i)]" class="year"></select>

Select groups get their generic class. CSS rules can be specified. Nice, but not perfect.

Improvement

Sometimes select tags get some love and receive nice styling. If only one select element
must contain a custom css classes this was hard to achieve.

Instead of an boolean value pass a hash of strings for :year, :month, :day, :hour,
:minute, :second to with_css_classes. This will extend the select type with the given value.
Similar how :prompt is doing this.

<%= f.date_select :birthday, with_css_classes: { month: "my-month", year: "my-year" } %>
<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.

<%= f.date_select :birthday, with_css_classes: { month: "my-month", year: "my-year" }, { class: "my-date optional" } %>
<select id="user_birthday_3i" name="user[birthday(3i)]" class="my-date optional"></select>
<select id="user_birthday_2i" name="user[birthday(2i)]" class="my-date optional my-month"></select>
<select id="user_birthday_1i" name="user[birthday(1i)]" class="my-date optional my-year"></select>

The hash for with_css_classes can applied to DateHelper methods:

  • select_day
  • select_month
  • select_year
  • select_hour
  • select_minute
  • select_second
  • select_datetime
  • select_time
  • date_select
  • time_select
  • datetime_select

The were missing tests for with_css_classes, I'd tried to cover.

🍀

@neumayr
Copy link
Contributor Author

neumayr commented Mar 17, 2016

r? @rafaelfranca

@neumayr neumayr force-pushed the date_select_helper_with_css_classes_accept_hash branch 3 times, most recently from 0d6e85e to 4d53745 Compare April 5, 2016 10:03
`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 rails#7975.
@neumayr neumayr force-pushed the date_select_helper_with_css_classes_accept_hash branch from 4d53745 to d7b6054 Compare April 5, 2016 11:37
@rafaelfranca rafaelfranca merged commit d7b6054 into rails:master Apr 20, 2016
rafaelfranca added a commit that referenced this pull request Apr 20, 2016
…asses_accept_hash

date_select helper with_css_classes option also accept a hash
y-yagi added a commit to y-yagi/rails that referenced this pull request Apr 30, 2016
@neumayr neumayr deleted the date_select_helper_with_css_classes_accept_hash branch October 18, 2017 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants