-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): improve the task array component (#7095)
* feat(ui): improve the task array component * chore(ui): replace existing task on editing during creation instead of re-adding them
- Loading branch information
1 parent
5f7468a
commit 1301aaa
Showing
2 changed files
with
40 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,51 @@ | ||
<template> | ||
<el-row | ||
v-for="(item, index) in values" | ||
v-for="(element, index) in items" | ||
:key="'array-' + index" | ||
:gutter="10" | ||
class="w-100 mb-2" | ||
> | ||
<el-col :span="22"> | ||
<component | ||
:is="`task-${getType(schema.items)}`" | ||
:model-value="item" | ||
@update:model-value="onInput(index, $event)" | ||
:root="getKey(index)" | ||
:schema="schema.items" | ||
:definitions="definitions" | ||
<InputText | ||
:model-value="element" | ||
@update:model-value="(v) => handleInput(v, index)" | ||
:placeholder="$t('value')" | ||
class="w-100" | ||
/> | ||
</el-col> | ||
<el-col :span="2" class="col align-self-center delete"> | ||
<DeleteOutline @click="removeItem(key)" /> | ||
<DeleteOutline @click="removeItem(index)" /> | ||
</el-col> | ||
</el-row> | ||
<Add @add="addItem()" v-if="values.at(-1)" /> | ||
<Add @add="addItem()" /> | ||
</template> | ||
|
||
<script setup> | ||
<script setup lang="ts"> | ||
import {ref} from "vue"; | ||
import {DeleteOutline} from "../../code/utils/icons"; | ||
import InputText from "../../code/components/inputs/InputText.vue"; | ||
import Add from "../../code/components/Add.vue"; | ||
</script> | ||
|
||
<script> | ||
import {toRaw} from "vue"; | ||
import Task from "./Task"; | ||
export default { | ||
mixins: [Task], | ||
emits: ["update:modelValue"], | ||
created() { | ||
if (!Array.isArray(this.modelValue) && this.modelValue !== undefined) { | ||
this.$emit("update:modelValue", []); | ||
} | ||
}, | ||
computed: { | ||
values() { | ||
if (this.modelValue === undefined) { | ||
return this.schema.default || [undefined]; | ||
} | ||
return this.modelValue; | ||
}, | ||
}, | ||
methods: { | ||
getPropertiesValue(properties) { | ||
return this.modelValue && this.modelValue[properties] | ||
? this.modelValue[properties] | ||
: undefined; | ||
}, | ||
onInput(index, value) { | ||
const local = this.modelValue || []; | ||
local[index] = value; | ||
this.$emit("update:modelValue", local); | ||
}, | ||
addItem() { | ||
let local = this.modelValue || []; | ||
local.push(undefined); | ||
const emits = defineEmits(["update:modelValue"]); | ||
const props = defineProps({modelValue: {type: Array, default: undefined}}); | ||
// click on + when there is no items | ||
if (this.modelValue === undefined) { | ||
local.push(undefined); | ||
} | ||
const items = ref( | ||
!Array.isArray(props.modelValue) ? [props.modelValue] : props.modelValue, | ||
); | ||
this.$emit("update:modelValue", local); | ||
}, | ||
removeItem(x) { | ||
let local = this.modelValue || []; | ||
local.splice(x, 1); | ||
if (local.length === 1) { | ||
let raw = toRaw(local[0]); | ||
if (raw === null || raw === undefined) { | ||
local = undefined; | ||
} | ||
} | ||
const handleInput = (value: string, index: number) => { | ||
items.value[index] = value; | ||
emits("update:modelValue", items.value); | ||
}; | ||
this.$emit("update:modelValue", local); | ||
}, | ||
}, | ||
const addItem = () => { | ||
items.value.push(undefined); | ||
emits("update:modelValue", items.value); | ||
}; | ||
const removeItem = (index: number) => { | ||
items.value.splice(index, 1); | ||
emits("update:modelValue", items.value); | ||
}; | ||
</script> | ||
|
||
<style scoped lang="scss"> | ||
@import "../../code/styles/code.scss"; | ||
</style> |