-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from nathanjdunn/feature/number-type
Number Element
- Loading branch information
Showing
2 changed files
with
116 additions
and
0 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,52 @@ | ||
<?php | ||
|
||
namespace TypiCMS\Form\Elements; | ||
|
||
class Number extends Input | ||
{ | ||
protected $attributes = [ | ||
'type' => 'number', | ||
]; | ||
|
||
public function placeholder($placeholder) | ||
{ | ||
$this->setAttribute('placeholder', $placeholder); | ||
|
||
return $this; | ||
} | ||
|
||
public function max($max) | ||
{ | ||
$this->setAttribute('max', $max); | ||
|
||
return $this; | ||
} | ||
|
||
public function min($min) | ||
{ | ||
$this->setAttribute('min', $min); | ||
|
||
return $this; | ||
} | ||
|
||
public function step($step) | ||
{ | ||
$this->setAttribute('step', $step); | ||
|
||
return $this; | ||
} | ||
|
||
public function defaultValue($value) | ||
{ | ||
if (!$this->hasValue()) { | ||
$this->setValue($value); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
protected function hasValue() | ||
{ | ||
return isset($this->attributes['value']); | ||
} | ||
} |
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,64 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use TypiCMS\Form\Elements\Number; | ||
|
||
class NumberTest extends TestCase | ||
{ | ||
use TextSubclassContractTest; | ||
|
||
protected function newTestSubjectInstance($name) | ||
{ | ||
return new Number($name); | ||
} | ||
|
||
protected function getTestSubjectType() | ||
{ | ||
return 'number'; | ||
} | ||
|
||
public function testDefaultValue() | ||
{ | ||
$number = new Number('number'); | ||
$number->defaultValue(0); | ||
|
||
$expected = '<input type="number" name="number" value="0">'; | ||
$this->assertSame($expected, $number->render()); | ||
} | ||
|
||
public function testMinValue() | ||
{ | ||
$number = new Number('number'); | ||
$number->min(5); | ||
|
||
$expected = '<input type="number" name="number" min="5">'; | ||
$this->assertSame($expected, $number->render()); | ||
} | ||
|
||
public function testMaxValue() | ||
{ | ||
$number = new Number('number'); | ||
$number->max(10); | ||
|
||
$expected = '<input type="number" name="number" max="10">'; | ||
$this->assertSame($expected, $number->render()); | ||
} | ||
|
||
public function testStepValue() | ||
{ | ||
$number = new Number('number'); | ||
$number->step(1); | ||
|
||
$expected = '<input type="number" name="number" step="1">'; | ||
$this->assertSame($expected, $number->render()); | ||
} | ||
|
||
public function testPlaceholderValue() | ||
{ | ||
$number = new Number('number'); | ||
$number->placeholder('Number'); | ||
|
||
$expected = '<input type="number" name="number" placeholder="Number">'; | ||
$this->assertSame($expected, $number->render()); | ||
} | ||
} |