Skip to content

Commit

Permalink
Add docs input RadioList::class. (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Feb 16, 2024
1 parent 79af382 commit f28220c
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 68 deletions.
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ This is particularly useful for creating forms and other interactive elements on
- [datetime](/docs/form/input/Datetime.md)
- [datetime-local](/docs/form/input/DatetimeLocal.md)
- [radio](/docs/form/input/Radio.md)
- [radio-list](/docs/form/input/RadioList.md)
- [text](/docs/form/input/Text.md)
- [time](/docs/form/input/Time.md)
- [url](/docs/form/input/Url.md)
Expand Down
202 changes: 202 additions & 0 deletions docs/form/input/RadioList.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
# RadioList

Generates a list of radio buttons.

A radio button is a graphical control element that allows the user to choose only one of a predefined set of mutually
exclusive options.

## Basic Usage

Instantiate the `RadioList` class using `RadioList::widget()`.

```php
$radioList = RadioList::widget();
```

## Generate field id and name

The `generateField` method is used to generate the field id and name for the `HTML` output.

Allowed arguments are:

- `modelName` - The name of the model.
- `fieldName` - The name of the field.
- `arrayable` - Whether the field is an array. For default, it is `false`.

```php
// generate field id and name
$radioList->generateField('model', 'field');
```

## Adding items

Use the `items` method to add items to the radio list.

```php
$radioList->items(
Radio::widget()->labelContent('Female')->value('1'),
Radio::widget()->labelContent('Male')->value('2'),
);
```

## Setting Attributes

Use the provided methods to set specific attributes for the a element.

```php
// setting class attribute
$radioList->class('container');
```

Or, use the `attributes` method to set multiple attributes at once.

```php
$radioList->attributes(['class' => 'container', 'style' => 'background-color: #eee;']);
```

## Adding checked

if you want to include the `checked` attribute, use the `checked` method.

```php
$radioList->checked(true);
```

Or add value for matching with the `radio` value attribute.

```php
$radioList->checked('MyValue')->value('MyValue');
```

## Adding label

if you want to include a label, use the `labelContent` method.

```php
$radioList->labelContent('MyLabel');
```

## Adding enclosed by label

if you want to include the `radio` enclosed by the `label` element, use the `enclosedByLabel` method.

```php
$radioList->enclosedByLabel(true);
```

## Adding hidden input

if you want to include a hidden input, use the `uncheckValue` method.

```php
$radioList->uncheckValue('MyValue');
```

## Rendering

Generate the `HTML` output using the `render` method, for simple instantiation.

```php
$html = $radioList->render();
```

Or, use the magic `__toString` method.

```php
$html = (string) $radioList;
```

## Common use cases

Below are examples of common use cases:

```php
// adding multiple attributes
$radioList->class('external')->value('Myvalue');
```

Explore additional methods for setting various attributes such as `lang`, `name`, `style`, `title`, etc.

## Template

The `template` method allows you to customize the `HTML` output of the a element.

The following template tags are available:

| Tag | Description |
| --------- | ------------------ |
| `{label}` | The label element. |
| `{tag}` | The a element. |

```php
// using a custom template
$radioList->template('<div>{tag}</div>');
```

## Attributes

Refer to the [Attribute Tests](https://github.com/php-forge/html/blob/main/tests/Input/RadioList/AttributeTest.php) for
comprehensive examples.

The following methods are available for setting attributes:

| Method | Description |
| ------------------ | ----------------------------------------------------------------------------------------------- |
| `ariaDescribedBy()`| Set the `aria-describedby` attribute. |
| `ariaLabel()` | Set the `aria-label` attribute. |
| `attributes()` | Set multiple `attributes` at once. |
| `autofocus()` | Set the `autofocus` attribute. |
| `checked()` | Set the `checked` attribute. |
| `class()` | Set the `class` attribute. |
| `id()` | Set the `id` attribute. |
| `name()` | Set the `name` attribute. |
| `style()` | Set the `style` attribute. |
| `tabIndex()` | Set the `tabindex` attribute. |

## Custom methods

Refer to the [Custom Methods Tests](https://github.com/php-forge/html/blob/main/tests/Input/RadioList/CustomMethodTest.php)
for comprehensive examples.

The following methods are available for customizing the `HTML` output:

| Method | Description |
| ---------------------------- | ------------------------------------------------------------------------------------- |
| `container()` | Set enabled or disabled for the `container` element. |
| `containerAttributes()` | Set `attributes` for the `container` element. |
| `containerClass()` | Set the `class` attribute for the `container` element. |
| `containerTag()` | Set the `tag` for the `container` element. |
| `generateField()` | Generate the field id and name for the `HTML` output. |
| `items()` | Set the `items` for the `HTML` output. |
| `render()` | Generates the `HTML` output. |
| `separator()` | Set the `separator` for the `HTML` output. |
| `template()` | Set the template for the `HTML` output. |
| `uncheckAttributes()` | Set the attributes for the hidden input tag. |
| `uncheckClass()` | Set the `class` attribute for the hidden input tag. |
| `uncheckValue()` | Set the `value` attribute for the hidden input tag. |
| `widget()` | Instantiates the `RadioList::class`. |

## Label methods

Refer to the [Label Tests](https://github.com/php-forge/html/blob/main/tests/Input/RadioList/LabelTest.php) for
comprehensive examples.

The following methods are available for customizing the `HTML` output:

| Method | Description |
| ------------------ | ----------------------------------------------------------------------------------------------- |
| `enclosedByLabel()`| Set enabled or disabled for the `enclosed-by-label` element. |
| `labelAttributes()`| Set `attributes` for the `label` element. |
| `labelClass()` | Set the `class` attribute for the `label` element. |
| `labelContent()` | Set the `content` within the `label` element. |
| `labelFor()` | Set the `for` attribute for the `label` element. |
| `notLabel()` | Set disabled for the `label` element.

## Validate methods

Refer to the [Validate Tests](https://github.com/php-forge/html/blob/main/tests/Input/RadioList/ValidateTest.php) for
comprehensive examples.

| Method | Description |
| -------------- | --------------------------------------------------------------------------------------------------- |
| `required()` | Set the `required` attribute. |
68 changes: 0 additions & 68 deletions tests/Input/RadioList/AttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,74 +272,6 @@ public function testName(): void
);
}

public function testRender(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div id="radiolist-65858c272ea89">
<input id="radio-6599b6a33dd96" name="radioform[text]" type="radio" value="red">
<label for="radio-6599b6a33dd96">Red</label>
<input id="radio-6599b6a33dd97" name="radioform[text]" type="radio" value="blue">
<label for="radio-6599b6a33dd97">Blue</label>
</div>
HTML,
RadioList::widget()
->id('radiolist-65858c272ea89')
->items(
Radio::widget()->id('radio-6599b6a33dd96')->labelContent('Red')->value('red'),
Radio::widget()->id('radio-6599b6a33dd97')->labelContent('Blue')->value('blue'),
)
->name('radioform[text]')
->render()
);
}

public function testRequired(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div id="radiolist-65858c272ea89">
<input id="radio-6599b6a33dd96" name="radioform[text]" type="radio" value="red" required>
<label for="radio-6599b6a33dd96">Red</label>
<input id="radio-6599b6a33dd97" name="radioform[text]" type="radio" value="blue" required>
<label for="radio-6599b6a33dd97">Blue</label>
</div>
HTML,
RadioList::widget()
->id('radiolist-65858c272ea89')
->items(
Radio::widget()->id('radio-6599b6a33dd96')->labelContent('Red')->value('red'),
Radio::widget()->id('radio-6599b6a33dd97')->labelContent('Blue')->value('blue'),
)
->name('radioform[text]')
->required()
->render()
);
}

public function testSeparator(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div id="radiolist-65858c272ea89">
<input id="radio-6599b6a33dd96" name="radioform[text]" type="radio" value="red">
<label for="radio-6599b6a33dd96">Red</label>
<input id="radio-6599b6a33dd97" name="radioform[text]" type="radio" value="blue">
<label for="radio-6599b6a33dd97">Blue</label>
</div>
HTML,
RadioList::widget()
->id('radiolist-65858c272ea89')
->items(
Radio::widget()->id('radio-6599b6a33dd96')->labelContent('Red')->value('red'),
Radio::widget()->id('radio-6599b6a33dd97')->labelContent('Blue')->value('blue'),
)
->name('radioform[text]')
->separator(PHP_EOL)
->render()
);
}

public function testTabindex(): void
{
Assert::equalsWithoutLE(
Expand Down
45 changes: 45 additions & 0 deletions tests/Input/RadioList/CustomMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,51 @@ public function testGenerateField(): void
);
}

public function testRender(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div id="radiolist-65858c272ea89">
<input id="radio-6599b6a33dd96" name="radioform[text]" type="radio" value="red">
<label for="radio-6599b6a33dd96">Red</label>
<input id="radio-6599b6a33dd97" name="radioform[text]" type="radio" value="blue">
<label for="radio-6599b6a33dd97">Blue</label>
</div>
HTML,
RadioList::widget()
->id('radiolist-65858c272ea89')
->items(
Radio::widget()->id('radio-6599b6a33dd96')->labelContent('Red')->value('red'),
Radio::widget()->id('radio-6599b6a33dd97')->labelContent('Blue')->value('blue'),
)
->name('radioform[text]')
->render()
);
}

public function testSeparator(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div id="radiolist-65858c272ea89">
<input id="radio-6599b6a33dd96" name="radioform[text]" type="radio" value="red">
<label for="radio-6599b6a33dd96">Red</label>
<input id="radio-6599b6a33dd97" name="radioform[text]" type="radio" value="blue">
<label for="radio-6599b6a33dd97">Blue</label>
</div>
HTML,
RadioList::widget()
->id('radiolist-65858c272ea89')
->items(
Radio::widget()->id('radio-6599b6a33dd96')->labelContent('Red')->value('red'),
Radio::widget()->id('radio-6599b6a33dd97')->labelContent('Blue')->value('blue'),
)
->name('radioform[text]')
->separator(PHP_EOL)
->render()
);
}

public function testTemplate(): void
{
Assert::equalsWithoutLE(
Expand Down
39 changes: 39 additions & 0 deletions tests/Input/RadioList/ValidateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Tests\Input\RadioList;

use PHPForge\Html\Input\Radio;
use PHPForge\Html\Input\RadioList;
use PHPForge\Support\Assert;
use PHPUnit\Framework\TestCase;

/**
* @psalm-suppress PropertyNotSetInConstructor
*/
final class ValidateTest extends TestCase
{
public function testRequired(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div id="radiolist-65858c272ea89">
<input id="radio-6599b6a33dd96" name="radioform[text]" type="radio" value="red" required>
<label for="radio-6599b6a33dd96">Red</label>
<input id="radio-6599b6a33dd97" name="radioform[text]" type="radio" value="blue" required>
<label for="radio-6599b6a33dd97">Blue</label>
</div>
HTML,
RadioList::widget()
->id('radiolist-65858c272ea89')
->items(
Radio::widget()->id('radio-6599b6a33dd96')->labelContent('Red')->value('red'),
Radio::widget()->id('radio-6599b6a33dd97')->labelContent('Blue')->value('blue'),
)
->name('radioform[text]')
->required()
->render()
);
}
}

0 comments on commit f28220c

Please sign in to comment.