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

Commit 58d07f7

Browse files
committed
Prevent false positives for has children when collections are used
1 parent 2b0a64b commit 58d07f7

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/Components/Inputs/CustomSelectOption.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public function hasChildren(string $childrenField): bool
8484
return $this->hasChildren;
8585
}
8686

87-
return $this->hasChildren = ! empty($this->optionChildren($childrenField));
87+
$children = $this->optionChildren($childrenField);
88+
89+
return $children instanceof Collection
90+
? $this->hasChildren = $children->isNotEmpty()
91+
: $this->hasChildren = ! empty($children);
8892
}
8993
}

src/Concerns/GetsSelectOptionProperties.php

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

55
use Illuminate\Database\Eloquent\Model;
66
use Illuminate\Support\Arr;
7+
use Illuminate\Support\Collection;
78

89
/**
910
* @property string $childrenField
@@ -54,7 +55,9 @@ public function optionIsOptGroup($option, ?string $childrenField = null): bool
5455
// We will consider an option an "opt group" if it has children.
5556
$children = $this->optionChildren($option, $childrenField);
5657

57-
return ! empty($children);
58+
return $children instanceof Collection
59+
? $children->isNotEmpty()
60+
: ! empty($children);
5861
}
5962

6063
protected function optionProperty($option, $field, $default = null)

tests/Components/Inputs/TreeSelectOptionTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,26 @@
105105
});
106106
});
107107
});
108+
109+
it('will not show child indicators on empty collections', function () {
110+
$option = [
111+
'id' => 'foo',
112+
'name' => 'Foo',
113+
'children' => collect(),
114+
];
115+
116+
$template = <<<'HTML'
117+
<x-tree-select>
118+
<x-tree-select-option :value="$option" />
119+
</x-tree-select>
120+
HTML;
121+
122+
Route::get('/test', fn () => Blade::render($template, ['option' => $option]));
123+
124+
get('/test')
125+
->assertElementExists('.tree-select__option-li', function (AssertElement $option) {
126+
$option->doesntContain('ul', [
127+
'class' => 'tree-select__children',
128+
]);
129+
});
130+
});

0 commit comments

Comments
 (0)