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

Improve Input Switch component #1317

Merged
merged 4 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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