Skip to content

Fix 131 #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,12 @@ use CodeWithDennis\FilamentSelectTree\SelectTree;
])
])
->query(function (Builder $query, array $data) {
$categories = [(int) $data['category']];

return $query->when($data['category'], function (Builder $query, $categories) {
if($data['category'] === -1){
return $query->whereDoesntHave('categories');
return $query->when($data['categories'], function (Builder $query, $categories) {
if (collect($categories)->contains('-1')) {
$query->whereDoesntHave('categories');
}

return $query->whereHas('categories', fn(Builder $query) => $query->whereIn('id', $categories));
return $query->orWhereHas('categories',
fn(Builder $query) => $query->whereIn('id', $categories));
});
})
])
Expand Down
2 changes: 1 addition & 1 deletion resources/dist/filament-select-tree.js

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion resources/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,21 @@ export default function selectTree({
/** @type Treeselect */
tree: null,

formatState: function (state) {
if (Array.isArray(state)) {
return (state ?? []).map((item) => item?.toString())
}

return state?.toString()
},


init() {
this.tree = new Treeselect({
id: `tree-${name}-id`,
ariaLabel: `tree-${name}-label`,
parentHtmlContainer: this.$refs.tree,
value: this.state,
value: this.formatState(this.state),
options,
searchable,
showCount,
Expand Down
16 changes: 12 additions & 4 deletions src/SelectTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private function buildNode($result, $resultMap, $disabledOptions, $hiddenOptions
$node = [
'name' => $result->{$this->getTitleAttribute()},
'value' => $key,
'parent' => $result->{$this->getParentAttribute()},
'parent' => (string) $result->{$this->getParentAttribute()},
'disabled' => in_array($key, $disabledOptions),
'hidden' => in_array($key, $hiddenOptions),
];
Expand Down Expand Up @@ -301,7 +301,13 @@ public function multiple(Closure|bool $multiple = true): static

public function prepend(Closure|array|null $prepend = null): static
{
$this->prepend = $prepend;
$this->prepend = $this->evaluate($prepend);

if (is_array($this->prepend) && isset($this->prepend['name'], $this->prepend['value'])) {
$this->prepend['value'] = (string) $this->prepend['value'];
} else {
throw new \InvalidArgumentException('The provided prepend value must be an array with "name" and "value" keys.');
}

return $this;
}
Expand Down Expand Up @@ -441,9 +447,11 @@ public function getIndependent(): bool
return $this->evaluate($this->independent);
}

public function getCustomKey($record)
public function getCustomKey($record): string
{
return is_null($this->customKey) ? $record->getKey() : $record->{$this->customKey};
$key = is_null($this->customKey) ? $record->getKey() : $record->{$this->customKey};

return (string) $key;
}

public function getWithCount(): bool
Expand Down