From 9efa375db36d73a2163f1203f28cbf0cc3d190b2 Mon Sep 17 00:00:00 2001 From: Diego Smania Date: Sat, 2 Nov 2024 17:45:54 -0300 Subject: [PATCH 1/4] [resources/components]: Input switch value attr may now be customized, using 'true' as default --- resources/views/components/form/input-switch.blade.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/views/components/form/input-switch.blade.php b/resources/views/components/form/input-switch.blade.php index 974647dc..968c11ad 100644 --- a/resources/views/components/form/input-switch.blade.php +++ b/resources/views/components/form/input-switch.blade.php @@ -9,8 +9,8 @@ @section('input_group_item') {{-- Input Switch --}} - merge(['class' => $makeItemClass()]) }}> + merge(['class' => $makeItemClass(), 'value' => 'true']) }}> @overwrite From 416ee43f95a848cd4642e34c4d0f01c8650ef559 Mon Sep 17 00:00:00 2001 From: Diego Smania Date: Thu, 7 Nov 2024 17:55:56 -0300 Subject: [PATCH 2/4] [src/Components]: Add new is-checked attribute to input switch --- src/View/Components/Form/InputSwitch.php | 8 +++++- tests/Components/FormComponentsTest.php | 36 ++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/View/Components/Form/InputSwitch.php b/src/View/Components/Form/InputSwitch.php index 3db1fe73..c960bfbf 100644 --- a/src/View/Components/Form/InputSwitch.php +++ b/src/View/Components/Form/InputSwitch.php @@ -24,7 +24,8 @@ 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, @@ -32,6 +33,11 @@ public function __construct( ); $this->config = is_array($config) ? $config : []; + + if (isset($isChecked)) { + $this->config['state'] = ! empty($isChecked); + } + $this->enableOldSupport = isset($enableOldSupport); } diff --git a/tests/Components/FormComponentsTest.php b/tests/Components/FormComponentsTest.php index c246c3ea..9ec6a321 100644 --- a/tests/Components/FormComponentsTest.php +++ b/tests/Components/FormComponentsTest.php @@ -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' @@ -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'); From 2a82e5efbfd40e6d9e784bf471aaaa4a755229c1 Mon Sep 17 00:00:00 2001 From: Diego Smania Date: Thu, 7 Nov 2024 17:57:35 -0300 Subject: [PATCH 3/4] [test]: Fix code style --- tests/Components/FormComponentsTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Components/FormComponentsTest.php b/tests/Components/FormComponentsTest.php index 9ec6a321..429ac7de 100644 --- a/tests/Components/FormComponentsTest.php +++ b/tests/Components/FormComponentsTest.php @@ -420,7 +420,7 @@ public function testInputSwitchComponentCheckedState() // Test the state property is true when is-checked attribute has a // truthy value. - foreach([true, 1, 'true'] as $v) { + foreach ([true, 1, 'true'] as $v) { $component = new Components\Form\InputSwitch( 'name', null, null, null, null, null, null, null, null, null, $v ); @@ -431,7 +431,7 @@ public function testInputSwitchComponentCheckedState() // Test the state property is false when is-checked attribute has a // falsy value. - foreach([false, 0, ''] as $v) { + foreach ([false, 0, ''] as $v) { $component = new Components\Form\InputSwitch( 'name', null, null, null, null, null, null, null, null, null, $v ); From 42fb232627b1083b4cb11d6ba623bb24bb6d13b4 Mon Sep 17 00:00:00 2001 From: Diego Smania Date: Fri, 8 Nov 2024 12:16:36 -0300 Subject: [PATCH 4/4] [resources/views]: Add workaround to ensure correct input-switch setup on initialization --- resources/views/components/form/input-switch.blade.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/resources/views/components/form/input-switch.blade.php b/resources/views/components/form/input-switch.blade.php index 968c11ad..6c9c3d38 100644 --- a/resources/views/components/form/input-switch.blade.php +++ b/resources/views/components/form/input-switch.blade.php @@ -20,7 +20,13 @@