2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 11:29:00 +00:00

fix: call validate on blur

- number type Float, Int
- remove floatPrecision
This commit is contained in:
18alantom 2021-12-23 14:40:45 +05:30
parent 44a60a84dd
commit ec3558f513
4 changed files with 55 additions and 18 deletions

View File

@ -162,7 +162,7 @@ export default class LedgerPosting {
} }
getPrecision() { getPrecision() {
return frappe.SystemSettings.floatPrecision; return frappe.SystemSettings.internalPrecision;
} }
getRoundOffAccount() { getRoundOffAccount() {

View File

@ -10,14 +10,17 @@
:value="value" :value="value"
:placeholder="inputPlaceholder" :placeholder="inputPlaceholder"
:readonly="isReadOnly" :readonly="isReadOnly"
@blur="e => triggerChange(e.target.value)" :max="df.maxValue"
@focus="e => $emit('focus', e)" :min="df.minValue"
@input="e => $emit('input', e)" @blur="(e) => triggerChange(e.target.value)"
@focus="(e) => $emit('focus', e)"
@input="(e) => $emit('input', e)"
/> />
</div> </div>
</template> </template>
<script> <script>
import { showMessageDialog } from '../../utils';
export default { export default {
name: 'Base', name: 'Base',
props: [ props: [
@ -28,18 +31,18 @@ export default {
'size', 'size',
'showLabel', 'showLabel',
'readOnly', 'readOnly',
'autofocus' 'autofocus',
], ],
inject: { inject: {
doctype: { doctype: {
default: null default: null,
}, },
name: { name: {
default: null default: null,
}, },
doc: { doc: {
default: null default: null,
} },
}, },
mounted() { mounted() {
if (this.autofocus) { if (this.autofocus) {
@ -55,9 +58,9 @@ export default {
{ {
'px-3 py-2': this.size !== 'small', 'px-3 py-2': this.size !== 'small',
'px-2 py-1': this.size === 'small', 'px-2 py-1': this.size === 'small',
'pointer-events-none': this.isReadOnly 'pointer-events-none': this.isReadOnly,
}, },
'focus:outline-none focus:bg-gray-200 rounded w-full text-gray-900 placeholder-gray-400' 'focus:outline-none focus:bg-gray-200 rounded w-full text-gray-900 placeholder-gray-400',
]; ];
return this.getInputClassesFromProp(classes); return this.getInputClassesFromProp(classes);
@ -70,7 +73,7 @@ export default {
return this.readOnly; return this.readOnly;
} }
return this.df.readOnly; return this.df.readOnly;
} },
}, },
methods: { methods: {
getInputClassesFromProp(classes) { getInputClassesFromProp(classes) {
@ -90,17 +93,41 @@ export default {
}, },
triggerChange(value) { triggerChange(value) {
value = this.parse(value); value = this.parse(value);
const isValid = this.validate(value);
if (!isValid) {
return;
}
if (value === '') { if (value === '') {
value = null; value = null;
} }
this.$emit('change', value); this.$emit('change', value);
}, },
validate(value) {
if (!(typeof this.df.validate === 'function')) {
return;
}
try {
this.df.validate(value, this.doc);
} catch (error) {
showMessageDialog({
message: this._('Invalid Value'),
description: error.message,
});
return false;
}
return true;
},
parse(value) { parse(value) {
return value; return value;
}, },
isNumeric(df) { isNumeric(df) {
return ['Int', 'Float', 'Currency'].includes(df.fieldtype); return ['Int', 'Float', 'Currency'].includes(df.fieldtype);
} },
} },
}; };
</script> </script>

View File

@ -4,11 +4,16 @@ import Int from './Int';
export default { export default {
name: 'Float', name: 'Float',
extends: Int, extends: Int,
computed: {
inputType() {
return 'number';
}
},
methods: { methods: {
parse(value) { parse(value) {
let parsedValue = parseFloat(value); let parsedValue = parseFloat(value);
return isNaN(parsedValue) ? 0 : parsedValue; return isNaN(parsedValue) ? 0 : parsedValue;
} },
} },
}; };
</script> </script>

View File

@ -4,11 +4,16 @@ import Data from './Data';
export default { export default {
name: 'Int', name: 'Int',
extends: Data, extends: Data,
computed: {
inputType() {
return 'number';
},
},
methods: { methods: {
parse(value) { parse(value) {
let parsedValue = parseInt(value, 10); let parsedValue = parseInt(value, 10);
return isNaN(parsedValue) ? 0 : parsedValue; return isNaN(parsedValue) ? 0 : parsedValue;
} },
} },
}; };
</script> </script>