Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Oct 20, 2022
2 parents 84c9cea + 918048f commit 35b81a8
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 4 deletions.
5 changes: 5 additions & 0 deletions app/app/Http/Controllers/FormComponentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public function vmodel()
return view('form.components.vmodel', ['countries' => $this->countries()]);
}

public function checkbox()
{
return view('form.components.checkbox');
}

public function checkboxes()
{
return view('form.components.checkboxes', [
Expand Down
35 changes: 35 additions & 0 deletions app/resources/views/form/components/checkbox.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@extends('layout')

@section('content')

FormCheckbox

<div class="max-w-sm mx-auto px-4">
<x-splade-form
:default="[
'check_0' => 0,
'check_1' => 1,
'check_false' => false,
'check_true' => true,
'check_0_diff_false' => 'no',
'check_1_diff_false' => 1,
'check_false_diff_false' => 'no',
'check_true_diff_false' => true,
]"
>
<x-splade-checkbox value="1" name="check_0" label="Checkbox 0" />
<x-splade-checkbox value="1" name="check_1" label="Checkbox 1" />
<x-splade-checkbox name="check_false" label="Checkbox False" />
<x-splade-checkbox name="check_true" label="Checkbox True" />

<x-splade-checkbox false-value="no" value="1" name="check_0_diff_false" label="Checkbox 0" />
<x-splade-checkbox false-value="no" value="1" name="check_1_diff_false" label="Checkbox 1" />
<x-splade-checkbox false-value="no" name="check_false_diff_false" label="Checkbox False" />
<x-splade-checkbox false-value="no" name="check_true_diff_false" label="Checkbox True" />

<div dusk="all">@{{ form.$all }}</div>
</x-splade-form>
</div>

@endsection
1 change: 1 addition & 0 deletions app/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@

Route::get('form/components/simple', [FormComponentsController::class, 'simple'])->name('form.components.simple');
Route::get('form/components/vmodel', [FormComponentsController::class, 'vmodel'])->name('form.components.vmodel');
Route::get('form/components/checkbox', [FormComponentsController::class, 'checkbox'])->name('form.components.checkbox');
Route::get('form/components/checkboxes', [FormComponentsController::class, 'checkboxes'])->name('form.components.checkboxes');
Route::post('form/components/checkboxes', [FormComponentsController::class, 'submitCheckboxes'])->name('form.components.submitCheckboxes');
Route::get('form/components/radios', [FormComponentsController::class, 'radios'])->name('form.components.radios');
Expand Down
43 changes: 43 additions & 0 deletions app/tests/Browser/Form/ComponentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,47 @@ public function it_can_render_multiple_radios_with_an_options_array()
->assertRouteIs('navigation.one');
});
}

/** @test */
public function it_can_change_the_value_of_a_checkbox_element()
{
$this->browse(function (Browser $browser) {
$browser->visit('form/components/checkbox')
->waitForText('FormCheckbox')
->assertNotChecked('check_0')
->assertChecked('check_1')
->assertNotChecked('check_false')
->assertChecked('check_true')
->assertNotChecked('check_0_diff_false')
->assertChecked('check_1_diff_false')
->assertNotChecked('check_false_diff_false')
->assertChecked('check_true_diff_false');

$checkboxes = [
'check_0',
'check_1',
'check_false',
'check_true',
'check_0_diff_false',
'check_1_diff_false',
'check_false_diff_false',
'check_true_diff_false',
];

// uncheck everything
foreach ($checkboxes as $field) {
$browser->check($field);
$browser->uncheck($field);
}

$browser->assertSeeIn('@all', '{ "check_0": false, "check_1": false, "check_false": false, "check_true": false, "check_0_diff_false": "no", "check_1_diff_false": "no", "check_false_diff_false": "no", "check_true_diff_false": "no" }');

// check everything
foreach ($checkboxes as $field) {
$browser->check($field);
}

$browser->assertSeeIn('@all', '{ "check_0": "1", "check_1": "1", "check_false": true, "check_true": true, "check_0_diff_false": "1", "check_1_diff_false": "1", "check_false_diff_false": true, "check_true_diff_false": true }');
});
}
}
3 changes: 1 addition & 2 deletions resources/views/form/checkbox.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
'type' => 'checkbox',
'v-model' => $vueModel(),
'data-validation-key' => $validationKey(),
]) }}
/>
]) }} :true-value="@js($value)" :false-value="@js($falseValue)" />

@if(trim($slot))
<span class="ml-2">{{ $slot }}</span>
Expand Down
5 changes: 3 additions & 2 deletions src/Components/Form/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ class Checkbox extends Component
public function __construct(
public string $name = '',
public string $vModel = '',
public $value = 1,
public $value = true,
public string $label = '',
public string $validationKey = '',
public bool $showErrors = true,
public string $help = '',
public bool $relation = false
public bool $relation = false,
public $falseValue = false,
) {
Form::allowAttribute($name);

Expand Down

0 comments on commit 35b81a8

Please sign in to comment.