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

Added support Enum for Select field #2713

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions resources/views/fields/select.blade.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
@component($typeForm, get_defined_vars())
<div data-controller="select"
data-select-placeholder="{{$attributes['placeholder'] ?? ''}}"
data-select-allow-empty="{{ $allowEmpty }}"
data-select-message-notfound="{{ __('No results found') }}"
data-select-allow-add="{{ var_export($allowAdd, true) }}"
data-select-message-add="{{ __('Add') }}"
data-select-placeholder="{{$attributes['placeholder'] ?? ''}}"
data-select-allow-empty="{{ $allowEmpty }}"
data-select-message-notfound="{{ __('No results found') }}"
data-select-allow-add="{{ var_export($allowAdd, true) }}"
data-select-message-add="{{ __('Add') }}"
>

<select {{ $attributes }}>
@foreach($options as $key => $option)
<option value="{{$key}}"
@isset($value)
@if (is_array($value) && in_array($key, $value)) selected
@elseif (isset($value[$key]) && $value[$key] == $option) selected
@elseif ( !($value instanceof \UnitEnum) && isset($value[$key]) && $value[$key] == $option) selected
@elseif ( ($value instanceof \UnitEnum) && !$isOptionList && $key == $value->value) selected
@elseif (!$isOptionList && $key == $value) selected
@endif
@endisset
@endisset
>{{$option}}</option>
@endforeach
</select>
Expand Down
2 changes: 2 additions & 0 deletions tests/App/ExemplarServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\ServiceProvider;
use Orchid\Platform\Dashboard;
use Orchid\Tests\App\Fields\BaseSelectScreen;
use Orchid\Tests\App\Fields\SelectFromEnumFieldScreen;
use Orchid\Tests\App\Screens\AsyncHeaderButtonActionScreen;
use Orchid\Tests\App\Screens\ConfirmScreen;
use Orchid\Tests\App\Screens\DependentListenerModalScreen;
Expand Down Expand Up @@ -53,6 +54,7 @@ public function boot(Dashboard $dashboard, Router $router): void

// Fields
$route->screen('fields/base-select-screen', BaseSelectScreen::class)->name('base-select-screen');
$route->screen('fields/select-from-enum-field-screen', SelectFromEnumFieldScreen::class)->name('select-from-enum-field-screen');

//issue 2517
$route->screen('item/{parentId}/addChild', ItemAddChildScreen::class)->name('item.addchild');
Expand Down
37 changes: 37 additions & 0 deletions tests/App/Fields/SelectFromEnumFieldScreen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Orchid\Tests\App\Fields;

use Orchid\Screen\Fields\Select;
use Orchid\Tests\App\Role;
use Orchid\Tests\App\Screens\BaseFieldScreen;

class SelectFromEnumFieldScreen extends BaseFieldScreen
{
/**
* @return string[]
*/
public function query(): array
{
return [
'item' => Role::find(1),
];
}

/**
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function fields(): array
{
return [
Select::make('item.name')
->options([
'admin' => 'Admin',
'user' => 'User',
]),

];
}
}
53 changes: 0 additions & 53 deletions tests/Database/Factory/RoleFactory.php

This file was deleted.

34 changes: 34 additions & 0 deletions tests/Feature/Platform/SelectFromEnumFieldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Orchid\Tests\Feature\Platform;

use Orchid\Platform\Models\Role;
use Orchid\Tests\App\Enums\RoleNames;
use Orchid\Tests\App\Role as RoleWithEnum;
use Orchid\Tests\TestFeatureCase;

class SelectFromEnumFieldTest extends TestFeatureCase
{
/**
* @var RoleWithEnum
*/
private $role;

protected function setUp(): void
{
parent::setUp();
Role::factory()->create([
'name' => RoleNames::Admin->value,
]);
$this->role = RoleWithEnum::find(1);
}

public function testBase(): void
{
$this
->actingAs($this->createAdminUser())
->get(route('test.select-from-enum-field-screen'))
->assertSuccessful()
->assertSee(RoleNames::Admin->name);
}
}