scinote-web/app/javascript/vue/forms/edit_fields/options_mixin.js

32 lines
853 B
JavaScript

/* global GLOBAL_CONSTANTS */
export default {
data() {
return {
options: this.field.attributes.data.options?.join('\n')
};
},
computed: {
validField() {
return !this.editField.attributes.data.options || this.editField.attributes.data.options.length <= GLOBAL_CONSTANTS.NAME_MAX_LENGTH;
},
optionFieldErrors() {
if (!this.validField) {
return this.i18n.t('forms.show.options_too_many_error', { limit: GLOBAL_CONSTANTS.NAME_MAX_LENGTH });
}
return '';
}
},
watch: {
options() {
const newOptions = this.options.split('\n')
.filter((option) => option.trim() !== '')
.map((option) => option.trim());
// remove duplicates
const uniqueOptions = [...new Set(newOptions)];
this.editField.attributes.data.options = uniqueOptions;
}
}
};