Files
mixtape/zero.Web.UI/App/components/forms/check-list.vue
T

139 lines
2.6 KiB
Vue
Raw Normal View History

2020-06-29 15:36:02 +02:00
<template>
2020-08-11 16:01:10 +02:00
<div class="ui-check-list" :class="{'is-disabled': disabled, 'is-inline': inline }">
2020-06-29 15:36:02 +02:00
<label v-for="item in list" class="ui-native-check ui-check-list-item">
2020-09-17 11:26:23 +02:00
<input type="checkbox" :checked="isChecked(item)" @input="onChange(item)" :disabled="disabled" />
2020-06-29 15:36:02 +02:00
<span class="ui-native-check-toggle"></span>
2020-10-20 13:27:17 +02:00
<span v-localize="item[labelKey]"></span>
2020-06-29 15:36:02 +02:00
</label>
</div>
</template>
<script>
import { map as _map, filter as _filter } from 'underscore';
export default {
name: 'uiCheckList',
props: {
value: {
type: Array,
default: () => []
},
items: {
type: [Array, Function, Promise],
required: true
},
disabled: {
type: Boolean,
default: false
},
2020-08-11 16:01:10 +02:00
inline: {
type: Boolean,
default: false
},
2020-10-20 13:27:17 +02:00
limit: {
2020-06-29 15:36:02 +02:00
type: Number,
default: 100
},
2020-10-20 13:27:17 +02:00
reverse: Boolean,
labelKey: {
type: String,
default: 'value'
},
idKey: {
type: String,
default: 'key'
2020-06-29 15:36:02 +02:00
}
},
data: () => ({
list: []
}),
watch: {
items()
{
this.init();
}
},
mounted()
{
this.init();
},
methods: {
init()
{
if (typeof this.items === 'function')
{
this.items().then(res =>
{
this.list = res;
});
}
else
{
this.list = JSON.parse(JSON.stringify(this.items));
}
},
isChecked(item)
{
2020-10-20 13:27:17 +02:00
let index = this.value.indexOf(item[this.idKey]);
2020-06-29 15:36:02 +02:00
return (!this.reverse && index > -1) || (this.reverse && index < 0);
},
onChange(item)
{
2020-10-20 13:27:17 +02:00
let index = this.value.indexOf(item[this.idKey]);
2020-06-29 15:36:02 +02:00
let value = JSON.parse(JSON.stringify(this.value));
if (index < 0)
{
2020-10-20 13:27:17 +02:00
value.push(item[this.idKey]);
2020-06-29 15:36:02 +02:00
}
else
{
value.splice(index, 1);
}
this.$emit('input', value);
},
}
}
</script>
<style lang="scss">
.ui-check-list-item
{
display: block;
}
2021-03-25 15:31:53 +01:00
.ui-check-list .ui-check-list-item + .ui-check-list-item
{
margin-top: 8px;
}
.ui-alias + .ui-check-list-item
{
margin-top: 14px;
}
2020-08-11 16:01:10 +02:00
.ui-check-list.is-inline .ui-check-list-item
{
display: inline-block;
}
.ui-check-list.is-inline .ui-check-list-item + .ui-check-list-item
{
margin-top: 0;
margin-left: 30px;
}
.ui-check-list.is-inline .ui-check-list-item .ui-native-check-toggle
{
margin-right: 6px;
2020-08-11 16:01:10 +02:00
}
2020-06-29 15:36:02 +02:00
</style>