This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Zaimki/components/ListInput.vue

76 lines
2.3 KiB
Vue
Raw Normal View History

2020-10-24 12:50:08 -07:00
<template>
2020-10-25 04:30:55 -07:00
<draggable tag="ul" v-model="iVal" handle=".handle" ghostClass="ghost" @end="$emit('input', iVal)" class="list-unstyled" :group="group">
<li v-for="(v, i) in iVal" ref="items">
2020-11-05 05:06:47 -08:00
<div class="input-group input-group-sm mb-1">
2021-01-22 14:54:24 -08:00
<button class="btn btn-light border handle" type="button" :aria-label="$t('table.sort')">
<Icon v="bars"/>
</button>
2020-10-25 04:30:55 -07:00
<slot v-bind:val="iVal[i]" v-bind:update="curry(update)(i)">
<input v-model="iVal[i]" type="text" class="form-control" required/>
2020-10-24 12:50:08 -07:00
</slot>
2021-01-22 14:54:24 -08:00
<button class="btn btn-outline-danger" type="button" @click.prevent="remove(i)" :aria-label="$t('crud.remove')">
<Icon v="times"/>
</button>
2020-10-24 12:50:08 -07:00
</div>
</li>
2020-10-25 04:30:55 -07:00
<li slot="footer">
2021-01-22 14:54:24 -08:00
<button class="btn btn-outline-success w-100 btn-sm" type="button" @click.prevent="add" :aria-label="$t('crud.add')">
2020-10-24 12:50:08 -07:00
<Icon v="plus"/>
</button>
</li>
2020-10-25 04:30:55 -07:00
</draggable>
2020-10-24 12:50:08 -07:00
</template>
<script>
import { curry } from "../src/helpers";
2020-10-25 04:30:55 -07:00
import draggable from 'vuedraggable'
2020-10-24 12:50:08 -07:00
export default {
2020-10-25 04:30:55 -07:00
components: {
draggable,
},
2020-10-24 12:50:08 -07:00
props: {
value: {},
prototype: { 'default': '' },
2020-10-25 04:30:55 -07:00
group: {},
2020-10-24 12:50:08 -07:00
},
data() {
return {
2020-10-25 04:30:55 -07:00
iVal: this.value,
2020-10-24 12:50:08 -07:00
curry: curry,
}
},
2020-10-25 04:30:55 -07:00
watch: {
value() {
this.iVal = this.value;
}
},
2020-10-24 12:50:08 -07:00
methods: {
remove(i) {
const v = [...this.value];
v.splice(i, 1);
this.$emit('input', v);
},
add() {
this.$emit('input', [...this.value, this.prototype]);
this.$nextTick(_ => {
2020-10-31 06:54:56 -07:00
this.$refs.items[this.value.length - 1].querySelector('input,textarea,select').focus();
2020-10-24 12:50:08 -07:00
});
},
update(i, val) {
const v = [...this.value];
v[i] = val;
this.$emit('input', v);
}
},
}
</script>
2020-10-25 04:30:55 -07:00
<style lang="scss" scoped>
.ghost {
opacity: 0.5;
background: #c8ebfb;
}
</style>