Skip to content

Commit 0bff64d

Browse files
Merge pull request #18 from CodeWithDennis/feature/relationships
[WIP] relationships
2 parents 4a8b7d3 + 6897c95 commit 0bff64d

File tree

7 files changed

+161
-169
lines changed

7 files changed

+161
-169
lines changed

README.md

+37-57
Original file line numberDiff line numberDiff line change
@@ -18,85 +18,65 @@ php artisan filament:assets
1818
```
1919

2020
## Features
21-
- ✅ Compatible with dark mode
22-
- ✅ Featuring search functionality
23-
- ✅ Comma seperated multi-select
24-
- ✅ Custom options
25-
- ❌ Disabled options (Planned)
26-
- ❌ Relationships (Planned)
27-
21+
22+
- Dark Mode: It seamlessly supports both Filament's light and dark modes out of the box.
23+
- Search: Searching is fully supported, and it seamlessly searches through all levels within the tree structure.
24+
- BelongsTo Integration: Establish connections within your data effortlessly.
25+
- BelongsToMany Integration: Simplify the management of complex relationships through BelongsToMany integration.
26+
27+
🐛 One thing I have noticed about this project is that it tends to run a lot of queries, mainly because of its recursive design. Working to fix this in the upcoming updates.
28+
2829
## Usage
2930

3031
```PHP
32+
// Create a tree based on a 'BelongsToMany' relationship
33+
SelectTree::make('categories')
34+
->relationship('categories', 'name', 'parent_id', function ($query) {
35+
return $query;
36+
})
37+
38+
// Create a tree based on a 'BelongsTo' relationship
3139
SelectTree::make('category_id')
32-
// Creates a select tree with 'Category' model, using 'category_id' as parent and 'name' as label, allowing custom query modification.
33-
->tree(Category::class, 'category_id', 'name', function ($query) {
40+
->relationship('category', 'name', 'parent_id', function ($query) {
3441
return $query;
3542
})
3643

37-
// Set a custom placeholder for when no items are selected
38-
->placeholder(__('Your custom placeholder here'))
44+
// Set a custom placeholder when no items are selected
45+
->placeholder(__('Enter your custom placeholder here'))
3946

40-
// Ensures that only leaf nodes can be selected while preventing the selection of groups.
41-
->disabledBranchNode()
42-
43-
// Adjust the emptyLabel for when there are zero search results.
44-
->emptyLabel(__('No results found'))
47+
// Enable the selection of groups
48+
->enableBranchNode()
4549

46-
// Show the count of children alongside the group's name.
47-
->withCount()
50+
// Customize the label when there are zero search results
51+
->emptyLabel(__('No results found'))
4852

49-
// To keep the dropdown open at all times
50-
->alwaysOpen()
53+
// Display the count of children alongside the group's name
54+
->withCount()
5155

52-
// By default, all nodes are independent.
53-
->independent(false)
54-
55-
// When 'independent' is set to false, the tree will open with the selected values by default.
56-
->expandSelected(false)
57-
58-
// Display individual leaf nodes instead of the main group when all leaf nodes are selected.
59-
->grouped(false)
56+
// Keep the dropdown open at all times
57+
->alwaysOpen()
6058

61-
// By default, the clearable icon is enabled, but you can hide it with:
62-
->clearable(false)
59+
// Set nodes as dependent
60+
->independent(false)
6361

64-
// Enable the option to save multiple values as a string (comma-separated)
65-
->multiple()
62+
// Expand the tree with selected values
63+
->expandSelected(false)
6664

67-
// Activates the search functionality for the SelectTree.
68-
->searchable()
69-
```
70-
### Custom
71-
If you prefer to create custom options rather than utilizing a pre-existing model, you can achieve this using the following example:
65+
// Display individual leaf nodes instead of the main group when all leaf nodes are selected
66+
->grouped(false)
7267

73-
```PHP
74-
->options([
75-
[
76-
'name' => 'Electronics',
77-
'value' => 'electronics',
78-
'children' => [
79-
[
80-
'name' => 'Mobile Devices',
81-
'value' => 'mobile devices',
82-
'children' => [
83-
[
84-
'name' => 'Apple Products',
85-
'value' => 'apple products',
86-
]
87-
]
88-
]
89-
]
90-
],
91-
])
68+
// Hide the clearable icon
69+
->clearable(false)
70+
71+
// Activate the search functionality for the SelectTree
72+
->searchable();
9273
```
9374

9475
## Screenshots
9576

9677
<img width="641" alt="light" src="https://github.com/CodeWithDennis/filament-select-tree/assets/23448484/4d348c85-5ee9-45b1-9424-0d8b3efcc02e">
9778
<img width="649" alt="dark" src="https://github.com/CodeWithDennis/filament-select-tree/assets/23448484/396627ff-bf36-44b7-b20c-0d32b2eff957">
9879

99-
10080
## Changelog
10181
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
10282

resources/css/custom.css

-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ html.dark .treeselect-input__tags-element:hover svg {
212212
}
213213

214214
.treeselect-input__clear svg {
215-
stroke: rgb(255 255 255/var(--tw-text-opacity));
216215
opacity: 0.5;
217216
}
218217

resources/dist/custom.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/dist/tree.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/js/index.js

+21-28
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,33 @@
11
import Treeselect from 'treeselectjs'
22

3-
export default function tree({
4-
state,
5-
name,
6-
options,
7-
searchable,
8-
showCount,
9-
placeholder,
10-
disabledBranchNode,
11-
disabled = false,
12-
isSingleSelect = true,
13-
showTags = true,
14-
clearable = true,
15-
isIndependentNodes = true,
16-
alwaysOpen = false,
17-
emptyText,
18-
expandSelected = true,
19-
grouped = true,
20-
}) {
3+
export default function tree(
4+
{
5+
state,
6+
name,
7+
options,
8+
searchable,
9+
showCount,
10+
placeholder,
11+
disabledBranchNode = true,
12+
disabled = false,
13+
isSingleSelect = true,
14+
showTags = true,
15+
clearable = true,
16+
isIndependentNodes = true,
17+
alwaysOpen = false,
18+
emptyText,
19+
expandSelected = true,
20+
grouped = true,
21+
}) {
2122
return {
2223
state,
2324
tree: null,
2425
init() {
25-
const values = isSingleSelect
26-
? (this.state !== null ? this.state : '')
27-
: (this.state !== null ? this.state.split(',') : []);
28-
2926
this.tree = new Treeselect({
3027
id: `tree-${name}-id`,
3128
ariaLabel: `tree-${name}-label`,
3229
parentHtmlContainer: this.$refs.tree,
33-
value: values,
30+
value: this.state,
3431
options,
3532
searchable,
3633
showCount,
@@ -48,11 +45,7 @@ export default function tree({
4845
});
4946

5047
this.tree.srcElement.addEventListener('input', (e) => {
51-
if (Array.isArray(e.detail)) {
52-
this.state = e.detail.join(",");
53-
} else {
54-
this.state = e.detail;
55-
}
48+
this.state = e.detail;
5649
});
5750
}
5851
}

resources/views/select-tree.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
searchable: '{{ $isSearchable() }}',
1919
showCount: '{{ $getWithCount() }}',
2020
placeholder: '{{ $getPlaceholder() }}',
21-
disabledBranchNode: '{{ $getDisabledBranchNode() }}',
21+
disabledBranchNode: '{{ !$getEnableBranchNode() }}',
2222
disabled: '{{ $isDisabled() }}',
2323
isSingleSelect: '{{ !$getMultiple() }}',
2424
isIndependentNodes: '{{ $getIndependent() }}',

0 commit comments

Comments
 (0)