diff --git a/src/AdamWathan/BootForms/BasicFormBuilder.php b/src/AdamWathan/BootForms/BasicFormBuilder.php index 45bdf25..6f173d0 100644 --- a/src/AdamWathan/BootForms/BasicFormBuilder.php +++ b/src/AdamWathan/BootForms/BasicFormBuilder.php @@ -5,6 +5,7 @@ use AdamWathan\BootForms\Elements\CheckGroup; use AdamWathan\BootForms\Elements\HelpBlock; use AdamWathan\BootForms\Elements\GroupWrapper; +use AdamWathan\BootForms\Elements\InputGroup; class BasicFormBuilder { @@ -169,6 +170,14 @@ public function file($label, $name, $value = null) return $this->wrap($formGroup); } + public function inputGroup($label, $name, $value = null) + { + $control = new InputGroup($name); + $control->value($value); + + return $this->formGroup($label, $name, $control); + } + public function __call($method, $parameters) { return call_user_func_array(array($this->builder, $method), $parameters); diff --git a/src/AdamWathan/BootForms/Elements/FormGroup.php b/src/AdamWathan/BootForms/Elements/FormGroup.php index c2753ed..098feeb 100644 --- a/src/AdamWathan/BootForms/Elements/FormGroup.php +++ b/src/AdamWathan/BootForms/Elements/FormGroup.php @@ -8,7 +8,6 @@ class FormGroup extends Element protected $label; protected $control; protected $helpBlock; - protected $inputGroup; public function __construct(Label $label, Element $control) { @@ -22,32 +21,15 @@ public function render() $html = 'renderAttributes(); $html .= '>'; - $html .= $this->label; - $html .= $this->renderInputGroup(); + $html .= $this->label; + $html .= $this->control; $html .= $this->renderHelpBlock(); + $html .= ''; return $html; } - public function inputGroup($class = "") - { - if (!isset($this->inputGroup)) { - $this->inputGroup = new InputGroup($this, $class); - } - - return $this->inputGroup; - } - - protected function renderInputGroup() - { - if ($this->inputGroup) { - return $this->inputGroup->render(); - } - - return $this->control; - } - public function helpBlock($text) { if (isset($this->helpBlock)) { diff --git a/src/AdamWathan/BootForms/Elements/GroupWrapper.php b/src/AdamWathan/BootForms/Elements/GroupWrapper.php index cb49d03..73e4538 100644 --- a/src/AdamWathan/BootForms/Elements/GroupWrapper.php +++ b/src/AdamWathan/BootForms/Elements/GroupWrapper.php @@ -21,12 +21,7 @@ public function helpBlock($text) $this->formGroup->helpBlock($text); return $this; } - - public function inputGroup($class = "") - { - return $this->formGroup->inputGroup($class); - } - + public function __toString() { return $this->render(); diff --git a/src/AdamWathan/BootForms/Elements/InputGroup.php b/src/AdamWathan/BootForms/Elements/InputGroup.php index 7ab2dbb..f3e1d41 100644 --- a/src/AdamWathan/BootForms/Elements/InputGroup.php +++ b/src/AdamWathan/BootForms/Elements/InputGroup.php @@ -1,55 +1,55 @@ formGroup = $formGroup; - $this->addClass('input-group'); - if (!empty($class)) $this->addClass($class); - } + protected $afterAddon = array(); - public function beforeAddon($addon) - { - $this->beforeAddon[] = $addon; - return $this->formGroup; - } + public function beforeAddon($addon) + { + $this->beforeAddon[] = $addon; + + return $this; + } + + public function afterAddon($addon) + { + $this->afterAddon[] = $addon; - public function afterAddon($addon) + return $this; + } + + public function type($type) { - $this->afterAddon[] = $addon; - return $this->formGroup; + $this->attributes['type'] = $type; + return $this; } - protected function renderAddons($addons) - { - $html = ''; - foreach ($addons as $addon) { - $html .= ''; - $html .= $addon; - $html .= ''; - } + protected function renderAddons($addons) + { + $html = ''; - return $html; - } + foreach ($addons as $addon) + { + $html .= ''; + $html .= $addon; + $html .= ''; + } + + return $html; + } public function render() { - $html = 'renderAttributes(); - $html .= '>'; + $html = '
'; $html .= $this->renderAddons($this->beforeAddon); - $html .= $this->formGroup->control(); + $html .= parent::render(); $html .= $this->renderAddons($this->afterAddon); $html .= '
'; - + return $html; } -} \ No newline at end of file +} diff --git a/tests/BasicFormBuilderTest.php b/tests/BasicFormBuilderTest.php index 19df4ee..01dd96f 100644 --- a/tests/BasicFormBuilderTest.php +++ b/tests/BasicFormBuilderTest.php @@ -59,13 +59,6 @@ public function testRenderTextGroupWithErrorOverridesCustomHelpBlock() $this->assertEquals($expected, $result); } - public function testRenderTextGroupWithInputGroup() - { - $expected = '
@
'; - $result = $this->form->text('Email', 'email')->inputGroup()->beforeAddon('@')->render(); - $this->assertEquals($expected, $result); - } - public function testRenderTextGroupWithOldInput() { $oldInput = Mockery::mock('AdamWathan\Form\OldInput\OldInputInterface'); @@ -480,6 +473,32 @@ public function testCanHideLabels() $this->assertEquals($expected, $result); } + public function testRenderInputGroupWithBeforeAddon() + { + $expected = '
@
'; + $result = $this->form->inputGroup('Username', 'username')->beforeAddon('@')->render(); + $this->assertEquals($expected, $result); + } + + public function testRenderInputGroupWithAfterAddon() + { + $expected = '
.com.br
'; + $result = $this->form->inputGroup('Site', 'site')->afterAddon('.com.br')->render(); + $this->assertEquals($expected, $result); + } + + public function testRenderInputGroupChangeTypeWithBothAddon() + { + $expected = '
beforeafter
'; + $result = $this->form + ->inputGroup('Secret', 'secret') + ->type('password') + ->beforeAddon('before') + ->afterAddon('after') + ->render(); + $this->assertEquals($expected, $result); + } + private function getStubObject() { $obj = new stdClass; diff --git a/tests/FormGroupTest.php b/tests/FormGroupTest.php index 78e041d..4260434 100644 --- a/tests/FormGroupTest.php +++ b/tests/FormGroupTest.php @@ -81,16 +81,4 @@ public function testCanRenderWithHelpBlock() $result = $formGroup->render(); $this->assertEquals($expected, $result); } - - public function testCanRenderWithInputGroup() - { - $label = $this->builder->label('Site'); - $text = $this->builder->text('site'); - $formGroup = new FormGroup($label, $text); - $formGroup->inputGroup()->beforeAddon('www')->inputGroup()->afterAddon('.com.br'); - - $expected = '
www.com.br
'; - $result = $formGroup->render(); - $this->assertEquals($expected, $result); - } } diff --git a/tests/InputGroupTest.php b/tests/InputGroupTest.php index e975426..5e26b26 100644 --- a/tests/InputGroupTest.php +++ b/tests/InputGroupTest.php @@ -5,35 +5,34 @@ class InputGroupTest extends PHPUnit_Framework_TestCase { - public function setUp() + public function testCanRenderBasicText() { + $input = new InputGroup('email'); + $this->assertInstanceOf('AdamWathan\Form\Elements\Text', $input); + + $expected = '
'; + $result = $input->render(); + $this->assertEquals($expected, $result); } public function testCanRenderBeforeAddon() { - $control = ''; - $formGroup = m::mock('AdamWathan\BootForms\Elements\FormGroup'); - $formGroup->shouldReceive('control')->once()->andReturn($control); - - $inputGroup = new InputGroup($formGroup, 'input-group-lg'); - $this->assertEquals($formGroup, $inputGroup->beforeAddon('@')); - - $expected = '
@'.$control.'
'; - $result = $inputGroup->render(); - $this->assertEquals($expected, $result); + $input = new InputGroup('username'); + $this->assertEquals($input, $input->beforeAddon('@')); + + $expected = '
@
'; + $result = $input->render(); + $this->assertEquals($expected, $result); } - public function testCanRenderAfterAddon() + public function testCanRenderAfterAddonAndType() { - $control = ''; - $formGroup = m::mock('AdamWathan\BootForms\Elements\FormGroup'); - $formGroup->shouldReceive('control')->once()->andReturn($control); - - $inputGroup = new InputGroup($formGroup, 'input-group-lg'); - $this->assertEquals($formGroup, $inputGroup->afterAddon(',00')); - - $expected = '
'.$control.',00
'; - $result = $inputGroup->render(); - $this->assertEquals($expected, $result); + $input = new InputGroup('mail'); + $this->assertEquals($input, $input->type('email')); + $this->assertEquals($input, $input->afterAddon('@domain.com')); + + $expected = '
@domain.com
'; + $result = $input->render(); + $this->assertEquals($expected, $result); } } \ No newline at end of file