Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add select component #52

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions config/blade-ui-kit.php
Expand Up @@ -37,6 +37,7 @@
'markdown' => Components\Markdown\Markdown::class,
'password' => Components\Forms\Inputs\Password::class,
'pikaday' => Components\Forms\Inputs\Pikaday::class,
'select' => Components\Forms\Inputs\Select::class,
'social-meta' => Components\Layouts\SocialMeta::class,
'textarea' => Components\Forms\Inputs\Textarea::class,
'toc' => Components\Markdown\ToC::class,
Expand Down
19 changes: 19 additions & 0 deletions resources/views/components/forms/inputs/select.blade.php
@@ -0,0 +1,19 @@
<select
name="{{ $name }}"
id="{{ $id }}"
{{ $attributes }}
>
@if(!$slot->isEmpty())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@if(!$slot->isEmpty())
@if(! $slot->isEmpty())

{{ $slot }}
@else
@if($placeholder)
<option value="" hidden>{{ $placeholder }}</option>
@endif
@foreach($options as $value => $label)
<option
value="{{ $value }}"
{!! $isSelected($value) ? 'selected' : '' !!}
>{{ $label }}</option>
@endforeach
@endif
</select>
70 changes: 70 additions & 0 deletions src/Components/Forms/Inputs/Select.php
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace BladeUIKit\Components\Forms\Inputs;

use BladeUIKit\Components\BladeComponent;
use http\Exception\InvalidArgumentException;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;

class Select extends BladeComponent
{
/** @var string */
public $name;

/** @var string|null */
public $id;

/** @var array|Collection */
public $options;

/** @var string */
public $nameAttribute;

/** @var string */
public $valueAttribute;

/** @var string|array|null */
public $selected;

/** @var string */
public $placeholder;

public function __construct(string $name, $options, string $placeholder = '', string $nameAttribute = 'name', string $valueAttribute = 'id', $id = null, $selected = null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that a more appropriate name for $nameAttribute is probably $labelAttribute - each option has a (display) label and value. Thoughts?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$labelAttribute suits better imo

{
$this->name = $name;
$this->id = $id ?? $name;
$this->nameAttribute = $nameAttribute;
$this->valueAttribute = $valueAttribute;
$this->selected = $selected;
$this->placeholder = $placeholder;

if ($options instanceof Collection) {
$this->options = $options->mapWithKeys(function ($option) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can refactor this mapWithKeys() expression to use pluck()

Suggested change
$this->options = $options->mapWithKeys(function ($option) {
$this->options = $options->pluck($this->nameAttribute, $this->valueAttribute)

return [$option->{$this->valueAttribute} => $option->{$this->nameAttribute}];
});
} elseif (is_array($options)) {
$this->options = $options;
} else {
throw new InvalidArgumentException('Invalid options');
}
}

public function isSelected($value): bool
{
$selected = old($this->name, $this->selected);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of the old() call here?


if (is_array($selected)) {
return in_array($value, $selected);
}

return ($value === $selected);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return ($value === $selected);
return $value === $selected;

}

public function render(): View
{
return view('blade-ui-kit::components.forms.inputs.select');
}
}
85 changes: 85 additions & 0 deletions tests/Components/Forms/Inputs/SelectTest.php
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Tests\Components\Forms\Inputs;

use Tests\Components\ComponentTestCase;

class SelectTest extends ComponentTestCase
{
/** @test */
public function the_component_can_be_rendered()
{
$template = <<<HTML
<select name="select" id="select">
<option value="0">Test</option>
</select>
HTML;
$this->assertComponentRenders(
$template,
'<x-select name="select" :options="[\'Test\']" />'
);
}

/** @test */
public function selected_attribute_test()
{
$template = <<<HTML
<select name="select" id="select">
<option value="test">Test</option>
<option value="test2" selected>Test2</option>
</select>
HTML;
$this->assertComponentRenders(
$template,
'<x-select name="select" :options="[\'test\' => \'Test\', \'test2\' => \'Test2\']" selected="test2" />'
);
}

/** @test */
public function specific_attributes_can_be_overwritten()
{
$template = <<<HTML
<select name="select" id="select" class="p-4">
<option value="0">Test</option>
</select>
HTML;
$this->assertComponentRenders(
$template,
'<x-select name="select" :options="[\'Test\']" class="p-4" />'
);
}

public function placeholder_attribute_test()
{
$template = <<<HTML
<select name="select" id="select">
<option value="" hidden>Placeholder</option>
<option value="0">Test</option>
</select>
HTML;
$this->assertComponentRenders(
$template,
'<x-select name="select" :options="[\'Test\']" placeholder="Placeholder" />'
);
}

/** @test */
public function inputs_can_have_old_values()
{
$this->flashOld(['select' => 'test2']);

$template = <<<HTML
<select name="select" id="select">
<option value="test">Test</option>
<option value="test2" selected>Test2</option>
</select>
HTML;

$this->assertComponentRenders(
$template,
'<x-select name="select" :options="[\'test\' => \'Test\', \'test2\' => \'Test2\']" />'
);
}
}