Switch Field(ui_switch_field)
UiBibz::Ui::Core::Forms::Choices::SwitchField
A switch has the markup of a custom checkbox but uses the .custom-switch class to render a toggle switch.
Usage
This component is an extension of component element. A Ui Bibz component consists of 3 arguments:
# by variable
ui_switch_field name, options, html_options
# or by block
ui_switch_field options, html_options do
name
end
Examples
Some examples explain how to use the options present in the component.Switch Field
ui_switch_field 'example_basic'
<div class="custom-control custom-switch">
<input type="hidden" name="example_basic" id="example_basic-hidden" value="0">
<input type="checkbox" name="example_basic" id="example_basic" value="1" class="custom-control-input">
<label class="custom-control-label" for="example_basic">example_basic</label>
</div>
Inline
ui_switch_field "example_inline", inline: true
ui_switch_field "example_inline_2", inline: true, checked: true
<div class="custom-control custom-switch custom-control-inline">
<input type="hidden" name="example_inline" id="example_inline-hidden" value="0">
<input type="checkbox" name="example_inline" id="example_inline" value="1" class="custom-control-input">
<label class="custom-control-label" for="example_inline">example_inline</label>
</div>
<div class="custom-control custom-switch custom-control-inline">
<input type="hidden" name="example_inline_2" id="example_inline_2-hidden" value="0">
<input type="checkbox" name="example_inline_2" id="example_inline_2" value="1" class="custom-control-input" checked="checked">
<label class="custom-control-label" for="example_inline_2">example_inline_2</label>
</div>
State
ui_switch_field "example_state", state: :active
ui_switch_field "example_state_2", state: :disabled
<div class="active custom-control custom-switch">
<input type="hidden" name="example_state" id="example_state-hidden" value="0">
<input type="checkbox" name="example_state" id="example_state" value="1" class="custom-control-input">
<label class="custom-control-label" for="example_state">example_state</label>
</div>
<div class="disabled custom-control custom-switch">
<input type="hidden" name="example_state_2" id="example_state_2-hidden" value="0">
<input type="checkbox" name="example_state_2" id="example_state_2" value="1" disabled="disabled" class="custom-control-input">
<label class="custom-control-label" for="example_state_2">example_state_2</label>
</div>
Checked
ui_switch_field "example_checked", checked: true
<div class="custom-control custom-switch"><input type="hidden" name="example_checked" id="example_checked-hidden" value="0">
<input type="checkbox" name="example_checked" id="example_checked" value="1" class="custom-control-input" checked="checked">
<label class="custom-control-label" for="example_checked">example_checked</label>
</div>
Simple form
Ui Bibz is compatible with simple form.
You can use defaults inputs of Simple Form and defaults inputs of Ui Bibz like ui_switch_field
. You can use Simple Form input options and Ui Bibz ui_switch_field options.
ui_bibz_form_for(@user) do |f|
...
f.input :active, as: :ui_switch_field
...
end