-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e74b93e
commit 5c20ba0
Showing
11 changed files
with
797 additions
and
412 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Bootstrap5; | ||
|
||
use Stringable; | ||
use Yiisoft\Arrays\ArrayHelper; | ||
use Yiisoft\Html\Html; | ||
use Yiisoft\Html\Tag\Base\Tag; | ||
|
||
abstract class AbstractToggleWidget extends Widget | ||
{ | ||
protected array $toggleOptions = []; | ||
protected string|Stringable $toggleLabel = ''; | ||
protected bool $renderToggle = true; | ||
|
||
abstract protected function bsToggle(): string; | ||
|
||
public function withToggleOptions(array $options): static | ||
{ | ||
$new = clone $this; | ||
$new->toggleOptions = $options; | ||
|
||
return $new; | ||
} | ||
|
||
public function withToggleLabel(string|Stringable $label): static | ||
{ | ||
$new = clone $this; | ||
$new->toggleLabel = $label; | ||
|
||
return $new; | ||
} | ||
|
||
public function withToggle(bool $value): static | ||
{ | ||
if ($this->renderToggle === $value) { | ||
return $this; | ||
} | ||
|
||
$new = clone $this; | ||
$new->renderToggle = $value; | ||
|
||
return $new; | ||
} | ||
|
||
protected function prepareToggleOptions(): array | ||
{ | ||
$options = $this->toggleOptions; | ||
$tagName = ArrayHelper::remove($options, 'tag', 'button'); | ||
$encode = ArrayHelper::remove($options, 'encode', true); | ||
$options['data-bs-toggle'] = $this->bsToggle(); | ||
|
||
if (!isset($options['aria-controls']) && !isset($options['aria']['controls'])) { | ||
$options['aria-controls'] = $this->getId(); | ||
} | ||
|
||
if ($tagName !== 'button') { | ||
$options['role'] = 'button'; | ||
} elseif (!isset($options['type'])) { | ||
$options['type'] = 'button'; | ||
} | ||
|
||
if ($tagName === 'a' && !isset($options['href'])) { | ||
$options['href'] = '#' . $this->getId(); | ||
} elseif (!isset($options['data-bs-target']) && !isset($options['data']['bs-target'])) { | ||
$options['data-bs-target'] = '#' . $this->getId(); | ||
} | ||
|
||
return [$tagName, $options, $encode]; | ||
} | ||
|
||
public function renderToggle(): Tag | ||
{ | ||
list($tagName, $options, $encode) = $this->prepareToggleOptions(); | ||
|
||
return Html::tag($tagName, $this->toggleLabel, $options) | ||
->encode($encode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.