Skip to content

Commit 1ec6a8d

Browse files
authored
Merge pull request #95 from unclead/issue-94
Ability to set custom renderer
2 parents d3b0b3d + edeee9a commit 1ec6a8d

File tree

8 files changed

+92
-26
lines changed

8 files changed

+92
-26
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
Yii2 multiple input change log
22
==============================
33

4-
1.3.2 in development
4+
1.4.0 in development
55
--------------------
66

7+
- #94: added ability to set custom renderer (unclead, bokodi-dev)
8+
79
1.3.1
810
-----
911

docs/configuration.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ This can be `MultipleInput::POS_HEADER`, `MultipleInput::POS_ROW` or `MultipleIn
2727
**columnClass** *string*: the name of column class. You can specify your own class to extend base functionality.
2828
Defaults to `unclead\widgets\MultipleInputColumn` for `MultipleInput` and `unclead\widgets\TabularColumn` for `TabularInput`.
2929

30+
**rendererClass** *string*: the name of renderer class. You can specify your own class to extend base functionality.
31+
Defaults to `unclead\widgets\renderers\TableRenderer`.
32+
3033
**columns** *array*: the row columns configuration where you can set the properties which is described below
3134

3235
**rowOptions** *array|\Closure*: the HTML attributes for the table body rows. This can be either an array

docs/usage.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
> You can find source code of examples [here](./examples/)
44
5-
- [Input with one column](#one-column)
6-
- [Input with multiple column in each row](#multiple-columns)
7-
- [Tabular input](#tabular)
5+
- [One column](#one-column)
6+
- [Multiple columns](#multiple-columns-example)
7+
- [Tabular input](#tabular-input)
88

9-
##Input with one column <a href="one-column"></a>
9+
##One column
1010

1111
![Single column example](./images/single-column.gif?raw=true)
1212

@@ -32,7 +32,7 @@ use unclead\widgets\MultipleInput;
3232

3333
You can find more detail about this use case [here](multiple_input_single.md)
3434

35-
##Input with multiple column in each row <a href="multiple-columns"></a>
35+
##Multiple columns
3636

3737
![Multiple columns example](./images/multiple-column.gif?raw=true)
3838

@@ -105,7 +105,7 @@ use unclead\widgets\MultipleInput;
105105

106106
You can find more detail about this use case [here](multiple_input_multiple.md)
107107

108-
##Tabular input <a href="tabular"></a>
108+
##Tabular input
109109

110110
For example you want to manage some models via tabular input. In this case you can use `TabularInput` widget which is based on `MultipleInput` widget.
111111
Use the following code for this purpose:

src/MultipleInput.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use yii\widgets\InputWidget;
1515
use yii\db\ActiveRecordInterface;
1616
use unclead\widgets\renderers\TableRenderer;
17+
use unclead\widgets\renderers\RendererInterface;
1718

1819

1920
/**
@@ -23,9 +24,9 @@
2324
*/
2425
class MultipleInput extends InputWidget
2526
{
26-
const POS_HEADER = TableRenderer::POS_HEADER;
27-
const POS_ROW = TableRenderer::POS_ROW;
28-
const POS_FOOTER = TableRenderer::POS_FOOTER;
27+
const POS_HEADER = RendererInterface::POS_HEADER;
28+
const POS_ROW = RendererInterface::POS_ROW;
29+
const POS_FOOTER = RendererInterface::POS_FOOTER;
2930

3031
/**
3132
* @var ActiveRecordInterface[]|array[] input data
@@ -101,6 +102,12 @@ class MultipleInput extends InputWidget
101102
*/
102103
public $columnClass;
103104

105+
/**
106+
* @var string the name of renderer class. Defaults to `unclead\widgets\renderers\TableRenderer`.
107+
* @since 1.4
108+
*/
109+
public $rendererClass;
110+
104111
/**
105112
* Initialization.
106113
*
@@ -183,14 +190,18 @@ private function createRenderer()
183190
'context' => $this,
184191
];
185192

186-
if (!is_null($this->removeButtonOptions)) {
193+
if ($this->removeButtonOptions !== null) {
187194
$config['removeButtonOptions'] = $this->removeButtonOptions;
188195
}
189196

190-
if (!is_null($this->addButtonOptions)) {
197+
if ($this->addButtonOptions !== null) {
191198
$config['addButtonOptions'] = $this->addButtonOptions;
192199
}
193200

201+
if (!$this->rendererClass) {
202+
$this->rendererClass = TableRenderer::className();
203+
}
204+
194205
return new TableRenderer($config);
195206
}
196207
}

src/TabularInput.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@
1414
use yii\db\ActiveRecordInterface;
1515
use yii\bootstrap\Widget;
1616
use unclead\widgets\renderers\TableRenderer;
17+
use unclead\widgets\renderers\RendererInterface;
1718

1819
/**
1920
* Class TabularInput
2021
* @package unclead\widgets
2122
*/
2223
class TabularInput extends Widget
2324
{
24-
const POS_HEADER = TableRenderer::POS_HEADER;
25-
const POS_ROW = TableRenderer::POS_ROW;
26-
const POS_FOOTER = TableRenderer::POS_FOOTER;
25+
const POS_HEADER = RendererInterface::POS_HEADER;
26+
const POS_ROW = RendererInterface::POS_ROW;
27+
const POS_FOOTER = RendererInterface::POS_FOOTER;
2728

2829
/**
2930
* @var array
@@ -95,6 +96,12 @@ class TabularInput extends Widget
9596
*/
9697
public $columnClass;
9798

99+
/**
100+
* @var string the name of renderer class. Defaults to `unclead\widgets\renderers\TableRenderer`.
101+
* @since 1.4
102+
*/
103+
public $rendererClass;
104+
98105
/**
99106
* Initialization.
100107
*
@@ -142,14 +149,18 @@ private function createRenderer()
142149
'context' => $this,
143150
];
144151

145-
if (!is_null($this->removeButtonOptions)) {
152+
if ($this->removeButtonOptions !== null) {
146153
$config['removeButtonOptions'] = $this->removeButtonOptions;
147154
}
148155

149-
if (!is_null($this->addButtonOptions)) {
156+
if ($this->addButtonOptions !== null) {
150157
$config['addButtonOptions'] = $this->addButtonOptions;
151158
}
152159

160+
if (!$this->rendererClass) {
161+
$this->rendererClass = TableRenderer::className();
162+
}
163+
153164
return new TableRenderer($config);
154165
}
155166
}

src/components/BaseRenderer.php renamed to src/renderers/BaseRenderer.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @license https://github.com/unclead/yii2-multiple-input/blob/master/LICENSE.md
77
*/
88

9-
namespace unclead\widgets\components;
9+
namespace unclead\widgets\renderers;
1010

1111
use Yii;
1212
use yii\helpers\Html;
@@ -20,17 +20,14 @@
2020
use unclead\widgets\MultipleInput;
2121
use unclead\widgets\TabularInput;
2222
use unclead\widgets\assets\MultipleInputAsset;
23+
use unclead\widgets\components\BaseColumn;
2324

2425
/**
2526
* Class BaseRenderer
26-
* @package unclead\widgets\components
27+
* @package unclead\widgets\renderers
2728
*/
28-
abstract class BaseRenderer extends Object
29+
abstract class BaseRenderer extends Object implements RendererInterface
2930
{
30-
const POS_HEADER = 'header';
31-
const POS_ROW = 'row';
32-
const POS_FOOTER = 'footer';
33-
3431
/**
3532
* @var string the ID of the widget
3633
*/
@@ -116,7 +113,7 @@ abstract class BaseRenderer extends Object
116113
private $indexPlaceholder;
117114

118115
/**
119-
* @param $context
116+
* @inheritdoc
120117
*/
121118
public function setContext($context)
122119
{

src/renderers/RendererInterface.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/**
4+
* @link https://github.com/unclead/yii2-multiple-input
5+
* @copyright Copyright (c) 2014 unclead
6+
* @license https://github.com/unclead/yii2-multiple-input/blob/master/LICENSE.md
7+
*/
8+
9+
namespace unclead\widgets\renderers;
10+
11+
12+
/**
13+
* Interface RendererInterface
14+
* @package unclead\widgets\renderers
15+
*/
16+
interface RendererInterface
17+
{
18+
const POS_HEADER = 'header';
19+
const POS_ROW = 'row';
20+
const POS_FOOTER = 'footer';
21+
22+
/**
23+
* Renders the widget's content.
24+
*
25+
* @return mixed
26+
*/
27+
public function render();
28+
29+
/**
30+
* Set current context.
31+
*
32+
* @param mixed $context
33+
* @return mixed
34+
*/
35+
public function setContext($context);
36+
37+
/**
38+
* Returns a placeholder.
39+
*
40+
* @return string
41+
*/
42+
public function getIndexPlaceholder();
43+
}

src/renderers/TableRenderer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use yii\db\ActiveRecordInterface;
1313
use yii\helpers\ArrayHelper;
1414
use yii\helpers\Html;
15-
use unclead\widgets\components\BaseRenderer;
1615
use unclead\widgets\components\BaseColumn;
1716

1817
/**

0 commit comments

Comments
 (0)