Skip to content

Commit

Permalink
Improve and validate page name edit
Browse files Browse the repository at this point in the history
  • Loading branch information
caleeli committed Mar 1, 2024
1 parent 72e81e8 commit 43250ec
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions src/components/sortable/sortableList/SortableList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@
v-model="item.name"
type="text"
autofocus
@blur.stop="onBlur()"
required
:state="validateState(item.name, item)"
:error="validateError(item.name, item)"
@blur.stop="onBlur(item.name, item)"
@keydown.enter.stop="onBlur(item.name, item)"
@focus="onFocus(item.name, item)"
/>
<span v-else>{{ item.name }}</span>
</div>
<div class="border rounded-lg sortable-item-action">
<button class="btn" @click.stop="onClick(item, index)">
<button v-if="editRowIndex === index" class="btn">
<i class="fas fa-check"></i>
</button>
<button v-else class="btn" @click.stop="onClick(item, index)">
<i class="fas fa-edit"></i>
</button>
<div class="sortable-item-vr"></div>
Expand All @@ -55,6 +63,7 @@ export default {
},
data() {
return {
originalName: '',
draggedItem: 0,
draggedOverItem: 0,
editRowIndex: null,
Expand All @@ -69,14 +78,40 @@ export default {
}
},
methods: {
onBlur() {
this.editRowIndex = -1;
validateState(name, item) {
const isEmpty = !name;
const isDuplicated = this.items
.filter((i) => i !== item)
.find((i) => i.name === name);
return isEmpty || isDuplicated ? false : null;
},
onClick(item, index) {
if (this.editRowIndex === -1 || this.editRowIndex === index) {
this.editRowIndex = null;
return;
validateError(name, item) {
const isEmpty = !name;
if (!isEmpty) {
return this.$t("The Page Name field is required.");
}
const isDuplicated = this.items
.filter((i) => i !== item)
.find((i) => i.name === name);
if (isDuplicated) {
return this.$t('Must be unique.');
}
return '';
},
onFocus(name, item) {
this.originalName = name;
},
async onBlur(name, item) {
if (this.validateState(name, item) === false) {
// eslint-disable-next-line no-param-reassign
item.name = this.originalName;
}
await this.$nextTick();
setTimeout(() => {
this.editRowIndex = null;
}, 250);
},
onClick(item, index) {
this.editRowIndex = index;
this.$emit("item-edit", item);
},
Expand Down

0 comments on commit 43250ec

Please sign in to comment.