Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

Commit a8e30ae

Browse files
committed
Make timemzone select compatible with new custom select
1 parent 695e6dd commit a8e30ae

File tree

3 files changed

+79
-26
lines changed

3 files changed

+79
-26
lines changed
Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
11
<x-form-components::inputs.custom-select
2-
:filterable="$filterable"
2+
:name="$name"
3+
:id="$id"
4+
:value="$value"
5+
:show-errors="$showErrors"
6+
:searchable="$searchable"
37
:optional="$optional"
48
:placeholder="$placeholder"
59
:multiple="$multiple"
6-
:max-width="$maxWidth"
7-
:options="$optionsForCustomSelect()"
8-
:container-class="$containerClass"
10+
:disabled="$disabled"
11+
:autofocus="$autofocus"
12+
:min-selected="$minSelected"
13+
:max-selected="$maxSelected"
14+
:show-checkbox="$showCheckbox"
15+
:clear-icon="$clearIcon"
916
:extra-attributes="$extraAttributes"
1017
{{ $attributes }}
11-
/>
18+
>
19+
@foreach (app('fc-timezone')->only($only)->all() as $region => $regionTimezones)
20+
<x-form-components::inputs.custom-select-option
21+
label="{{ $region }}"
22+
is-opt-group
23+
/>
24+
25+
@foreach ($regionTimezones as $id => $name)
26+
<x-form-components::inputs.custom-select-option
27+
value="{{ $id }}"
28+
label="{{ $name }}"
29+
/>
30+
@endforeach
31+
@endforeach
32+
</x-form-components::inputs.custom-select>

resources/views/partials/timezone-select-native.blade.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<select @if ($name) name="{{ $name }}" @endif
55
@if ($id) id="{{ $id }}" @endif
66
@if ($multiple) multiple @endif
7+
@if ($disabled) disabled @endif
8+
@if ($autofocus) autofocus @endif
79

810
{!! $ariaDescribedBy() !!}
911
@if ($hasErrorsAndShow($name))

src/Components/Inputs/TimezoneSelect.php

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,34 @@
66

77
class TimezoneSelect extends Select
88
{
9-
public null|string $placeholder;
10-
119
public function __construct(
12-
public null | string $name = null,
13-
public null | string $id = null,
10+
public null|string $name = null,
11+
public null|string $id = null,
1412
public mixed $value = null,
1513
public bool $multiple = false,
16-
public null | string $maxWidth = null,
14+
public null|string $maxWidth = null, // Native only
1715
bool $showErrors = true,
18-
$leadingAddon = false,
19-
$inlineAddon = false,
20-
$inlineAddonPadding = self::DEFAULT_INLINE_ADDON_PADDING,
21-
$leadingIcon = false,
22-
$trailingAddon = false,
23-
$trailingAddonPadding = self::DEFAULT_TRAILING_ADDON_PADDING,
24-
$trailingIcon = false,
25-
public bool | array | string | null $only = null,
16+
$leadingAddon = false, // Native only
17+
$inlineAddon = false, // Native only
18+
$inlineAddonPadding = self::DEFAULT_INLINE_ADDON_PADDING, // Native only
19+
$leadingIcon = false, // Native only
20+
$trailingAddon = false, // Native only
21+
$trailingAddonPadding = self::DEFAULT_TRAILING_ADDON_PADDING, // Native only
22+
$trailingIcon = false, // Native only
23+
public bool|array|string|null $only = null,
2624
public bool $useCustomSelect = false,
27-
public bool $filterable = true,
25+
public bool $searchable = true,
2826
public bool $optional = false,
29-
null|string $placeholder = 'form-components::messages.timezone_select_placeholder',
30-
public null | string $containerClass = null,
27+
public bool|null|string $placeholder = null,
28+
public null | string $containerClass = null, // Native only
3129
public $extraAttributes = '',
32-
public $after = null,
30+
public $after = null, // Native only
31+
public int $minSelected = 1,
32+
public null|int $maxSelected = null,
33+
public bool $disabled = false,
34+
public null|string $clearIcon = null,
35+
public null|bool $showCheckbox = null,
36+
public bool $autofocus = false,
3337
) {
3438
parent::__construct(
3539
name: $name,
@@ -50,19 +54,45 @@ public function __construct(
5054
);
5155

5256
$this->only = is_null($only) ? config('form-components.timezone_subset', false) : $only;
53-
$this->placeholder = __($placeholder);
57+
58+
$this->resolveLang();
5459
}
5560

5661
public function optionsForCustomSelect(): array
5762
{
58-
return collect(app('fc-timezone')->only($this->only)->all())
63+
$groups = app('fc-timezone')->only($this->only)->all();
64+
$options = [];
65+
66+
foreach ($groups as $regionName => $timezones) {
67+
$options[] = [
68+
'name' => $regionName,
69+
'is_opt_group' => true,
70+
];
71+
72+
$options = array_merge($options, collect($timezones)->map(fn ($id, $name) => compact('id', 'name'))->values()->toArray());
73+
}
74+
75+
return $options;
76+
77+
$opts = collect(app('fc-timezone')->only($this->only)->all())
5978
->map(function (array $timezones, string $region) {
6079
return [
61-
'label' => $region,
62-
'options' => collect($timezones)->map(fn (string $text, string $value) => compact('value', 'text'))->values()->toArray(),
80+
'name' => $region,
81+
'is_opt_group' => true,
82+
'options' => collect($timezones)->map(fn (string $name, string $id) => compact('id', 'name'))->values()->toArray(),
6383
];
6484
})
6585
->values()
86+
->flatten(1)
6687
->toArray();
88+
89+
return $opts;
90+
}
91+
92+
protected function resolveLang(): void
93+
{
94+
if ($this->placeholder !== false) {
95+
$this->placeholder = $this->placeholder ?? __('form-components::messages.timezone_select_placeholder');
96+
}
6797
}
6898
}

0 commit comments

Comments
 (0)