Skip to content

Commit

Permalink
Remove all Form::select calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ilestis committed Jun 5, 2024
1 parent 9c68f62 commit d3ab390
Show file tree
Hide file tree
Showing 98 changed files with 466 additions and 356 deletions.
2 changes: 1 addition & 1 deletion app/Services/PaginationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function disabled(): array
{
$disabled = [];

if (!auth()->user()->isSubscriber()) {
if (auth()->user()->isSubscriber()) {
$disabled[self::MAX] = ['disabled' => true];
}

Expand Down
72 changes: 72 additions & 0 deletions app/View/Components/Forms/Select.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace App\View\Components\Forms;

use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
use Illuminate\View\Component;

class Select extends Component
{
/**
* Create a new component instance.
*/
public function __construct(
public string $name,
public array|Collection $options,
public array|Collection $optionAttributes = [],
public string $id = '',
public bool $required = false,
public bool $multiple = false,
public string $class = '',
public string $label = '',
public string $placeholder = '',
public mixed $selected = null,
public array $extra = [],
) {
}

/**
* Get the view / contents that represent the component.
*/
public function render(): View|Closure|string
{
// Form submitted? Re-load the value
$old = old($this->name);
if (!is_null($old)) {
dd('a');
$this->selected = $old;

Check failure on line 39 in app/View/Components/Forms/Select.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Unreachable statement - code above always terminates.
} elseif (!empty($this->placeholder)) {

$this->selected = '';
$this->options = ['' => $this->placeholder] + $this->options;
}
return view('components.forms.select');
}

public function fieldId(): string
{
return !empty($this->id) ? $this->id : uniqid($this->name);
}

public function isSelected(mixed $value): bool
{
if (empty($this->selected)) {
return empty($value);
}
if (is_array($this->selected)) {
return in_array($value, $this->selected);
}
if (is_int($this->selected)) {
return $value == $this->selected;
}
// Always force values to lower to avoid thinking
return mb_strtolower($value) == mb_strtolower($this->selected);
}

public function optionAttributes(string $key): array
{
return $this->optionAttributes[$key] ?? [];
}
}
2 changes: 1 addition & 1 deletion lang/en/campaigns.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@
],
'sharing' => [
'filters' => 'Public campaigns are visible on the :public-campaigns page. Filling out these fields makes it easier for others to discover it.',
'language' => 'The language in which the campaign\'s content is written.',
'language' => 'The main language in which the campaign\'s content is written.',
'system' => 'If playing a TTRPG, the system used to play in the campaign.',
],
'show' => [
Expand Down
2 changes: 1 addition & 1 deletion lang/en/profiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
'success' => 'Theme changed.',
'themes' => [
'dark' => 'Dark',
'default' => 'Default',
'default' => 'Light',
'future' => 'Future',
'midnight' => 'Midnight Blue',
],
Expand Down
20 changes: 18 additions & 2 deletions resources/js/components/select2.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,25 @@ $(document).ready(function() {
return;
}

// Check it isn't the select2-icon
let url = $(this).data('url');
let allowClear = $(this).data('allow-clear');
let dropdownParent = $(this).data('dropdown-parent');

if (!url) {
$(this).select2({
tags: false,
placeholder: $(this).data('placeholder'),
allowClear: allowClear ?? false,
//tags: $(this).data('tags') || false,
language: $(this).data('language'),
minimumInputLength: 0,
dropdownParent: dropdownParent || '',
width: '100%',
});
return;
}

// Check it isn't the select2-icon
$(this).select2({
tags: false,
placeholder: $(this).data('placeholder'),
Expand All @@ -36,7 +52,7 @@ $(document).ready(function() {
ajax: {
delay: 500,
quietMillis: 500,
url: $(this).data('url'),
url: url,
dataType: 'json',
data: function (params) {
return {
Expand Down
2 changes: 1 addition & 1 deletion resources/views/auth/register.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<div class="mb-3 has-feedback{{ $errors->has('name') ? ' has-error' : '' }}">
<div class="flex items-stretch w-full">
<input id="name" type="text" class="rounded border p-2 w-full dark:bg-slate-800 dark:border-slate-500" name="name" value="{{ old('name') }}" placeholder="{{ __('auth.register.fields.name') }}" required autofocus>
<input id="name" type="text" class="rounded border p-2 w-full dark:bg-slate-800 dark:border-slate-500" name="name" value="{!! old('name') !!}" placeholder="{{ __('auth.register.fields.name') }}" required autofocus>
</div>

@if ($errors->has('name'))
Expand Down
2 changes: 1 addition & 1 deletion resources/views/billing/payment-method.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
@include('settings.subscription.currency._blocked')
@else
<x-forms.field field="currency" :label="__('settings.subscription.fields.currency')">
{!! Form::select('currency', $currencies, auth()->user()->currency(), []) !!}
<x-forms.select name="currency" :options="$currencies" :selected="auth()->user()->currency()" />
</x-forms.field>

<div class="text-right">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/bookmarks/form/_dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@if($campaign->boosted())
<x-grid>
<x-forms.field field="dashboard" :label="__('bookmarks.fields.dashboard')">
{!! Form::select('dashboard_id', $dashboards, FormCopy::field('dashboard_id')->string(), ['class' => '']) !!}
<x-forms.select name="dashboard_id" :options="$dashboards" :selected="$source->dashboard_id ?? $model->dashboard_id ?? null" />
</x-forms.field>

<input type="hidden" name="options[default_dashboard]" value="0" />
Expand Down
2 changes: 1 addition & 1 deletion resources/views/bookmarks/form/_entity.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
])

<x-forms.field field="menu" :label="__('bookmarks.fields.menu')">
{!! Form::select('menu', $menus, null, ['class' => '', 'id' => 'entity-selector']) !!}
<x-forms.select name="menu" id="entity-selector" :options="$menus" :selected="$model->menu ?? null" />
</x-forms.field>

<x-forms.field field="filter" :label="__('bookmarks.fields.filters')" :hidden="true">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/bookmarks/form/_entry.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
:tooltip="true"
:helper="__('entities/links.helpers.parent')">
@if ($campaign->boosted())
{{ Form::select('parent', $sidebar->campaign($campaign)->availableParents(), (empty($model) || empty($model->parent) ? 'bookmarks' : $model->parent), ['class' => '']) }}
<x-forms.select name="parent" :options="$sidebar->campaign($campaign)->availableParents()" :selected="$model->parent ?? 'bookmarks'" />

<p class="text-neutral-content md:hidden">
{!! __('entities/links.helpers.parent') !!}
Expand Down
2 changes: 1 addition & 1 deletion resources/views/bookmarks/form/_random.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<x-grid>
<x-forms.field field="random-type" :label="__('bookmarks.fields.random_type')">
{!! Form::select('random_entity_type', $entityTypes, FormCopy::field('random_entity_type')->string(), ['class' => '']) !!}
<x-forms.select name="random_entity_type" :options="$entityTypes" :selected="$source->random_entity_type ?? $model->random_entity_type ?? null" />
</x-forms.field>

<div>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/bookmarks/form/_type.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<x-grid>
<x-forms.field field="type" :label="__('crud.fields.type')">
{!! Form::select('type', $entityTypes, FormCopy::field('type')->string(), ['class' => '']) !!}
<x-forms.select name="type" :options="$entityTypes" :selected="$source->type ?? $model->type ?? null" />
</x-forms.field>

<x-forms.field field="filters" :label="__('bookmarks.fields.filters')">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/calendars/events/_form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<x-forms.field
field="name"
:label="__('crud.fields.name')">
<input type="text" name="name" placeholder="{{ __('crud.placeholders.name') }}" maxlength="191" value="{{ old('name', $source->name ?? $model->name ?? null) }}" />
<input type="text" name="name" placeholder="{{ __('crud.placeholders.name') }}" maxlength="191" value="{!! old('name', $source->name ?? $model->name ?? null) !!}" />
</x-forms.field>
</div>
<div class="self-start">
Expand Down
8 changes: 4 additions & 4 deletions resources/views/calendars/events/_subform.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<x-forms.field
field="month"
:label="__('calendars.fields.month')">
{!! Form::select('month', isset($calendar) ? $calendar->monthList() : [], (!empty($month) ? $month : null), []) !!}
<x-forms.select name="month" :options="$calendar->monthList() ?? []" :selected="$month ?? null" />
</x-forms.field>

<x-forms.field
Expand Down Expand Up @@ -39,7 +39,7 @@
<x-forms.field
field="recurring"
:label="__('calendars.fields.is_recurring')">
{!! Form::select('recurring_periodicity', (isset($calendar) ? $calendar->recurringOptions() : []), (isset($entityEvent) && $entityEvent->is_recurring ? $entityEvent->recurring_periodicity : ''), ['class' => 'reminder-periodicity']) !!}
<x-forms.select name="recurring_periodicity" :options="$calendar->recurringOptions() ?? []" :selected="$entityEvent->recurring_periodicity ?? null" class="w-full reminder-periodicity" />
</x-forms.field>

<x-forms.field field="recurring-until" :hidden="!isset($entityEvent) || !$entityEvent->is_recurring" :label="__('calendars.fields.recurring_until')" id="add_event_recurring_until">
Expand All @@ -52,15 +52,15 @@
field="type"
:label="__('entities/events.fields.type')"
:helper=" __('entities/events.helpers.characters', ['more' => link_to('https://docs.kanka.io/en/latest/advanced/age.html', __('crud.actions.find_out_more'), null, ['target' => '_blank'])])">
{!! Form::select('type_id', [null => '', 2 => __('entities/events.types.birth'), 3 => __('entities/events.types.death')], (isset($entityEvent) ? $entityEvent->type_id : null), []) !!}
<x-forms.select name="type_id" :options="[null => '', 2 => __('entities/events.types.birth'), 3 => __('entities/events.types.death')]" :selected="$entityEvent->type_id ?? null" />
</x-forms.field>
@endif
@if (!empty($entity) && in_array($entity->typeId(), [config('entities.ids.location'), config('entities.ids.family'), config('entities.ids.organisation')]))
<x-forms.field
field="type"
:label="__('entities/events.fields.type')"
:helper="__('entities/events.helpers.founding', ['type' => '<code>' . __('entities/events.types.founded') . '</code>'])">
{!! Form::select('type_id', [null => '', 5 => __('entities/events.types.founded')], (isset($entityEvent) ? $entityEvent->type_id : null), []) !!}
<x-forms.select name="type_id" :options="[null => '', 5 => __('entities/events.types.founded')]" :selected="$entityEvent->type_id ?? null" />
</x-forms.field>
@endif
</x-grid>
Expand Down
4 changes: 2 additions & 2 deletions resources/views/calendars/form/_calendar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
:label="__('calendars.fields.reset')"
:tooltip="true"
:helper="__('calendars.hints.reset')">
{!! Form::select('reset', __('calendars.options.resets')) !!}
<x-forms.select name="reset" :options="__('calendars.options.resets')" :selected="$source->reset ?? $model->reset ?? null" />
</x-forms.field>

<input type="hidden" name="calendar_id" value="" />
Expand All @@ -33,7 +33,7 @@
:label="__('calendars.fields.default_layout')"
:tooltip="true"
:helper="__('calendars.helpers.default_layout')">
{!! Form::select('parameters[layout]', ['' => __('calendars.layouts.monthly'), 'yearly' => __('calendars.layouts.yearly')])!!}
<x-forms.select name="parameters[layout]" :options="['' => __('calendars.layouts.monthly'), 'yearly' => __('calendars.layouts.yearly')]" :selected="$source->parameters['layout'] ?? $model->parameters['layout'] ?? null" />
</x-forms.field>

@include('cruds.fields.format')
Expand Down
15 changes: 8 additions & 7 deletions resources/views/calendars/form/_months.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
$months = $model->months();
} elseif (isset($source)) {
$months = $source->months();
}?>
}
$monthTypes = [
'intercalary' => __('calendars.month_types.intercalary'),
'standard' => __('calendars.month_types.standard'),
];
?>
<div class="flex flex-col gap-2 calendar-months sortable-elements" data-handle=".sortable-handler">
<div class="grid gap-2 grid-cols-2 md:grid-cols-4 md:gap-4">
<div class="">{{ __('calendars.parameters.month.name') }}</div>
Expand Down Expand Up @@ -66,10 +71,7 @@
<div class="flex items-center gap-2">
<div class="field">
<label class="sr-only">{{ __('calendars.parameters.month.type') }}</label>
{!! Form::select('month_type[]', __('calendars.month_types'), (!empty($month['type']) ? $month['type'] : 'standard'), [
'class' => 'w-full',
'aria-label' => __('calendars.parameters.month.type'),
]) !!}
<x-forms.select name="month_type[]" :options="$monthTypes" :selected="\Illuminate\Support\Arr::get($month, 'type', 'standard')" class="w-full" :label="__('calendars.parameters.month.type')" />
</div>
<div>
<span class="dynamic-row-delete btn2 btn-error btn-outline btn-sm" data-remove="4" title="{{ __('crud.remove') }}">
Expand Down Expand Up @@ -107,8 +109,7 @@
<div class="flex gap-2 items-center">
<div class="field">
<label class="sr-only">{{ __('calendars.parameters.month.type') }}</label>
{!! Form::select('month_type[]', __('calendars.month_types'), 'standard', ['class' => 'w-full',
'aria-label' => __('calendars.parameters.month.type'),]) !!}
<x-forms.select name="month_type[]" :options="$monthTypes" selected="standard" :label="__('calendars.parameters.month.type')" />
</div>
<div class="">
<span class="dynamic-row-delete btn2 btn-error btn-outline btn-sm" data-remove="4" title="{{ __('crud.remove') }}">
Expand Down
5 changes: 3 additions & 2 deletions resources/views/calendars/form/_moons.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
<div class="grid grid-cols-2 gap-2">
<div class="field w-full">
<label class="sr-only">{{ __('crud.fields.colour') }}</label>
{!! Form::select('moon_colour[]', \App\Facades\FormCopy::colours(false), \Illuminate\Support\Arr::get($fullmoon, 'colour', 'grey'), ['class' => 'select2-colour', 'style' => 'width: 100%', 'aria-label' => __('crud.fields.colour')]) !!}

<x-forms.select name="moon_colour[]" :options="FormCopy::colours(false)" :selected="\Illuminate\Support\Arr::get($fullmoon, 'colour', 'grey')" class="w-full select2-colour" :label="__('crud.fields.colour')"/>
</div>
<div class="flex gap-2 items-center">
<div class="grow field">
Expand Down Expand Up @@ -105,7 +106,7 @@
<div class="grid grid-cols-2 gap-2">
<div class="field w-full">
<label class="sr-only">{{ __('crud.fields.colour') }}</label>
{!! Form::select('moon_colour[]', \App\Facades\FormCopy::colours(false), 'grey', ['class' => 'w-full select2-colour', 'style' => 'width: 100%', 'aria-label' => __('crud.fields.colour')]) !!}
<x-forms.select name="moon_colour[]" :options="FormCopy::colours(false)" selected="grey" class="w-full select2-colour" :label="__('crud.fields.colour')"/>
</div>
<div class="flex gap-2 items-center">
<div class="grow field">
Expand Down
4 changes: 2 additions & 2 deletions resources/views/calendars/weather/_form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
css="col-span-2"
:required="true"
:label="__('calendars/weather.fields.weather')">
{!! Form::select('weather', __('calendars/weather.options.weather'), null, ['class' => 'w-full']) !!}
<x-forms.select name="weather" :options="__('calendars/weather.options.weather')" :selected="$model->weather ?? null" />
</x-forms.field>

<x-forms.field
field="name"
css="col-span-2"
:label="__('calendars/weather.fields.name')">
<input type="text" name="name" placeholder="{{ __('calendars/weather.placeholders.name') }}" maxlength="40" value="{{ old('name', $source->name ?? $weather->name ?? null) }}" />
<input type="text" name="name" placeholder="{{ __('calendars/weather.placeholders.name') }}" maxlength="40" value="{!! old('name', $source->name ?? $weather->name ?? null) !!}" />
</x-forms.field>

<x-forms.field
Expand Down
3 changes: 2 additions & 1 deletion resources/views/campaigns/default-images/_form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
field="entity-type"
:required="true"
:label="__('crud.fields.entity_type')">
{!! Form::select('entity_type', $entities, [], ['class' => 'w-full']) !!}

<x-forms.select name="entity_type" :options="$entities" class="w-full" />
</x-forms.field>

<x-forms.field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
<x-forms.field
field="public"
:label="__('campaigns.fields.public')">
{!! Form::select('is_public', [0 => __('campaigns.visibilities.private'), 1 => __('campaigns.visibilities.public')], null, ['class' => 'w-full']) !!}
<x-forms.select name="is_public" :options="[0 => __('campaigns.visibilities.private'), 1 => __('campaigns.visibilities.public')]" :selected="$campaign->is_public ?? null" />
</x-forms.field>
</x-grid>
2 changes: 1 addition & 1 deletion resources/views/campaigns/forms/panes/entry.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
:required="true"
:label="__('campaigns.fields.name')"
:helper="__('campaigns.helpers.name')">
<input type="text" name="name" placeholder="{{ __('campaigns.placeholders.name') }}" maxlength="191" required value="{{ old('name', $model->name ?? null) }}" />
<input type="text" name="name" placeholder="{{ __('campaigns.placeholders.name') }}" maxlength="191" required value="{!! old('name', $model->name ?? null) !!}" />
</x-forms.field>

<x-forms.field
Expand Down
6 changes: 3 additions & 3 deletions resources/views/campaigns/forms/panes/permission.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@
field="privacy"
:label="__('campaigns.fields.entity_privacy')"
:helper="__('campaigns.helpers.entity_privacy')">
{!! Form::select('entity_visibility', [0 => __('campaigns.privacy.visible'), 1 => __('campaigns.privacy.private')], null, ['class' => '']) !!}
<x-forms.select name="entity_visibility" :options="[0 => __('campaigns.privacy.visible'), 1 => __('campaigns.privacy.private')]" :selected="$campaign->entity_visibility ?? null" />
</x-forms.field>

<x-forms.field
field="related-visibility"
:label="__('campaigns.fields.related_visibility')"
:helper="__('campaigns.helpers.related_visibility')">
{!! Form::select('settings[default_visibility]', $visibilities, null, ['class' => '']) !!}
<x-forms.select name="settings[default_visibility]" :options="$visibilities" :selected="$campaign->settings['default_visibility'] ?? null" />
</x-forms.field>

<x-forms.field
field="character_personality_visibility"
:label="__('campaigns.fields.character_personality_visibility')"
:helper="__('campaigns.helpers.character_personality_visibility')">
{!! Form::select('entity_personality_visibility', [0 => __('campaigns.privacy.visible'), 1 => __('campaigns.privacy.private')], null, ['class' => '']) !!}
<x-forms.select name="entity_personality_visibility" :options="[0 => __('campaigns.privacy.visible'), 1 => __('campaigns.privacy.private')]" :selected="$campaign->entity_personality_visibility ?? null" />
</x-forms.field>
</x-grid>
</x-grid>
Expand Down
Loading

0 comments on commit d3ab390

Please sign in to comment.