Skip to content

Commit

Permalink
More rework code.
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Dec 21, 2021
1 parent 123dd3b commit 1c5a165
Show file tree
Hide file tree
Showing 13 changed files with 880 additions and 8 deletions.
21 changes: 21 additions & 0 deletions src/Field.php
Expand Up @@ -222,6 +222,27 @@ public function radiolist(FormModelInterface $formModel, string $attribute, arra
return $new;
}

/**
* Renders a range widget.
*
* @param FormModelInterface $formModel The model object.
* @param string $attribute The attribute name or expression.
* @param array $config The config array definition for the factory widget.
* Available methods:
* [
* 'outputTag()' => ['p'],
* 'outputAttributes()' => [['class' => 'test-class']],
* ]
*
* @return static the field object itself.
*/
public function range(FormModelInterface $formModel, string $attribute, array $config = []): self
{
$new = clone $this;
$new->widget = Range::widget($config)->for($formModel, $attribute);
return $new;
}

/**
* Renders a reset button widget.
*
Expand Down
126 changes: 126 additions & 0 deletions src/Range.php
@@ -0,0 +1,126 @@
<?php

declare(strict_types=1);

namespace Yii\Extension\Simple\Forms;

use InvalidArgumentException;
use Yii\Extension\Simple\Forms\Attribute\InputAttributes;
use Yii\Extension\Simple\Forms\Interface\NumberInterface;
use Yiisoft\Html\Html;
use Yiisoft\Html\Tag\CustomTag;
use Yiisoft\Html\Tag\Input;

/**
* The input element with a type attribute whose value is "range" represents an imprecise control for setting the
* element’s value to a string representing a number.
*
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.range.html
*/
final class Range extends InputAttributes implements NumberInterface
{
private array $outputAttributes = [];
private string $outputTag = 'output';

/**
* The expected upper bound for the element’s value.
*
* @param int $value
*
* @return static
*
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.range.html#input.range.attrs.max
*/
public function max(int $value): self
{
$new = clone $this;
$new->attributes['max'] = $value;
return $new;
}

/**
* The expected lower bound for the element’s value.
*
* @param int $value
*
* @return static
*
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.range.html#input.range.attrs.min
*/
public function min(int $value): self
{
$new = clone $this;
$new->attributes['min'] = $value;
return $new;
}

/**
* The HTML attributes for output tag. The following special options are recognized.
*
* @param array $value
*
* @return static
*
* See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
*/
public function outputAttributes(array $value): self
{
$new = clone $this;
$new->outputAttributes = $value;
return $new;
}

/**
* The tag name of the output tag.
*
* Empty to render error messages without container {@see Html::tag()}.
*
* @param string $value
*
* @return static
*/
public function outputTag(string $value): self
{
$new = clone $this;
$new->outputTag = $value;
return $new;
}

/**
* @return string the generated input tag.
*/
protected function run(): string
{
$attributes = $this->build($this->attributes);
$outputAttributes = $this->outputAttributes;

if (empty($this->outputTag)) {
throw new InvalidArgumentException('The output tag name it cannot be empty value.');
}

/** @link https://www.w3.org/TR/2012/WD-html-markup-20120329/input.range.html#input.range.attrs.value */
$value = $attributes['value'] ?? $this->getAttributeValue();
unset($attributes['value']);

if (!is_numeric($value) && null !== $value) {
throw new InvalidArgumentException('Range widget must be a numeric or null value.');
}

$nameOutput = Html::generateId();
/** @var string|null */
$outputAttributes['for'] = $attributes['name'] ?? null;
$outputAttributes['name'] = $nameOutput;
$attributes['oninput'] = "$nameOutput.value=this.value";

return
Input::tag()
->type('range')
->attributes($attributes)
->value($value > 0 ? $value : 0)->render() . PHP_EOL .
CustomTag::name($this->outputTag)
->attributes($outputAttributes)
->content($value > 0 ? (string)$value : '0')
->id($nameOutput)
->render();
}
}
2 changes: 1 addition & 1 deletion tests/CheckboxListTest.php
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Yiisoft\Form\Tests\Widget;
namespace Yii\Extension\Simple\Forms\Tests;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
Expand Down
2 changes: 1 addition & 1 deletion tests/CheckboxTest.php
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Yiisoft\Form\Tests\Widget;
namespace Yii\Extension\Simple\Forms\Tests;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
Expand Down
2 changes: 1 addition & 1 deletion tests/Field/FieldCheckBoxListTest.php
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Yiisoft\Form\Tests\Widget;
namespace Yii\Extension\Simple\Forms\Tests;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
Expand Down
2 changes: 1 addition & 1 deletion tests/Field/FieldRadioListTest.php
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Yiisoft\Form\Tests\Widget;
namespace Yii\Extension\Simple\Forms\Tests;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
Expand Down

0 comments on commit 1c5a165

Please sign in to comment.