Skip to content

Commit

Permalink
Fix addons with Bootstrap 4
Browse files Browse the repository at this point in the history
Usage with Laravel:
{{-- Basic input addon --}}
{!! Former::open()->method('GET') !!}
    {!! Former::text('test2')->append('€') !!}
{!! Former::close() !!}

{{-- Multiple input addons --}}
{!! Former::open()->method('GET') !!}
    {!! Former::text('test3')->append(['€', 'second']) !!}
{!! Former::close() !!}

{{-- Button addon --}}
{!! Former::open()->method('GET') !!}
{!! Former::text('test4')->append('<button class="btn">OK</button>')
!!}
{!! Former::close() !!}

{{-- Button addons --}}
{!! Former::open()->method('GET') !!}
{!! Former::text('test5')->append(['<button
class="btn">OK</button>', '<button class="btn">2</button>']) !!}
{!! Former::close() !!}

Credits: @mrextreme

Reference:
formers#581 (comment)
  • Loading branch information
tortuetorche committed Dec 30, 2020
1 parent c5bcd07 commit fd9e5cd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Former/Form/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ protected function placeAround($items, $place)
{
// Iterate over the items and place them where they should
foreach ((array) $items as $item) {
$item = $this->app['former.framework']->placeAround($item);
$item = $this->app['former.framework']->placeAround($item, $place);
$this->{$place}[] = $item;
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/Former/Framework/TwitterBootstrap4.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,17 +405,25 @@ public function createPlainTextField(Field $field)
*
* @return Element A wrapped item
*/
public function placeAround($item)
public function placeAround($item, $place = null)
{
// Render object
if (is_object($item) and method_exists($item, '__toString')) {
$item = $item->__toString();
}

// Get class to use
$class = (strpos($item, '<button') !== false) ? 'btn' : 'addon';
$items = (array) $item;
$element = '';
foreach ($items as $item) {
$hasHtmlTag = strpos(ltrim($item), '<') === 0;

return Element::create('span', $item)->addClass('input-group-'.$class);
// Get class to use
$class = $hasHtmlTag ? '' : 'input-group-text';

$element .= $hasHtmlTag ? $item : Element::create('span', $item)->addClass($class);
}

return Element::create('div', $element)->addClass('input-group-'.$place);
}

/**
Expand Down

0 comments on commit fd9e5cd

Please sign in to comment.