2
0
mirror of https://github.com/frappe/books.git synced 2024-12-27 12:42:44 +00:00
books/ui/components/controls/Base.vue

159 lines
3.3 KiB
Vue
Raw Normal View History

2018-06-27 14:38:27 +00:00
<script>
export default {
render(h) {
if (this.onlyInput) {
return this.getInputElement(h);
}
return this.getWrapperElement(h);
},
2019-09-17 08:14:09 +00:00
data() {
return {
label: this.docfield.label
};
},
props: {
docfield: Object,
value: [String, Number, Array, FileList],
onlyInput: {
type: Boolean,
default: false
},
disabled: Boolean,
autofocus: Boolean,
doc: Object
},
mounted() {
if (this.autofocus) {
this.$refs.input.focus();
}
},
computed: {
id() {
return (
this.docfield.fieldname +
'-' +
document.querySelectorAll(
`[data-fieldname="${this.docfield.fieldname}"]`
).length
);
},
inputClass() {
return [];
},
wrapperClass() {
return [];
},
labelClass() {
return [];
}
},
methods: {
getWrapperElement(h) {
return h(
'div',
{
class: [
'form-group',
this.onlyInput ? 'mb-0' : '',
...this.wrapperClass
],
attrs: {
'data-fieldname': this.docfield.fieldname,
'data-fieldtype': this.docfield.fieldtype
}
},
this.getChildrenElement(h)
);
},
getChildrenElement(h) {
2019-09-03 10:43:05 +00:00
return [
this.getLabelElement(h),
this.getInputElement(h),
this.getDescriptionElement(h)
];
},
getLabelElement(h) {
return h('label', {
class: [this.labelClass, 'text-muted'],
attrs: {
for: this.id
},
domProps: {
2019-09-17 08:14:09 +00:00
textContent: this.label
2019-09-03 10:43:05 +00:00
}
});
},
getDescriptionElement(h) {
return h('small', {
class: ['form-text', 'text-muted'],
domProps: {
textContent: this.docfield.description || ''
2018-06-27 14:38:27 +00:00
}
});
2018-06-27 14:38:27 +00:00
},
getInputElement(h) {
return h(
this.getInputTag(),
{
class: this.getInputClass(),
attrs: this.getInputAttrs(),
on: this.getInputListeners(),
domProps: this.getDomProps(),
ref: 'input'
},
this.getInputChildren(h)
);
},
getInputTag() {
return 'input';
},
getFormControlSize() {
return this.docfield.size === 'small'
? 'form-control-sm'
: this.size === 'large'
? 'form-control-lg'
: '';
},
getInputClass() {
return ['form-control', this.getFormControlSize(), ...this.inputClass];
},
getInputAttrs() {
return {
id: this.id,
type: 'text',
placeholder: this.docfield.placeholder || '',
value: this.value,
required: this.docfield.required,
disabled: this.disabled
};
},
getInputListeners() {
return {
change: e => {
this.handleChange(e.target.value);
2018-06-27 14:38:27 +00:00
}
};
},
getInputChildren() {
return null;
},
getDomProps() {
return null;
},
async handleChange(value) {
value = this.parse(value);
const isValid = await this.validate(value);
this.$refs.input.setCustomValidity(isValid === false ? 'error' : '');
this.$emit('change', value);
},
getValueFromInput(e) {},
validate() {
return true;
},
parse(value) {
return value;
2018-06-27 14:38:27 +00:00
}
}
};
2018-06-27 14:38:27 +00:00
</script>