Skip to content

Commit 007297a

Browse files
authored
Merge pull request #218 from antkaz/master
Fixed #217 Added 'layoutConfig' property for ListRenderer
2 parents a79b6d6 + ef58ed8 commit 007297a

File tree

6 files changed

+55
-10
lines changed

6 files changed

+55
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Yii2 multiple input change log
33

44
2.15.0 (in development)
55
=======================
6+
- #217 added `layoutConfig` property for the ListRenderer
67

78
2.14.0
89
======

examples/actions/TabularInputAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class TabularInputAction extends Action
2323
{
2424
public function run()
2525
{
26-
Yii::setAlias('@unclead-examples', realpath(__DIR__ . '/../'));
26+
Yii::setAlias('@unclead-examples', dirname(__DIR__) . '/');
2727

2828
$models = $this->getItems();
2929

examples/views/tabular-input.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use unclead\multipleinput\renderers\ListRenderer;
34
use yii\bootstrap\ActiveForm;
45
use unclead\multipleinput\TabularInput;
56
use yii\helpers\Html;
@@ -21,7 +22,14 @@
2122
<?= TabularInput::widget([
2223
'models' => $models,
2324
'modelClass' => Item::class,
25+
'rendererClass' => ListRenderer::class,
2426
'min' => 0,
27+
'layoutConfig' => [
28+
'offsetClass' => 'col-sm-offset-4',
29+
'labelClass' => 'col-sm-4',
30+
'wrapperClass' => 'col-sm-4',
31+
'errorClass' => 'col-sm-4'
32+
],
2533
'attributeOptions' => [
2634
'enableAjaxValidation' => true,
2735
'enableClientValidation' => false,

src/TabularInput.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ class TabularInput extends Widget
139139
*/
140140
public $modelClass;
141141

142+
/**
143+
* @var array CSS grid classes for horizontal layout. This must be an array with these keys:
144+
* - 'offsetClass' the offset grid class to append to the wrapper if no label is rendered
145+
* - 'labelClass' the label grid class
146+
* - 'wrapperClass' the wrapper grid class
147+
* - 'errorClass' the error grid class
148+
*/
149+
public $layoutConfig = [];
150+
142151
/**
143152
* Initialization.
144153
*
@@ -206,6 +215,7 @@ private function createRenderer()
206215
'form' => $this->form,
207216
'sortable' => $this->sortable,
208217
'enableError' => $this->enableError,
218+
'layoutConfig' => $this->layoutConfig,
209219
];
210220

211221
if ($this->removeButtonOptions !== null) {

src/renderers/BaseRenderer.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@ abstract class BaseRenderer extends BaseObject implements RendererInterface
157157
*/
158158
public $extraButtons;
159159

160+
/**
161+
* @var array CSS grid classes for horizontal layout. This must be an array with these keys:
162+
* - 'offsetClass' the offset grid class to append to the wrapper if no label is rendered
163+
* - 'labelClass' the label grid class
164+
* - 'wrapperClass' the wrapper grid class
165+
* - 'errorClass' the error grid class
166+
*/
167+
public $layoutConfig = [];
168+
160169
/**
161170
* @inheritdoc
162171
*/

src/renderers/ListRenderer.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,20 @@ public function renderCellContent($column, $index)
194194

195195
$hasError = false;
196196
$error = '';
197+
$layoutConfig = array_merge([
198+
'offsetClass' => 'col-sm-offset-3',
199+
'labelClass' => 'col-sm-3',
200+
'wrapperClass' => 'col-sm-6',
201+
'errorClass' => 'col-sm-offset-3 col-sm-6',
202+
], $this->layoutConfig);
203+
204+
Html::addCssClass($column->errorOptions, $layoutConfig['errorClass']);
197205

198206
if ($index !== null) {
199207
$error = $column->getFirstError($index);
200208
$hasError = !empty($error);
201209
}
202210

203-
if ($column->enableError) {
204-
$input .= "\n" . $column->renderError($error);
205-
}
206-
207211
$wrapperOptions = [
208212
'class' => 'field-' . $id
209213
];
@@ -212,13 +216,26 @@ public function renderCellContent($column, $index)
212216
Html::addCssClass($wrapperOptions, 'has-error');
213217
}
214218

215-
$input = Html::tag('div', $input, $wrapperOptions);
219+
Html::addCssClass($wrapperOptions, $layoutConfig['wrapperClass']);
216220

217-
$content = Html::beginTag('div', ['class' => 'form-group list-cell__' . $column->name]);
218-
$content .= Html::label($column->title, $id, [
219-
'class' => 'col-sm-2 control-label' . (empty($column->title) ? ' sr-only' : '')
221+
$content = Html::beginTag('div', [
222+
'class' => 'form-group list-cell__' . $column->name . ($hasError ? ' has-error' : '')
220223
]);
221-
$content .= Html::tag('div', $input, ['class' => 'col-sm-10']);
224+
225+
if (empty($column->title)) {
226+
Html::addCssClass($wrapperOptions, $layoutConfig['offsetClass']);
227+
} else {
228+
$content .= Html::label($column->title, $id, [
229+
'class' => $layoutConfig['labelClass'] . ' control-label'
230+
]);
231+
}
232+
233+
$content .= Html::tag('div', $input, $wrapperOptions);
234+
235+
if ($column->enableError) {
236+
$content .= "\n" . $column->renderError($error);
237+
}
238+
222239
$content .= Html::endTag('div');
223240

224241
return $content;

0 commit comments

Comments
 (0)