Skip to content

Commit 22a21d4

Browse files
Merge pull request #153 from lotestudio/fix-131
Fix 131
2 parents 3be986a + ce48160 commit 22a21d4

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,12 @@ use CodeWithDennis\FilamentSelectTree\SelectTree;
210210
])
211211
])
212212
->query(function (Builder $query, array $data) {
213-
$categories = [(int) $data['category']];
214-
215-
return $query->when($data['category'], function (Builder $query, $categories) {
216-
if($data['category'] === -1){
217-
return $query->whereDoesntHave('categories');
213+
return $query->when($data['categories'], function (Builder $query, $categories) {
214+
if (collect($categories)->contains('-1')) {
215+
$query->whereDoesntHave('categories');
218216
}
219-
220-
return $query->whereHas('categories', fn(Builder $query) => $query->whereIn('id', $categories));
217+
return $query->orWhereHas('categories',
218+
fn(Builder $query) => $query->whereIn('id', $categories));
221219
});
222220
})
223221
])

resources/dist/filament-select-tree.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/js/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,21 @@ export default function selectTree({
2727
/** @type Treeselect */
2828
tree: null,
2929

30+
formatState: function (state) {
31+
if (Array.isArray(state)) {
32+
return (state ?? []).map((item) => item?.toString())
33+
}
34+
35+
return state?.toString()
36+
},
37+
38+
3039
init() {
3140
this.tree = new Treeselect({
3241
id: `tree-${name}-id`,
3342
ariaLabel: `tree-${name}-label`,
3443
parentHtmlContainer: this.$refs.tree,
35-
value: this.state,
44+
value: this.formatState(this.state),
3645
options,
3746
searchable,
3847
showCount,

src/SelectTree.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ private function buildNode($result, $resultMap, $disabledOptions, $hiddenOptions
233233
$node = [
234234
'name' => $result->{$this->getTitleAttribute()},
235235
'value' => $key,
236-
'parent' => $result->{$this->getParentAttribute()},
236+
'parent' => (string) $result->{$this->getParentAttribute()},
237237
'disabled' => in_array($key, $disabledOptions),
238238
'hidden' => in_array($key, $hiddenOptions),
239239
];
@@ -305,7 +305,13 @@ public function multiple(Closure|bool $multiple = true): static
305305

306306
public function prepend(Closure|array|null $prepend = null): static
307307
{
308-
$this->prepend = $prepend;
308+
$this->prepend = $this->evaluate($prepend);
309+
310+
if (is_array($this->prepend) && isset($this->prepend['name'], $this->prepend['value'])) {
311+
$this->prepend['value'] = (string) $this->prepend['value'];
312+
} else {
313+
throw new \InvalidArgumentException('The provided prepend value must be an array with "name" and "value" keys.');
314+
}
309315

310316
return $this;
311317
}
@@ -445,9 +451,11 @@ public function getIndependent(): bool
445451
return $this->evaluate($this->independent);
446452
}
447453

448-
public function getCustomKey($record)
454+
public function getCustomKey($record): string
449455
{
450-
return is_null($this->customKey) ? $record->getKey() : $record->{$this->customKey};
456+
$key = is_null($this->customKey) ? $record->getKey() : $record->{$this->customKey};
457+
458+
return (string) $key;
451459
}
452460

453461
public function getWithCount(): bool

0 commit comments

Comments
 (0)