Skip to content

Commit cd515d7

Browse files
Add prompt option to select fields in l_ui_form (#87)
1 parent a94c32e commit cd515d7

5 files changed

Lines changed: 30 additions & 4 deletions

File tree

.claude/skills/layered-ui-rails/references/HELPERS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ Field options:
226226
- `required` (Boolean, optional) - marks field as required; default false
227227
- `hint` (String, optional) - help text below the field
228228
- `collection` (Array, optional) - required for `:select` type; e.g. `[['Label', value], ...]`
229+
- `include_blank` (Boolean or String, optional) - for `:select` fields; defaults to `true`. Pass a string to use as the blank option's label, or `false` to omit it. Suppressed when `prompt:` is set
230+
- `prompt` (String, optional) - for `:select` fields; prompt text shown as the first option, only selectable when no value is set
229231
- `placeholder` (String, optional) - input placeholder text
230232

231233
```erb

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to this project will be documented in this file. This project follows [Semantic Versioning](https://semver.org/).
44

5+
## [Unreleased]
6+
7+
### Added
8+
9+
- `prompt:` field option on `:select` fields in `l_ui_form`, shown as the first option and only selectable when no value is set; suppresses the default blank option
10+
511
## [0.16.1] - 2026-05-16
612

713
### Fixed

app/helpers/layered/ui/form_helper.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def l_ui_normalise_field(record, config)
3535
label = config[:label] || attribute.to_s.humanize
3636

3737
extras = config.except(:attribute, :as, :label, :required, :hint,
38-
:collection, :placeholder)
38+
:collection, :placeholder, :prompt, :include_blank)
3939

4040
{
4141
attribute: attribute,
@@ -45,6 +45,8 @@ def l_ui_normalise_field(record, config)
4545
hint: config[:hint],
4646
collection: config[:collection],
4747
placeholder: config[:placeholder],
48+
prompt: config[:prompt],
49+
include_blank: config[:include_blank],
4850
extras: extras
4951
}
5052
end

app/views/layered/ui/managed_resource/_field_input.html.erb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@
2525
<%
2626
collection = config[:collection]
2727
choices = collection.respond_to?(:call) ? collection.call : collection
28-
include_blank = extras.delete(:include_blank)
29-
include_blank = true if include_blank.nil?
28+
include_blank = config[:include_blank]
29+
prompt = config[:prompt]
30+
include_blank = true if include_blank.nil? && prompt.nil?
31+
select_options = {}
32+
select_options[:include_blank] = include_blank unless include_blank.nil?
33+
select_options[:prompt] = prompt unless prompt.nil?
3034
%>
3135
<div class="l-ui-select-container">
32-
<%= form.select(attribute, choices, { include_blank: include_blank },
36+
<%= form.select(attribute, choices, select_options,
3337
class: field_class.call("l-ui-select"), **base_opts, **extras) %>
3438
</div>
3539
<% elsif config[:as] == :checkbox %>

test/dummy/app/views/pages/forms_helper.html.erb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
{ attribute: :title, as: :string, required: true, placeholder: "Enter a title" },
4646
{ attribute: :body, as: :text, hint: "A longer description" },
4747
{ attribute: :user_id, as: :select, label: "Author",
48+
prompt: "Choose an author",
4849
collection: @users.map { |u| [u.name, u.id] } },
4950
],
5051
url: "#") %>
@@ -65,6 +66,7 @@
6566
{ attribute: :body, as: :text,
6667
hint: "A longer description" },
6768
{ attribute: :user_id, as: :select, label: "Author",
69+
prompt: "Choose an author",
6870
collection: User.pluck(:name, :id) },
6971
],
7072
url: posts_path) %&gt;</code></pre>
@@ -162,6 +164,16 @@
162164
<td class="l-ui-table__cell">Array / Proc</td>
163165
<td class="l-ui-table__cell">Options for <code>:select</code> fields. Array of <code>[label, value]</code> pairs or a callable.</td>
164166
</tr>
167+
<tr>
168+
<th class="l-ui-table__cell l-ui-table__cell--primary" scope="row"><code>prompt:</code></th>
169+
<td class="l-ui-table__cell">String</td>
170+
<td class="l-ui-table__cell">Prompt text for <code>:select</code> fields (e.g. "Choose an author"). Shown as the first option; only selectable when no value is set. Setting <code>prompt:</code> suppresses the default blank option.</td>
171+
</tr>
172+
<tr>
173+
<th class="l-ui-table__cell l-ui-table__cell--primary" scope="row"><code>include_blank:</code></th>
174+
<td class="l-ui-table__cell">Boolean / String</td>
175+
<td class="l-ui-table__cell">For <code>:select</code> fields. Defaults to <code>true</code>. Pass a string to use it as the blank option's label, or <code>false</code> to omit the blank option entirely.</td>
176+
</tr>
165177
</tbody>
166178
</table>
167179
</div>

0 commit comments

Comments
 (0)