Files
mixtape/zero.Backoffice.UI/app/components/ui-check-list.vue
T

151 lines
2.9 KiB
Vue
Raw Normal View History

2021-04-30 12:04:21 +02:00
<template>
2022-01-14 11:51:38 +01:00
<div class="ui-check-list" :class="{'is-disabled': disabled, 'is-inline': inline }">
<label v-for="item in list" class="ui-native-check ui-check-list-item">
<input type="checkbox" :checked="isChecked(item)" @input="onChange(item)" :disabled="disabled" />
<span class="ui-native-check-toggle"></span>
<span v-localize="item[labelKey]"></span>
<span class="-desc" v-if="item[descriptionKey]" v-localize="item[descriptionKey]"></span>
</label>
</div>
</template>
<script>
export default {
2022-01-14 11:51:38 +01:00
name: 'uiCheckList',
props: {
2021-12-30 13:39:31 +01:00
value: {
type: Array,
default: () => []
},
2022-01-14 11:51:38 +01:00
items: {
type: [Array, Function, Promise],
required: true
},
disabled: {
type: Boolean,
default: false
},
2022-01-14 11:51:38 +01:00
inline: {
2020-09-18 11:15:17 +02:00
type: Boolean,
default: false
},
2022-01-14 11:51:38 +01:00
limit: {
type: Number,
default: 100
},
reverse: Boolean,
labelKey: {
type: String,
default: 'value'
},
descriptionKey: {
type: String,
default: 'description'
},
idKey: {
type: String,
default: 'key'
}
},
data: () => ({
2022-01-14 11:51:38 +01:00
list: []
}),
watch: {
2022-01-14 11:51:38 +01:00
items()
{
2022-01-14 11:51:38 +01:00
this.init();
}
},
2020-06-23 12:23:23 +02:00
mounted()
{
2022-01-14 11:51:38 +01:00
this.init();
2020-06-23 12:23:23 +02:00
},
methods: {
2022-01-14 11:51:38 +01:00
init()
2021-12-07 15:59:29 +01:00
{
2022-01-14 11:51:38 +01:00
if (typeof this.items === 'function')
2020-09-18 11:15:17 +02:00
{
2022-01-14 11:51:38 +01:00
this.items().then(res =>
{
this.list = res.data;
});
}
else
{
this.list = JSON.parse(JSON.stringify(this.items));
}
},
2022-01-14 11:51:38 +01:00
isChecked(item)
{
2022-01-14 11:51:38 +01:00
let index = this.value.indexOf(item[this.idKey]);
return (!this.reverse && index > -1) || (this.reverse && index < 0);
},
onChange(item)
{
let index = this.value.indexOf(item[this.idKey]);
let value = JSON.parse(JSON.stringify(this.value));
if (index < 0)
{
value.push(item[this.idKey]);
}
else
{
value.splice(index, 1);
}
this.$emit('input', value);
}
}
}
</script>
<style lang="scss">
2022-01-14 11:51:38 +01:00
.ui-check-list-item
{
2022-01-14 11:51:38 +01:00
display: block;
}
2022-01-14 11:51:38 +01:00
.ui-check-list .ui-check-list-item + .ui-check-list-item
{
2022-01-14 11:51:38 +01:00
margin-top: 8px;
}
2022-01-14 11:51:38 +01:00
.ui-alias + .ui-check-list-item
{
margin-top: 14px;
}
2020-09-17 11:26:23 +02:00
2022-01-14 11:51:38 +01:00
.ui-check-list.is-inline .ui-check-list-item
{
display: inline-block;
}
2022-01-14 11:51:38 +01:00
.ui-check-list.is-inline .ui-check-list-item + .ui-check-list-item
{
margin-top: 0;
margin-left: 30px;
}
2021-03-04 15:47:26 +01:00
2022-01-14 11:51:38 +01:00
.ui-check-list.is-inline .ui-check-list-item .ui-native-check-toggle
{
margin-right: 6px;
}
.ui-check-list-item .-desc
{
display: inline-block;
color: var(--color-text-dim);
font-size: var(--font-size-s);
margin-left: 0.5em;
&:before { content: '('; }
&:after { content: ')'; }
}
</style>