Skip to content

Commit

Permalink
Merge pull request #212 from spaantje/main
Browse files Browse the repository at this point in the history
Add Search & support Collections in Select
  • Loading branch information
freekmurze authored Feb 20, 2024
2 parents 0c0e1bc + c0c9a76 commit ead179a
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 3 deletions.
7 changes: 7 additions & 0 deletions docs/general-usage/html-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,12 @@ To make things easier we've added the `modelForm()` and `closeModelForm()` metho
- `function select($name = null, $options = [], $value = null): Select`
- `function submit($text = null): Button`
- `function text($name = null, $value = null): Input`
- `function search($name = null, $value = null): Input`
- `function date($name = '', $value = null, $format = true): Input`
- `function datetime($name = '', $value = null, $format = true): Input`
- `function time($name = '', $value = null, $format = true): Input`
- `function range($name = '', $value = '', $min = null, $max = null, $step = null): Input`
- `function number($name = null, $value = null, $min = null, $max = null, $step = null): Input`
- `function textarea($name = null, $value = null): Textarea`
- `function file($name = null): File`
- `function token(): Input`
4 changes: 1 addition & 3 deletions src/Elements/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function multiple()
public function options($options)
{
return $this->addChildren($options, function ($text, $value) {
if (is_array($text)) {
if (is_array($text) || $text instanceof Collection) {
return $this->optgroup($value, $text);
}

Expand All @@ -84,8 +84,6 @@ public function optgroup($label, $options)
->text($text)
->selectedIf($value === $this->value);
});

return $this->addChild($optgroup);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ public function email($name = null, $value = null)
return $this->input('email', $name, $value);
}

/**
* @param string|null $name
* @param string|null $value
*
* @return \Spatie\Html\Elements\Input
*/
public function search($name = null, $value = null)
{
return $this->input('search', $name, $value);
}

/**
* @param string|null $name
* @param string|null $value
Expand Down
14 changes: 14 additions & 0 deletions tests/Html/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,17 @@
$this->html->number('test', '30', null, '100', '10')
);
});

it('can create a search input', function () {
assertHtmlStringEqualsHtmlString(
'<input type="search">',
$this->html->search()
);
});

it('can create a search input with blank date', function () {
assertHtmlStringEqualsHtmlString(
'<input id="test_search" name="test_search" type="search" value=""/>',
$this->html->search('test_search', '')
);
});
72 changes: 72 additions & 0 deletions tests/Html/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,75 @@
$this->html->select('select', $options, '+2')->render()
);
});

it('can render a select element with options in groups', function () {
$options = [
'group' => [
'value1' => 'text1',
'value2' => 'text2',
],
];

assertHtmlStringEqualsHtmlString(
'<select name="select" id="select">
<optgroup label="group">
<option value="value1">text1</option>
<option value="value2">text2</option>
</optgroup>
</select>',
$this->html->select('select', $options)->render()
);
});

it('can render a select element with collection of options with a selected value', function () {
$options = collect([
'value1' => 'text1',
'value2' => 'text2',
]);

assertHtmlStringEqualsHtmlString(
'<select name="select" id="select">
<option value="value1" selected="selected">text1</option>
<option value="value2">text2</option>
</select>',
$this->html->select('select', $options, 'value1')->render()
);
});

it('can render a select element with a collection of options in groups', function () {
$options = [
'group' => collect([
'value1' => 'text1',
'value2' => 'text2',
]),
];

assertHtmlStringEqualsHtmlString(
'<select name="select" id="select">
<optgroup label="group">
<option value="value1">text1</option>
<option value="value2">text2</option>
</optgroup>
</select>',
$this->html->select('select', $options)->render()
);
});

it('can render a select element of a collection with options in groups', function () {
$options = collect([
'group' => collect([
'value1' => 'text1',
'value2' => 'text2',
]),
]);

assertHtmlStringEqualsHtmlString(
'<select name="select" id="select">
<optgroup label="group">
<option value="value1">text1</option>
<option value="value2">text2</option>
</optgroup>
</select>',
$this->html->select('select', $options)->render()
);
});

0 comments on commit ead179a

Please sign in to comment.