Skip to content

Commit e3482c7

Browse files
author
Daniel Wiser
committed
Update toggle with preset state
1 parent ac9009e commit e3482c7

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/Components/Form/Toggle.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,26 @@ class Toggle extends Component
1919
* @var string
2020
*/
2121
public $name;
22+
/**
23+
* Whether or not the toggle is checked.
24+
*
25+
* @var bool
26+
*/
27+
public $checked;
2228

2329
/**
2430
* Create a new component instance.
2531
*
2632
* @param string $name
27-
* @param string $checked
33+
* @param bool $checked
2834
*
2935
* @return void
3036
*/
31-
public function __construct($name = "toggle")
37+
public function __construct($name = "toggle", $checked = false)
3238
{
3339
$this->id = 'form-' . Str::kebab(class_basename(get_class($this))) . '-' . md5($name);
3440
$this->name = $name;
41+
$this->checked = $checked;
3542
}
3643

3744
/**

src/Views/form/toggle.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<label {{ $attributes->merge(['class' => 'form-check form-switch']) }}>
2-
<input name="{{ $name }}" type="checkbox" class="form-check-input">
2+
<input name="{{ $name }}" type="checkbox" class="form-check-input" {{ $checked ? 'checked' : old($name) }}>
33
<span class="form-check-label">{{ $slot }}</span>
44
@error($name)
55
<div class="form-check-description invalid-feedback">{{ $message }}</div>

0 commit comments

Comments
 (0)