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

80 lines
2.4 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-10-24 12:50:08 -07:00
<div class="input-group mb-1">
2020-10-25 04:30:55 -07:00
<div class="input-group-prepend">
<button class="btn btn-light border handle" type="button">
<Icon v="bars"/>
</button>
</div>
<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>
<div class="input-group-append">
<button class="btn btn-outline-danger" type="button" @click.prevent="remove(i)">
<Icon v="times"/>
</button>
</div>
</div>
</li>
2020-10-25 04:30:55 -07:00
<li slot="footer">
2020-10-24 12:50:08 -07:00
<button class="btn btn-outline-success btn-block" type="button" @click.prevent="add">
<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>