Skip to content

Commit b924773

Browse files
committed
Allow selection of both option types
1 parent 4805454 commit b924773

File tree

1 file changed

+48
-19
lines changed

1 file changed

+48
-19
lines changed

src/Concerns/HasSelectableValues.php

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@ protected static function resolveResourceModel(string $tableName): ?object
3434

3535
protected static function addValuesToInput(mixed $input, mixed $field, string $type, string $method): mixed
3636
{
37-
if (isset($field->config[$type]) && $field->config[$type] === 'relationship') {
38-
$options = [];
37+
$allOptions = [];
3938

40-
foreach ($field->config['relations'] as $relation) {
39+
// Handle relationship options
40+
if (isset($field->config[$type]) &&
41+
(is_string($field->config[$type]) && $field->config[$type] === 'relationship') ||
42+
(is_array($field->config[$type]) && in_array('relationship', $field->config[$type]))) {
43+
44+
$relationshipOptions = [];
45+
46+
foreach ($field->config['relations'] ?? [] as $relation) {
4147
if (! isset($relation['resource'])) {
4248
continue;
4349
}
@@ -71,17 +77,27 @@ protected static function addValuesToInput(mixed $input, mixed $field, string $t
7177
continue;
7278
}
7379

74-
$options[] = $opts;
80+
$relationshipOptions[] = $opts;
81+
}
82+
83+
if (! empty($relationshipOptions)) {
84+
$allOptions = array_merge($allOptions, ...$relationshipOptions);
7585
}
86+
}
7687

77-
if (! empty($options)) {
78-
$options = array_merge(...$options);
79-
$input->$method($options);
88+
// Handle array options
89+
if (isset($field->config[$type]) &&
90+
(is_string($field->config[$type]) && $field->config[$type] === 'array') ||
91+
(is_array($field->config[$type]) && in_array('array', $field->config[$type]))) {
92+
93+
if (isset($field->config['options']) && is_array($field->config['options'])) {
94+
$allOptions = array_merge($allOptions, $field->config['options']);
8095
}
8196
}
8297

83-
if (isset($field->config[$type]) && $field->config[$type] === 'array') {
84-
$input->$method($field->config['options']);
98+
// Apply all merged options to the input
99+
if (! empty($allOptions)) {
100+
$input->$method($allOptions);
85101
}
86102

87103
return $input;
@@ -107,21 +123,25 @@ protected function selectableValuesFormFields(string $type, string $label, strin
107123
->schema([
108124
Forms\Components\Grid::make(2)
109125
->schema([
110-
Forms\Components\Select::make("config.{$type}")
126+
Forms\Components\CheckboxList::make("config.{$type}")
111127
->options([
112128
'array' => __('Array'),
113129
'relationship' => __('Relationship'),
114130
])
115-
->searchable()
116-
->live(onBlur: true)
117-
->reactive()
118-
->label(__('Type')),
131+
->label(__('Type'))
132+
->live(),
119133
// Array options
120134
$arrayComponent::make('config.options')
121135
->label(__('Options'))
122136
->columnSpanFull()
123-
->visible(fn (Forms\Get $get): bool => $get("config.{$type}") == 'array')
124-
->required(fn (Forms\Get $get): bool => $get("config.{$type}") == 'array'),
137+
->visible(fn (Forms\Get $get): bool =>
138+
is_array($get("config.{$type}")) && in_array('array', $get("config.{$type}")) ||
139+
$get("config.{$type}") === 'array'
140+
)
141+
->required(fn (Forms\Get $get): bool =>
142+
is_array($get("config.{$type}")) && in_array('array', $get("config.{$type}")) ||
143+
$get("config.{$type}") === 'array'
144+
),
125145
// Relationship options
126146
Repeater::make('config.relations')
127147
->label(__('Relations'))
@@ -169,7 +189,10 @@ protected function selectableValuesFormFields(string $type, string $label, strin
169189
->toArray();
170190
})
171191
->noSearchResultsMessage(__('No types found'))
172-
->required(fn (Forms\Get $get): bool => $get("config.{$type}") == 'relationship'),
192+
->required(fn (Forms\Get $get): bool =>
193+
is_array($get("../../config.{$type}")) && in_array('relationship', $get("../../config.{$type}")) ||
194+
$get("../../config.{$type}") === 'relationship'
195+
),
173196
Forms\Components\Select::make('relationValue')
174197
->label(__('Column'))
175198
->helperText(__('The column to use as name for the options'))
@@ -180,7 +203,10 @@ protected function selectableValuesFormFields(string $type, string $label, strin
180203
Forms\Components\Hidden::make('relationKey')
181204
->default('ulid')
182205
->label(__('Key'))
183-
->required(fn (Forms\Get $get): bool => $get("config.{$type}") == 'relationship'),
206+
->required(fn (Forms\Get $get): bool =>
207+
is_array($get("../../config.{$type}")) && in_array('relationship', $get("../../config.{$type}")) ||
208+
$get("../../config.{$type}") === 'relationship'
209+
),
184210
Forms\Components\Repeater::make('relationValue_filters')
185211
->label(__('Filters'))
186212
->visible(fn (Forms\Get $get): bool => ! empty($get('resource')))
@@ -233,7 +259,10 @@ protected function selectableValuesFormFields(string $type, string $label, strin
233259
->columnSpanFull(),
234260
]),
235261
])
236-
->visible(fn (Forms\Get $get): bool => $get("config.{$type}") == 'relationship')
262+
->visible(fn (Forms\Get $get): bool =>
263+
is_array($get("config.{$type}")) && in_array('relationship', $get("config.{$type}")) ||
264+
$get("config.{$type}") === 'relationship'
265+
)
237266
->columnSpanFull(),
238267
]),
239268
]);

0 commit comments

Comments
 (0)