Skip to content

Commit

Permalink
grid is defined in renderer config
Browse files Browse the repository at this point in the history
  • Loading branch information
czubehead committed May 28, 2018
1 parent 9cd2810 commit 08ffa40
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 24 deletions.
2 changes: 2 additions & 0 deletions src/BootstrapContainer.php
Expand Up @@ -10,6 +10,7 @@
namespace Czubehead\BootstrapForms;


use Czubehead\BootstrapForms\Traits\AddRowTrait;
use Czubehead\BootstrapForms\Traits\BootstrapContainerTrait;
use Nette\Forms\Container;

Expand All @@ -22,4 +23,5 @@
class BootstrapContainer extends Container
{
use BootstrapContainerTrait;
use AddRowTrait;
}
16 changes: 2 additions & 14 deletions src/BootstrapForm.php
Expand Up @@ -9,7 +9,7 @@
namespace Czubehead\BootstrapForms;


use Czubehead\BootstrapForms\Grid\BootstrapRow;
use Czubehead\BootstrapForms\Traits\AddRowTrait;
use Czubehead\BootstrapForms\Traits\BootstrapContainerTrait;
use Nette\Application\UI\Form;
use Nette\ComponentModel\IContainer;
Expand All @@ -30,6 +30,7 @@
class BootstrapForm extends Form
{
use BootstrapContainerTrait;
use AddRowTrait;

/**
* @var string Class to be added if this is ajax. Defaults to 'ajax'
Expand Down Expand Up @@ -76,19 +77,6 @@ public function __construct($container = NULL)
};
}

/**
* Adds a new Grid system row.
* @param string|null $name optional. If null is passed, it is generated.
* @return BootstrapRow
*/
public function addRow($name = NULL)
{
$row = new BootstrapRow($this, $name);
$this->addComponent($row, $row->name);

return $row;
}

public function getElementPrototype()
{
return $this->elementPrototype;
Expand Down
8 changes: 8 additions & 0 deletions src/BootstrapRenderer.php
Expand Up @@ -167,6 +167,14 @@ public function getConfig()
Cnf::elementName => 'legend',
],

Cnf::gridRow => [
Cnf::elementName => 'div',
Cnf::classSet => 'form-row',
],
Cnf::gridCell => [
Cnf::elementName => 'div',
],

Cnf::formOwnErrors => [],
Cnf::formOwnError => [
Cnf::elementName => 'div',
Expand Down
14 changes: 14 additions & 0 deletions src/Enums/RendererConfig.php
Expand Up @@ -7,6 +7,10 @@
namespace Czubehead\BootstrapForms\Enums;


use Czubehead\BootstrapForms\Grid\BootstrapCell;
use Czubehead\BootstrapForms\Grid\BootstrapRow;


class RendererConfig
{
/**
Expand All @@ -23,6 +27,16 @@ class RendererConfig
*/
const groupLabel = 'group-label';

/**
* Bootstrap row
* @see BootstrapRow
*/
const gridRow = 'grid-row';
/**
* @see BootstrapCell
*/
const gridCell = 'grid-cell';

/**
* Errors belonging to the form rather than an individual control. This is a container.
*/
Expand Down
10 changes: 4 additions & 6 deletions src/Grid/BootstrapCell.php
Expand Up @@ -8,7 +8,7 @@


use Czubehead\BootstrapForms\BootstrapRenderer;
use Czubehead\BootstrapForms\BootstrapUtils;
use Czubehead\BootstrapForms\Enums\RendererConfig;
use Czubehead\BootstrapForms\Traits\BootstrapContainerTrait;
use LogicException;
use Nette\ComponentModel\IComponent;
Expand Down Expand Up @@ -96,15 +96,13 @@ public function getNumOfColumns()
public function render()
{
$element = $this->elementPrototype;
/** @var BootstrapRenderer $renderer */
$renderer = $this->row->getParent()->form->renderer;

BootstrapUtils::standardizeClass($element);
$element = $renderer->configElem(RendererConfig::gridCell, $element);
$element->class[] = $this->createClass();

if ($this->childControl) {
/** @noinspection PhpUndefinedFieldInspection */
/** @var BootstrapRenderer $renderer */
$renderer = $this->childControl->form->renderer;

$pairHtml = $renderer->renderPair($this->childControl);
$element->addHtml($pairHtml);
}
Expand Down
11 changes: 7 additions & 4 deletions src/Grid/BootstrapRow.php
Expand Up @@ -6,6 +6,8 @@

namespace Czubehead\BootstrapForms\Grid;

use Czubehead\BootstrapForms\BootstrapRenderer;
use Czubehead\BootstrapForms\Enums\RendererConfig;
use Czubehead\BootstrapForms\Traits\FakeControlTrait;
use Nette\ComponentModel\IComponent;
use Nette\ComponentModel\IContainer;
Expand Down Expand Up @@ -93,9 +95,7 @@ public function __construct(Container $container, $name = NULL)
}
$this->name = $name;

$this->elementPrototype = Html::el('div', [
'class' => ['row'],
]);
$this->elementPrototype = Html::el();
}

/**
Expand Down Expand Up @@ -222,7 +222,10 @@ public function getOwnedNames()
*/
public function render()
{
$element = $this->elementPrototype;
/** @var BootstrapRenderer $renderer */
$renderer = $this->container->form->renderer;

$element = $renderer->configElem(RendererConfig::gridRow, $this->elementPrototype);
foreach ($this->cells as $cell) {
$cellHtml = $cell->render();
$element->addHtml($cellHtml);
Expand Down
32 changes: 32 additions & 0 deletions src/Traits/AddRowTrait.php
@@ -0,0 +1,32 @@
<?php
/**
* Created by Petr Čech (czubehead).
* Timestamp: 28.5.18 20:51
*/

namespace Czubehead\BootstrapForms\Traits;


use Czubehead\BootstrapForms\Grid\BootstrapRow;


/**
* Trait AddRowTrait. Implements method to add a bootstrap row.
* @package Czubehead\BootstrapForms\Traits
*/
trait AddRowTrait
{
/**
* Adds a new Grid system row.
* @param string|null $name optional. If null is passed, it is generated.
* @return BootstrapRow
*/
public function addRow($name = NULL)
{
/** @noinspection PhpParamsInspection */
$row = new BootstrapRow($this, $name);
$this->addComponent($row, $row->name);

return $row;
}
}

0 comments on commit 08ffa40

Please sign in to comment.