Skip to content
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

[3.x] Add selectable prop on VueSelect component #1619

Merged
merged 4 commits into from
May 23, 2022
Merged
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
35 changes: 35 additions & 0 deletions docs/src/form-fields/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Select::make()
->options(
Options::make([
Option::make('key', 'value'),
Option::make('key', 'value', selectable: false),
...
])
);
Expand Down Expand Up @@ -83,6 +84,40 @@ Select::make()
| default | Sets a default value if empty | string | |
| disabled | Disables the field | boolean | false |


Select item option
| Option | Description | Type/values | Default value |
| :---------- | :----------------------------------------------------------- | :-------------- | :------------ |
| selectable | Defines if select item should be selectable in the select or not | boolean | true |

Example of `selectable` prop usage:
```php
@php
$selectOptions = [
[
'value' => 1,
'label' => 'New York'
],
[
'value' => 2,
'label' => 'London'
],
[
'value' => 3,
'label' => 'Berlin'
'selectable' => false // This item will be non-selectable in the select form component
]
];
@endphp

<x-twill::select
name="office"
label="office"
placeholder="Select an office"
:options="$selectOptions"
/>
```

A migration to save a `select` field would be:

```php
Expand Down
11 changes: 6 additions & 5 deletions frontend/js/components/VSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
:value="value"
:options="currentOptions"
:searchable="searchable"
:selectable="selectable"
:clearSearchOnSelect="clearSearchOnSelect"
:label="optionsLabel"
:taggable="taggable"
Expand Down Expand Up @@ -44,7 +45,6 @@
import extendedVSelect from '@/components/VSelect/ExtendedVSelect.vue'
// check full options of the vueSelect here : http://sagalbot.github.io/vue-select/
// import vSelect from 'vue-select' // check full options of the vueSelect here : http://sagalbot.github.io/vue-select/

export default {
name: 'A17VueSelect',
mixins: [randKeyMixin, InputframeMixin, FormStoreMixin, AttributesMixin],
Expand Down Expand Up @@ -81,6 +81,10 @@
type: Boolean,
default: false
},
selectable: {
type: Function,
default: option => option.selectable ?? true,
},
clearSearchOnSelect: {
type: Boolean,
default: true
Expand Down Expand Up @@ -150,7 +154,6 @@
if (typeof this.value[0] === 'object') {
return this.value.map(e => e.value)
}

return this.value.join(',')
}
}
Expand Down Expand Up @@ -199,12 +202,10 @@
// see formStore mixin
this.value = value
this.saveIntoStore()

this.$emit('change', value)
},
getOptions: debounce(function (search, loading) {
if (!this.isAjax()) return true

loading(true)
this.$http.get(this.ajaxUrl, { params: { q: search } }).then((resp) => {
if (resp.data.items && resp.data.items.length) {
Expand All @@ -226,4 +227,4 @@
}, 500)
}
}
</script>
</script>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@area17/twill",
"version": "2.8.0",
"version": "3.0.0",
"private": true,
"scripts": {
"inspect": "vue-cli-service inspect --mode production",
Expand Down
6 changes: 4 additions & 2 deletions src/Services/Forms/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ class Option implements Arrayable
public function __construct(
protected string|int $value,
protected string $label,
protected bool $selectable = true,
) {
}

public static function make(string|int $value, string $label): self
public static function make(string|int $value, string $label, bool $selectable = true): self
{
return new self($value, $label);
return new self($value, $label, $selectable);
}

public function toArray(): array
{
return [
'value' => $this->value,
'label' => $this->label,
'selectable' => $this->selectable,
];
}
}