Skip to content

Commit 381fe14

Browse files
committed
wip
1 parent e6f19b3 commit 381fe14

File tree

3 files changed

+93
-6
lines changed

3 files changed

+93
-6
lines changed

src/Fields/OptGroup.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cone\Root\Fields;
6+
7+
use Closure;
8+
use Cone\Root\Traits\HasAttributes;
9+
use Cone\Root\Traits\Makeable;
10+
use Illuminate\Contracts\Support\Arrayable;
11+
use Illuminate\Support\Traits\Conditionable;
12+
use JsonSerializable;
13+
14+
class OptGroup implements Arrayable, JsonSerializable
15+
{
16+
use Conditionable;
17+
use HasAttributes;
18+
use Makeable;
19+
20+
/**
21+
* The options within the opt group.
22+
*
23+
* @var list<Option>
24+
*/
25+
protected array $options = [];
26+
27+
/**
28+
* Create a new opt group instance.
29+
*/
30+
public function __construct(protected string $label, array $options)
31+
{
32+
$this->options = array_map(function (Option|int|string $item, int|string $key): Option {
33+
return match (true) {
34+
$item instanceof Option => $item,
35+
default => new Option((string) $key, (string) $item),
36+
};
37+
}, $options, array_keys($options));
38+
}
39+
40+
/**
41+
* Set the "disabled" HTML attribute.
42+
*/
43+
public function disabled(bool $value = true): static
44+
{
45+
return $this->setAttribute('disabled', $value);
46+
}
47+
48+
/**
49+
* Set the "selected" HTML attribute.
50+
*/
51+
public function selected(bool|Closure $value = true): static
52+
{
53+
foreach ($this->options as $option) {
54+
$option->selected($value);
55+
}
56+
57+
return $this;
58+
}
59+
60+
/**
61+
* Convert the element to a JSON serializable format.
62+
*/
63+
public function jsonSerialize(): array
64+
{
65+
return $this->toArray();
66+
}
67+
68+
/**
69+
* Get the array representation of the object.
70+
*/
71+
public function toArray(): array
72+
{
73+
return [
74+
'attrs' => $this->newAttributeBag(),
75+
'label' => $this->label,
76+
'options' => array_map(fn (Option $option): array => $option->toArray(), $this->options),
77+
];
78+
}
79+
}

src/Fields/Option.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Cone\Root\Fields;
66

7+
use Closure;
78
use Cone\Root\Traits\HasAttributes;
89
use Cone\Root\Traits\Makeable;
910
use Illuminate\Contracts\Support\Arrayable;
@@ -44,8 +45,10 @@ public function disabled(bool $value = true): static
4445
/**
4546
* Set the "selected" HTML attribute.
4647
*/
47-
public function selected(bool $value = true): static
48+
public function selected(bool|Closure $value = true): static
4849
{
50+
$value = $value instanceof Closure ? call_user_func_array($value, [$this]) : $value;
51+
4952
return $this->setAttribute('selected', $value);
5053
}
5154

src/Fields/Select.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,18 @@ public function resolveOptions(Request $request, Model $model): array
120120

121121
$value = Arr::wrap($this->resolveValue($request, $model));
122122

123+
$value = array_map(fn (mixed $v): mixed => enum_value($v), $value);
124+
123125
return array_map(function (mixed $label, mixed $option) use ($value): array {
124-
$option = $label instanceof Option ? $label : $this->newOption($option, $label);
126+
$option = match (true) {
127+
$option instanceof Option => $option,
128+
is_array($label) => new OptGroup((string) $option, (array) $label),
129+
default => $this->newOption($option, $label),
130+
};
125131

126-
$option->selected(in_array(
127-
$option->getAttribute('value'),
128-
array_map(fn (mixed $v): mixed => enum_value($v), $value)
129-
));
132+
$option->selected(static function (Option $option) use ($value): bool {
133+
return in_array($option->getAttribute('value'), $value);
134+
});
130135

131136
return $option->toArray();
132137
}, $options, array_keys($options));

0 commit comments

Comments
 (0)