Skip to content

Commit

Permalink
Enable readonly state for select fields
Browse files Browse the repository at this point in the history
Simulates disabled state but keeps input values in the form
  • Loading branch information
annda committed Dec 5, 2024
1 parent f3ffce4 commit 6a893cb
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion doc/formelements.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ They are required by default.

Options:
* `validation` _(optional)_ - used to apply [validation](validation.md)
* `readonly` _(optional)_ - Disables user input. Available only for fields where users type in values. Useful when values are pre-filled from `values.yaml`
* `readonly` _(optional)_ - Disables user input but keeps any pre-filled values intact. Fields where users type in values get an actual HTML attribute `readonly`. Select fields like dropdowns and radios, where this property is not allowed, are visually toned down and are non-interactive. Useful when values are pre-filled from `values.yaml`

```yaml
<id>:
Expand Down
13 changes: 12 additions & 1 deletion public/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,23 @@ div.label-smaller {
}

/**
* Readonly field looking more a like disabled one
* Readonly field looking more like disabled ones,
* simulate readonly look for inputs that have no HTML readonly attribute
*/
input[readonly] {
cursor: not-allowed;
}

select.readonly-select {
pointer-events:none;
background: var(--bulma-input-disabled-placeholder-color);
}

.readonly-choices {
pointer-events:none;
opacity: 0.7;
}

/**
** Bulma overrides
*/
Expand Down
4 changes: 2 additions & 2 deletions view/checklist.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
<div class="control">
<div class="grouped-form-elements" id="{{ id }}">
{%- for choice in choices -%}
<label class="checkbox {{ labelsmall is not empty ? 'label-smaller' }}">
<label class="checkbox {{ labelsmall is not empty ? 'label-smaller' }}{% if is_readonly%} readonly-choices{% endif %}">
<input
type="checkbox"
id="{{ id~'['~loop.index0~']' }}"
name="{{ id~'['~loop.index0~']' }}"
value="{{ choice }}"
{{ choice in value ? 'checked' }}
class="checkbox-input form-input" />
class="checkbox-input form-input{% if is_readonly%} readonly-choices{% endif %}" />
{{- transformed_choices[loop.index0] | raw -}}
</label>
{%- if alignment is defined and alignment == 'vertical' -%}
Expand Down
2 changes: 1 addition & 1 deletion view/dropdown.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<select
id="{{ id }}"
name="{{ id }}[]"
class="form-input"
class="form-input{% if is_readonly%} readonly-select{% endif %}"
{{ size is defined ? 'size='~size }}
{{ multiselect is defined and multiselect ? 'multiple' }}>

Expand Down
4 changes: 2 additions & 2 deletions view/radioset.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
<div class="control">
<div class="grouped-form-elements" id="{{ id }}">
{%- for choice in choices -%}
<label class="radio {{ labelsmall is not empty ? 'label-smaller' }}">
<label class="radio {{ labelsmall is not empty ? 'label-smaller' }}{% if is_readonly%} readonly-choices{% endif %}">
<input
type="radio"
id="{{ id~'['~loop.index0~']' }}"
name="{{ id }}"
class="form-input"
class="form-input{% if is_readonly%} readonly-choices{% endif %}"
value="{{ choice }}"
{{ value == choice ? 'checked' }} />
{{ choice }}
Expand Down

0 comments on commit 6a893cb

Please sign in to comment.