Skip to content

Commit d292f64

Browse files
authored
Merge pull request #4
1.x
2 parents db7aeb6 + 5410ab9 commit d292f64

File tree

7 files changed

+171
-44
lines changed

7 files changed

+171
-44
lines changed

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
## v1.0.4 (2025-03-22)
2+
### Added
3+
* Support for IDs: Added support for adding `id` attributes to the selectable items using the `withId` method.
4+
5+
### Improved
6+
* `withDataAttribute` method: Added support for passing a Closure to the `withDataAttribute` method to generate the data attribute value dynamically.
7+
* `withClass` method: Added support for passing an array of classes to the `withClass` method to add multiple classes to the selectable items.
8+
* Performance optimization option rendering
9+
10+
## v1.0.3 (2024-11-09)
11+
### Added
12+
* Support for non-object (array) collections
13+
14+
### Added
15+
* Support for IDs: Added support for adding IDs to the selectable items using the `withId` method.
16+
17+
18+
## v1.0.2 (2024-07-02)
19+
### Added
20+
* Introduced custom IDE helper file
21+
122
## v1.0.1 (2024-05-21)
223

324
### Added

README.md

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ composer require ringlesoft/laravel-selectable
3333

3434
```html
3535
<select name="user_id">
36-
{!! \\App\\Model\\User::all()->toSelectOptions(); !!}}
36+
{!! \\App\\Models\\User::all()->toSelectOptions(); !!}}
3737
</select>
3838
```
3939

@@ -51,7 +51,7 @@ the value.
5151

5252
```bladehtml
5353
<select name="user_id">
54-
{!! \\App\\Model\\User::all()->toSelectOptions('email', 'uuid', '6490132934f22'); !!}}
54+
{!! \\App\\Models\\User::all()->toSelectOptions('email', 'uuid', '6490132934f22'); !!}}
5555
</select>
5656
```
5757

@@ -116,21 +116,22 @@ other than '`active`', and a custom class '`form-option custom`' will be applied
116116

117117
#### Available methods
118118

119-
- `withLabel(string|callable $label)`: This method allows you to customize the label for each option. A string will be
120-
used as the collection field from which the label will be generated, while a callable will be used to generate the
119+
- `withLabel(string|Closure $label)`: This method allows you to customize the label for each option. A string will be
120+
used as the collection field from which the label will be generated, while a Closure will be used to generate the
121121
label.
122-
- `withValue(string|callable $value)`: This method allows you to customize the value for each option. A string will be
123-
used as the collection field from which the value will be generated, while a callable will be used to generate the
122+
- `withValue(string|Closure $value)`: This method allows you to customize the value for each option. A string will be
123+
used as the collection field from which the value will be generated, while a Closure will be used to generate the
124124
value.
125-
- `withSelected(mixed|callable $selected)`: This method allows you to customize the selected options. Can be
126-
a `string`, `int`, an array of `string`/`int`, a `model` or a callable that returns a boolean value.
127-
- `withDisabled(mixed|callable $disabled)`: This method allows you to customize the disabled options. Can be
128-
a `string`, `int`, an array of `string`/`int`, a `model` or a callable that returns a boolean value.
129-
- `withDataAttribute(string $attribute, mixed|callable $value)`: This method allows you to add a data attribute to each
130-
option.
131-
- `withClass(string $class)`: This method allows you to add a class to each option.
132-
- `toSelectItems()`: This method converts the selectable collection to an array of selectable items. Useful for Ajax
133-
responses or SPA.
125+
- `withSelected(mixed|Closure $selected)`: This method allows you to customize the selected options. Can be
126+
a `string`, `int`, an array of `string`/`int`, a `model` or a `Closure` that returns a `boolean` value.
127+
- `withDisabled(mixed|Closure $disabled)`: This method allows you to customize the disabled options. Can be
128+
a `string`, `int`, an array of `string`/`int`, a `model` or a `Closure` that returns a `boolean` value.
129+
- `withDataAttribute(string|Closure $attribute, mixed|Closure $value)`: This method allows you to add a data attribute to each
130+
option. The first parameter can be a `string` or a `Closure` that returns a `string` which will be attached as `data-{attribute}="{value}"` to the option. The second parameter can be any type convertable to a string or a `Closure` that returns a `string`.
131+
- `withId(string|Closure $id)`: This method allows you to add an `id` attribute to each option. The value can be a `Closure` that returns a unique `string` for each option.
132+
- `withClass(string|array|Closure $class)`: This method allows you to add a class to each option. The value can be a `string` or an `array` of `string` or a `Closure` that returns a `string`.
133+
- `toSelectItems()`: This method converts the selectable collection to an `array` of selectable items. Useful for Ajax
134+
responses or SPAs.
134135
- `toSelectOptions()`: This method converts the selectable collection to an HTML select options string.
135136
- Some of the methods from `Illuminate\Support\Collection` are also available including `groupBy()`.
136137

@@ -142,14 +143,14 @@ You can work with collections of non-object arrays both flat and associative
142143
```php
143144
$array1 = ["First", "Second", "Third"];
144145
$array2 = ['first' => "First", 'second' => "Second", 'third' => "Third"];
145-
$array3 = [['name' => 'First', 'number' => 1],['name' => 'Second', 'number' => 2],['name' => 'Third', 'number' => 3]]
146+
$array3 = [['name' => 'First', 'number' => 1],['name' => 'Second', 'number' => 2],['name' => 'Third', 'number' => 3]];
146147
$options = collect($array)->toSelectOptions();
147148
$options2 = collect($array2)->toSelectOptions();
148149
$options3 = collect($array3)->toSelectable()->withValue('number')->toSelectOptions();
149150
```
150151

151152

152-
## Get Selectable Items
153+
## Getting Selectable Items
153154
```php
154155
$selectableItems = \App\Models\User::all()->toSelectable()->toSelectItems();
155156
```
@@ -173,7 +174,7 @@ Single Page Applications (SPAs).
173174

174175

175176
## Testing
176-
177+
To run tests, run the following command:
177178
```bash
178179
composer test
179180
```

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
],
2828
"require": {
2929
"php": ">=8.1",
30-
"illuminate/collections": "^9.0|^10.0|^11.0",
31-
"illuminate/routing": "^9.0|^10.24|^11.0"
30+
"illuminate/collections": "^9|^10|^11|^12",
31+
"illuminate/support": "^9|^10|^11|^12"
3232
}
3333
,
3434
"require-dev": {
@@ -51,7 +51,7 @@
5151
"name": "master"
5252
}
5353
},
54-
"version": "1.0.3",
54+
"version": "1.0.4",
5555
"minimum-stability": "dev",
5656
"prefer-stable": true
5757
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace RingleSoft\LaravelSelectable\Exceptions;
4+
5+
use Throwable;
6+
7+
class InvalidCallableException extends LaravelSelectableException
8+
{
9+
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null)
10+
{
11+
parent::__construct($message, $code, $previous);
12+
}
13+
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace RingleSoft\LaravelSelectable\Exceptions;
4+
5+
use Exception;
6+
use Throwable;
7+
8+
class LaravelSelectableException extends Exception
9+
{
10+
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null)
11+
{
12+
$message = "Laravel Selectable: " . $message;
13+
parent::__construct($message, $code, $previous);
14+
}
15+
16+
}

0 commit comments

Comments
 (0)