Skip to content

Commit 8db559e

Browse files
author
Eugene Tupikov
committed
#16 refactoring rendering of inputs
1 parent 54653ce commit 8db559e

File tree

5 files changed

+121
-36
lines changed

5 files changed

+121
-36
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Yii2 multiple input change log
44
1.1.1 under development
55
-----------------------
66

7+
- Bug #16 refactoring rendering of inputs (unclead)
8+
79
1.1.0
810
-----
911

examples/models/ExampleModel.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class ExampleModel extends Model
3131
*/
3232
public $schedule;
3333

34+
public $enable;
35+
3436
public function init()
3537
{
3638
parent::init();
@@ -44,12 +46,14 @@ public function init()
4446
[
4547
'day' => '27.02.2015',
4648
'user_id' => 31,
47-
'priority' => 1
49+
'priority' => 1,
50+
'enable' => true
4851
],
4952
[
5053
'day' => '27.02.2015',
5154
'user_id' => 33,
52-
'priority' => 2
55+
'priority' => 2,
56+
'enable' => false
5357
],
5458
];
5559
}

examples/views/example.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525

2626
<h3>Single column</h3>
2727
<?php
28-
// echo $form->field($model, 'emails')->widget(MultipleInput::className(), [
29-
// 'limit' => 5
30-
// ])
31-
// ->label(false);
28+
echo $form->field($model, 'emails')->widget(MultipleInput::className(), [
29+
'limit' => 5
30+
])
31+
->label(false);
3232
?>
3333

3434
<h3>Multiple columns</h3>
@@ -43,7 +43,6 @@
4343
'title' => 'User',
4444
'defaultValue' => 33,
4545
'items' => [
46-
'' => 'Select user',
4746
31 => 'item 31',
4847
32 => 'item 32',
4948
33 => 'item 33',
@@ -96,11 +95,15 @@
9695
[
9796
'type' => 'checkbox',
9897
'name' => 'enable',
99-
'defaultValue' => 0
98+
'defaultValue' => 0,
99+
'headerOptions' => [
100+
'style' => 'width: 20px;',
101+
]
100102
]
101103
]
102104
]);
103105
?>
106+
104107
<?= Html::submitButton('Update', ['class' => 'btn btn-success']);?>
105108
<?php ActiveForm::end();?>
106109

src/MultipleInputColumn.php

Lines changed: 98 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use yii\db\ActiveRecord;
1515
use yii\helpers\ArrayHelper;
1616
use yii\helpers\Html;
17+
use yii\helpers\Inflector;
1718

1819
/**
1920
* Class MultipleInputColumn
@@ -28,6 +29,8 @@ class MultipleInputColumn extends Object
2829
const TYPE_CHECKBOX_LIST = 'checkboxList';
2930
const TYPE_RADIO_LIST = 'radioList';
3031
const TYPE_STATIC = 'static';
32+
const TYPE_CHECKBOX = 'checkbox';
33+
const TYPE_RADIO = 'radio';
3134

3235
/**
3336
* @var string input name
@@ -174,38 +177,12 @@ public function prepareValue($data)
174177
*/
175178
public function renderCellContent($value, $index)
176179
{
177-
$type = $this->type;
178180
$name = $this->widget->getElementName($this->name, $index);
179181

180182
$options = $this->options;
181183
$options['id'] = $this->widget->getElementId($this->name, $index);
182-
Html::addCssClass($options, 'form-control');
183184

184-
switch ($this->type) {
185-
case self::TYPE_HIDDEN_INPUT:
186-
$input = Html::hiddenInput($name, $value, $options);
187-
break;
188-
case self::TYPE_DROPDOWN:
189-
case self::TYPE_LISTBOX:
190-
case self::TYPE_CHECKBOX_LIST:
191-
case self::TYPE_RADIO_LIST:
192-
$input = Html::$type($name, $value, $this->items, $options);
193-
break;
194-
case self::TYPE_STATIC:
195-
$input = Html::tag('p', $value, ['class' => 'form-control-static']);
196-
break;
197-
default:
198-
if (method_exists('yii\helpers\Html', $type)) {
199-
$input = Html::$type($name, $value, $options);
200-
} elseif (class_exists($type) && method_exists($type, 'widget')) {
201-
$input = $type::widget(array_merge($options, [
202-
'name' => $name,
203-
'value' => $value,
204-
]));
205-
} else {
206-
throw new InvalidConfigException("Invalid column type '$type'");
207-
}
208-
}
185+
$input = $this->renderInput($name, $value, $options);
209186

210187
if ($this->isHiddenInput()) {
211188
return $input;
@@ -233,6 +210,100 @@ public function renderCellContent($value, $index)
233210
]);
234211
}
235212

213+
/**
214+
* Renders the input.
215+
*
216+
* @param string $name name of the input
217+
* @param mixed $value value of the input
218+
* @param array $options the input options
219+
* @return string
220+
*/
221+
private function renderInput($name, $value, $options)
222+
{
223+
$method = 'render' . Inflector::camelize($this->type);
224+
225+
if (method_exists($this, $method)) {
226+
$input = call_user_func_array([$this, $method], [$name, $value, $options]);
227+
} else {
228+
$input = $this->renderDefault($name, $value, $options);
229+
}
230+
return $input;
231+
}
232+
233+
234+
protected function renderDropDownList($name, $value, $options)
235+
{
236+
Html::addCssClass($options, 'form-control');
237+
return Html::dropDownList($name, $value, $this->items, $options);
238+
}
239+
240+
protected function renderListBox($name, $value, $options)
241+
{
242+
Html::addCssClass($options, 'form-control');
243+
return Html::listBox($name, $value, $this->items, $options);
244+
}
245+
246+
protected function renderHiddenInput($name, $value, $options)
247+
{
248+
return Html::hiddenInput($name, $value, $options);
249+
}
250+
251+
protected function renderRadio($name, $value, $options)
252+
{
253+
if (!isset($options['label'])) {
254+
$options['label'] = '';
255+
}
256+
$input = Html::radio($name, $value, $options);
257+
return Html::tag('div', $input, ['class' => 'radio']);
258+
}
259+
260+
protected function renderRadioList($name, $value, $options)
261+
{
262+
$options['item'] = function ($index, $label, $name, $checked, $value) {
263+
return '<div class="radio">' . Html::radio($name, $checked, ['label' => $label, 'value' => $value]) . '</div>';
264+
};
265+
$input = Html::radioList($name, $value, $this->items, $options);
266+
return Html::tag('div', $input, ['class' => 'radio']);
267+
}
268+
269+
protected function renderCheckbox($name, $value, $options)
270+
{
271+
if (!isset($options['label'])) {
272+
$options['label'] = '';
273+
}
274+
$input = Html::checkbox($name, $value, $options);
275+
return Html::tag('div', $input, ['class' => 'checkbox']);
276+
}
277+
278+
protected function renderCheckboxList($name, $value, $options)
279+
{
280+
$options['item'] = function ($index, $label, $name, $checked, $value) {
281+
return '<div class="checkbox">' . Html::checkbox($name, $checked, ['label' => $label, 'value' => $value]) . '</div>';
282+
};
283+
$input = Html::checkboxList($name, $value, $this->items, $options);
284+
return Html::tag('div', $input, ['class' => 'checkbox']);
285+
}
286+
287+
protected function renderDefault($name, $value, $options)
288+
{
289+
$type = $this->type;
290+
291+
if ($type == self::TYPE_STATIC) {
292+
$input = Html::tag('p', $value, ['class' => 'form-control-static']);
293+
} elseif (method_exists('yii\helpers\Html', $type)) {
294+
Html::addCssClass($options, 'form-control');
295+
$input = Html::$type($name, $value, $options);
296+
} elseif (class_exists($type) && method_exists($type, 'widget')) {
297+
$input = $type::widget(array_merge($options, [
298+
'name' => $name,
299+
'value' => $value,
300+
]));
301+
} else {
302+
throw new InvalidConfigException("Invalid column type '$type'");
303+
}
304+
return $input;
305+
}
306+
236307
/**
237308
* @param string $error
238309
* @return string

src/assets/src/css/multiple-input.css

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
width: 80%;
88
}
99

10-
.multiple-input-list.no-buttons .multiple-input-list__input {
10+
.multiple-input-list.no-buttons
11+
.multiple-input-list__input {
1112
display: block;
1213
width: 100%;
1314
}
@@ -29,4 +30,8 @@ table.multiple-input-list tr > th {
2930
}
3031
.multiple-input-list__item .list-cell__button {
3132
width: 40px;
33+
}
34+
.multiple-input-list__item .radio,
35+
.multiple-input-list__item .checkbox {
36+
margin: 7px 0 7px 0;
3237
}

0 commit comments

Comments
 (0)