Skip to content

Commit 238796e

Browse files
authored
Merge pull request #275 from bscheshirwork/add-template-for-input
- Add template for input - Pass more params to a prepareValue closure - implement DivRenderer
2 parents 3f5447f + 7ea250a commit 238796e

File tree

3 files changed

+447
-21
lines changed

3 files changed

+447
-21
lines changed

src/components/BaseColumn.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ abstract class BaseColumn extends BaseObject
150150
*/
151151
public $columnOptions = [];
152152

153+
/**
154+
* @var string the template of input for customize view.
155+
* For example: '<div class="input-group"><span class="input-group-addon"><i class="fas fa-mobile-alt"></i></span>{input}</div>'
156+
*/
157+
public $inputTemplate = '{input}';
158+
153159
/**
154160
* @var Model|ActiveRecordInterface|array
155161
*/
@@ -214,14 +220,19 @@ public function isHiddenInput()
214220

215221
/**
216222
* Prepares the value of column.
217-
*
223+
* @param array $contextParams the params who passed to closure:
224+
* string $id the id of input element
225+
* string $name the name of input element
226+
* string $indexPlaceholder The index placeholder of multiple input. The {$indexPlaceholder} template will be replace by $index
227+
* int $index The index of multiple input
228+
* int $columnIndex The index of current model attributes
218229
* @return mixed
219230
*/
220-
protected function prepareValue()
231+
protected function prepareValue($contextParams = [])
221232
{
222233
$data = $this->getModel();
223234
if ($this->value instanceof \Closure) {
224-
$value = call_user_func($this->value, $data);
235+
$value = call_user_func($this->value, $data, $contextParams);
225236
} else {
226237
$value = null;
227238
if ($data instanceof ActiveRecordInterface ) {
@@ -284,11 +295,18 @@ private function normalize($name) {
284295
/**
285296
* Renders the input.
286297
*
287-
* @param string $name the name of the input
288-
* @param array $options the HTML options of input
298+
* @param string $name the name of the input
299+
* @param array $options the HTML options of input
300+
* @param array $contextParams the params who passed to closure:
301+
* string $id the id of input element
302+
* string $name the name of input element
303+
* string $indexPlaceholder The index placeholder of multiple input. The {$indexPlaceholder} template will be replace by $index
304+
* int $index The index of multiple input
305+
* int $columnIndex The index of current model attributes
289306
* @return string
307+
* @throws InvalidConfigException
290308
*/
291-
public function renderInput($name, $options)
309+
public function renderInput($name, $options, $contextParams = [])
292310
{
293311
if ($this->options instanceof \Closure) {
294312
$optionsExt = call_user_func($this->options, $this->getModel());
@@ -301,7 +319,7 @@ public function renderInput($name, $options)
301319

302320
$value = null;
303321
if ($this->type !== self::TYPE_DRAGCOLUMN) {
304-
$value = $this->prepareValue();
322+
$value = $this->prepareValue($contextParams);
305323
}
306324

307325
if (isset($options['items'])) {
@@ -314,7 +332,7 @@ public function renderInput($name, $options)
314332
$input = $this->renderDefault($name, $value, $options);
315333
}
316334

317-
return $input;
335+
return strtr($this->inputTemplate, ['{input}' => $input]);
318336
}
319337

320338

0 commit comments

Comments
 (0)