Skip to content

Commit

Permalink
Improve Input Switch component (#1317)
Browse files Browse the repository at this point in the history
* [resources/components]: Input switch value attr may now be customized, using 'true' as default

* [src/Components]: Add new is-checked attribute to input switch

* [test]: Fix code style

* [resources/views]: Add workaround to ensure correct input-switch setup on initialization
  • Loading branch information
dfsmania authored Nov 9, 2024
1 parent fbb7064 commit dfd6b46
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
12 changes: 9 additions & 3 deletions resources/views/components/form/input-switch.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
@section('input_group_item')

{{-- Input Switch --}}
<input type="checkbox" id="{{ $id }}" name="{{ $name }}" value="true"
{{ $attributes->merge(['class' => $makeItemClass()]) }}>
<input type="checkbox" id="{{ $id }}" name="{{ $name }}"
{{ $attributes->merge(['class' => $makeItemClass(), 'value' => 'true']) }}>

@overwrite

Expand All @@ -20,7 +20,13 @@
<script>
$(() => {
$('#{{ $id }}').bootstrapSwitch( @json($config) );
let usrCfg = @json($config);
$('#{{ $id }}').bootstrapSwitch(usrCfg);
// Workaround to ensure correct state setup on initialization.
$('#{{ $id }}').bootstrapSwitch('state', usrCfg.state ?? false);
// Add support to auto select the previous submitted value in case of
// validation errors.
Expand Down
8 changes: 7 additions & 1 deletion src/View/Components/Form/InputSwitch.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,20 @@ class InputSwitch extends InputGroupComponent
public function __construct(
$name, $id = null, $label = null, $igroupSize = null, $labelClass = null,
$fgroupClass = null, $igroupClass = null, $disableFeedback = null,
$errorKey = null, $config = [], $enableOldSupport = null
$errorKey = null, $config = [], $isChecked = null,
$enableOldSupport = null
) {
parent::__construct(
$name, $id, $label, $igroupSize, $labelClass, $fgroupClass,
$igroupClass, $disableFeedback, $errorKey
);

$this->config = is_array($config) ? $config : [];

if (isset($isChecked)) {
$this->config['state'] = ! empty($isChecked);
}

$this->enableOldSupport = isset($enableOldSupport);
}

Expand Down
36 changes: 34 additions & 2 deletions tests/Components/FormComponentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,39 @@ public function testInputSliderComponentOldSupport()
|--------------------------------------------------------------------------
*/

public function testInputSwitchComponent()
public function testInputSwitchComponentCheckedState()
{
// Test the state property isn't defined when is-checked attribute
// isn't provided.

$component = new Components\Form\InputSwitch('name');

$this->assertArrayNotHasKey('state', $component->config);

// Test the state property is true when is-checked attribute has a
// truthy value.

foreach ([true, 1, 'true'] as $v) {
$component = new Components\Form\InputSwitch(
'name', null, null, null, null, null, null, null, null, null, $v
);

$this->assertTrue($component->config['state']);
}

// Test the state property is false when is-checked attribute has a
// falsy value.

foreach ([false, 0, ''] as $v) {
$component = new Components\Form\InputSwitch(
'name', null, null, null, null, null, null, null, null, null, $v
);

$this->assertFalse($component->config['state']);
}
}

public function testInputSwitchComponentWithErrorStyle()
{
$component = new Components\Form\InputSwitch(
'name', null, null, 'lg', null, null, 'igroup-class'
Expand Down Expand Up @@ -438,7 +470,7 @@ public function testInputSwitchComponentOldSupport()
// Test component with old support enabled.

$component = new Components\Form\InputSwitch(
'name', null, null, null, null, null, null, null, null, null, true
'name', null, null, null, null, null, null, null, null, null, null, true
);

$this->addInputOnCurrentRequest('name', 'foo');
Expand Down

0 comments on commit dfd6b46

Please sign in to comment.