Skip to content

Commit

Permalink
Merge pull request #607 from tortuetorche/add-bs5-framework
Browse files Browse the repository at this point in the history
Bootstrap 5.0 support
  • Loading branch information
tortuetorche authored Jun 2, 2021
2 parents 13a7d4e + 8d8856a commit 45d37ae
Show file tree
Hide file tree
Showing 13 changed files with 1,668 additions and 24 deletions.
23 changes: 23 additions & 0 deletions src/Former/Form/Fields/Switchbox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace Former\Form\Fields;

class Switchbox extends Checkbox
{
////////////////////////////////////////////////////////////////////
////////////////////////// FIELD METHODS ///////////////////////////
////////////////////////////////////////////////////////////////////

/**
* Create a serie of switches
*/
public function switches()
{
if ($this->isGrouped()) {
// Remove any possible items added by the Populator.
$this->items = array();
}
$this->items(func_get_args());

return $this;
}
}
77 changes: 68 additions & 9 deletions src/Former/Form/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use BadMethodCallException;
use Former\Helpers;
use HtmlObject\Input;
use HtmlObject\Element;
use HtmlObject\Traits\Tag;
use Illuminate\Container\Container;
Expand Down Expand Up @@ -176,17 +177,30 @@ public function wrapField($field)
{
$label = $this->getLabel($field);
$help = $this->getHelp();
if ($field->isCheckable() && $this->app['former']->framework() == 'TwitterBootstrap4') {
$wrapperClass = $field->isInline() ? 'form-check form-check-inline' : 'form-check';
if ($field->isCheckable() &&
in_array($this->app['former']->framework(), ['TwitterBootstrap4', 'TwitterBootstrap5'])
) {
$wrapperClass = null;
if ($this->app['former']->framework() === 'TwitterBootstrap4') {
$wrapperClass = $field->isInline() ? 'form-check form-check-inline' : 'form-check';
}
if ($this->app['former']->getErrors($field->getName())) {
$hiddenInput = Element::create('input', null, ['type' => 'hidden'])->class('form-check-input is-invalid');
$hiddenInput = Input::create('hidden')->addClass('form-check-input is-invalid');
$help = $hiddenInput.$help;
}
$help = Element::create('div', $help)->class($wrapperClass);
$help = $help ? Element::create('div', $help)->addClass($wrapperClass) : '';
}
$withFloatingLabel = $field->withFloatingLabel();

$field = $this->prependAppend($field);
$field .= $help;

if ($withFloatingLabel &&
$this->app['former']->framework() === 'TwitterBootstrap5'
) {
return $this->wrapWithFloatingLabel($field, $label);
}

return $this->wrap($field, $label);
}

Expand All @@ -210,17 +224,27 @@ public function state($state)
/**
* Set a class on the Group
*
* @param string $class The class to add
* @param string $class The class(es) to add on the Group
*/
public function addGroupClass($class)
{
$this->addClass($class);
}

/**
* Remove one or more classes on the Group
*
* @param string $class The class(es) to remove on the Group
*/
public function removeGroupClass($class)
{
$this->removeClass($class);
}

/**
* Set a class on the Label
*
* @param string $class The class to add on the Label
* @param string $class The class(es) to add on the Label
*/
public function addLabelClass($class)
{
Expand All @@ -234,6 +258,23 @@ public function addLabelClass($class)
return $this;
}

/**
* Remove one or more classes on the Label
*
* @param string $class The class(es) to remove on the Label
*/
public function removeLabelClass($class)
{
// Don't remove a label class if it isn't an Element instance
if (!$this->label instanceof Element) {
return $this;
}

$this->label->removeClass($class);

return $this;
}

/**
* Adds a label to the group
*
Expand Down Expand Up @@ -322,8 +363,9 @@ public function blockHelp($help, $attributes = array())
{
// Reserved method
if ($this->app['former.framework']->isnt('TwitterBootstrap') &&
$this->app['former.framework']->isnt('TwitterBootstrap3') &&
$this->app['former.framework']->isnt('TwitterBootstrap4')
$this->app['former.framework']->isnt('TwitterBootstrap3') &&
$this->app['former.framework']->isnt('TwitterBootstrap4') &&
$this->app['former.framework']->isnt('TwitterBootstrap5')
) {
throw new BadMethodCallException('This method is only available on the Bootstrap framework');
}
Expand Down Expand Up @@ -428,6 +470,23 @@ public function wrap($contents, $label = null)
return $group;
}

/**
* Wraps content in a group with floating label
*
* @param string $contents The content
* @param string $label The label to add
*
* @return string A group
*/
public function wrapWithFloatingLabel($contents, $label = null)
{
$floatingLabelClass = $this->app['former.framework']->getFloatingLabelClass();
if ($floatingLabelClass) {
$this->addClass($floatingLabelClass);
}
return $this->wrap($label, $contents);
}

/**
* Prints out the current label
*
Expand All @@ -445,7 +504,7 @@ protected function getLabel($field = null)
// Wrap label in framework classes
$labelClasses = $this->app['former.framework']->getLabelClasses();
if ($field->isCheckable() &&
$this->app['former']->framework() == 'TwitterBootstrap4' &&
in_array($this->app['former']->framework(), ['TwitterBootstrap4', 'TwitterBootstrap5']) &&
$this->app['former.form']->isOfType('horizontal')
) {
$labelClasses = array_merge($labelClasses, array('pt-0'));
Expand Down
Loading

0 comments on commit 45d37ae

Please sign in to comment.