generated from yii-tools/template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RadioList and CheckboxList classes. (#172)
- Loading branch information
1 parent
fc90d04
commit 0c5272b
Showing
18 changed files
with
929 additions
and
10 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 |
---|---|---|
|
@@ -9,8 +9,6 @@ | |
*/ | ||
trait HasSeparator | ||
{ | ||
protected string $separator = PHP_EOL; | ||
|
||
/** | ||
* Set the separator. | ||
* | ||
|
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
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
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
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,114 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PHPForge\Html\Input\Base; | ||
|
||
use InvalidArgumentException; | ||
use PHPForge\Html\Attribute; | ||
use PHPForge\Html\Input\Checkbox; | ||
use PHPForge\Html\Input\InputInterface; | ||
use PHPForge\Html\Input\LabelInterface; | ||
use PHPForge\Html\Input\Radio; | ||
use PHPForge\Html\Label; | ||
use PHPForge\Html\Tag; | ||
use PHPForge\Widget\Element; | ||
|
||
abstract class AbstractChoiceList extends Element implements InputInterface, LabelInterface | ||
{ | ||
use Attribute\Aria\HasAriaDescribedBy; | ||
use Attribute\Aria\HasAriaLabel; | ||
use Attribute\CanBeAutofocus; | ||
use Attribute\Custom\HasAttributes; | ||
use Attribute\Custom\HasContainer; | ||
use Attribute\Custom\HasLabel; | ||
use Attribute\Custom\HasSeparator; | ||
use Attribute\HasClass; | ||
use Attribute\HasId; | ||
use Attribute\HasTabindex; | ||
use Attribute\Input\CanBeChecked; | ||
use Attribute\Input\CanBeRequired; | ||
use Attribute\Input\HasName; | ||
use Attribute\Input\HasValue; | ||
|
||
protected array $attributes = []; | ||
protected bool $container = true; | ||
protected string $containerTag = 'div'; | ||
/** | ||
* @psalm-var Checkbox[]|Radio[] $items | ||
*/ | ||
protected array $items = []; | ||
protected string $separator = ''; | ||
|
||
public function items(Checkbox|Radio ...$items): static | ||
{ | ||
$new = clone $this; | ||
$new->items = $items; | ||
|
||
return $new; | ||
} | ||
|
||
protected function run(): string | ||
{ | ||
$attributes = $this->attributes; | ||
$containerAttributes = $this->containerAttributes; | ||
$id = $this->generateId('choice-'); | ||
$items = $this->items; | ||
$labelTag = ''; | ||
$listItems = []; | ||
$value = $this->getValue(); | ||
|
||
/** | ||
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.checkbox.html#input.checkbox.attrs.value | ||
*/ | ||
if (is_iterable($value) || is_object($value)) { | ||
throw new InvalidArgumentException( | ||
sprintf('%s::class widget must be a scalar value.', static::class) | ||
); | ||
} | ||
|
||
unset($attributes['value']); | ||
|
||
if (array_key_exists('autofocus', $attributes) && is_bool($attributes['autofocus'])) { | ||
$containerAttributes['autofocus'] = $attributes['autofocus']; | ||
|
||
unset($attributes['autofocus']); | ||
} | ||
|
||
if (array_key_exists('tabindex', $attributes) && is_int($attributes['tabindex'])) { | ||
$containerAttributes['tabindex'] = $attributes['tabindex']; | ||
|
||
unset($attributes['tabindex']); | ||
} | ||
|
||
foreach ($items as $item) { | ||
$listItems[] = $item | ||
->attributes($attributes) | ||
->checked($value === $item->getValue()) | ||
->id(null) | ||
->separator($this->separator) | ||
->labelFor(null); | ||
} | ||
|
||
$listTagItems = implode(PHP_EOL, $listItems); | ||
|
||
if ($this->labelContent !== '') { | ||
$labelTag = Label::widget() | ||
->attributes($this->labelAttributes) | ||
->content($this->labelContent) | ||
->for($id) | ||
->render() . PHP_EOL; | ||
} | ||
|
||
return match ($this->container) { | ||
true => $labelTag . | ||
Tag::widget() | ||
->attributes($containerAttributes) | ||
->content($listTagItems) | ||
->id($id) | ||
->tagName($this->containerTag) | ||
->render(), | ||
default => $listTagItems, | ||
}; | ||
} | ||
} |
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
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
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
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
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PHPForge\Html\Input; | ||
|
||
/** | ||
* Provide methods for handling HTML checked value-related attributes and properties. | ||
*/ | ||
interface CheckedValueInterface | ||
{ | ||
/** | ||
* Set the value that determines when the checkbox should be checked. | ||
* | ||
* @param mixed $checkedValue The value that, when matched with the checkbox value, will mark the checkbox as checked. | ||
* | ||
* @return static Returns a new instance of the class with the updated checked value. | ||
*/ | ||
public function checkedValue(mixed $checkedValue): static; | ||
} |
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PHPForge\Html\Input; | ||
|
||
/* | ||
* Generates a list of checkboxes. | ||
* | ||
* A checkbox or radio list allows many selections. | ||
*/ | ||
final class ChoiceList extends Base\AbstractChoiceList | ||
{ | ||
} |
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
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
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.