From 68e2f08fa5e8b941ac679f574944361c3c4e8885 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 16 Jul 2024 12:13:45 +1000 Subject: [PATCH] Added onInput event for fields in forms - fix for issue #7600 (#7660) (#7664) An onInput event is added for fields in forms that gets triggered everytime an input is detected in the field (cherry picked from commit a3103cf5689e5003db709e59b41fe1ea489d561a) Co-authored-by: Roche Christopher --- .../templates/js/translated/forms.js | 20 ++++++++++++++++++- .../templates/js/translated/purchase_order.js | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/backend/InvenTree/templates/js/translated/forms.js b/src/backend/InvenTree/templates/js/translated/forms.js index 3f5539b7d8..f0d186c0ec 100644 --- a/src/backend/InvenTree/templates/js/translated/forms.js +++ b/src/backend/InvenTree/templates/js/translated/forms.js @@ -298,7 +298,8 @@ function constructDeleteForm(fields, options) { * - closeText: Text for the "close" button * - fields: list of fields to display, with the following options * - filters: API query filters - * - onEdit: callback or array of callbacks which get fired when field is edited + * - onEdit: callback or array of callbacks which get fired when field is edited - does not get triggered until the field loses focus, ref: https://api.jquery.com/change/ + * - onInput: callback or array of callbacks which get fired when an input is detected in the field * - secondary: Define a secondary modal form for this field * - label: Specify custom label * - help_text: Specify custom help_text @@ -1642,6 +1643,23 @@ function addFieldCallback(name, field, options) { }); } + if(field.onInput){ + + el.on('input', function(){ + var value = getFormFieldValue(name, field, options); + let onInputHandlers = field.onInput; + + if (!Array.isArray(onInputHandlers)) { + onInputHandlers = [onInputHandlers]; + } + + for (const onInput of onInputHandlers) { + onInput(value, name, field, options); + } + }); + + } + // attach field callback for nested fields if(field.type === "nested object") { for (const [c_name, c_field] of Object.entries(field.children)) { diff --git a/src/backend/InvenTree/templates/js/translated/purchase_order.js b/src/backend/InvenTree/templates/js/translated/purchase_order.js index c829fed140..8aee04532e 100644 --- a/src/backend/InvenTree/templates/js/translated/purchase_order.js +++ b/src/backend/InvenTree/templates/js/translated/purchase_order.js @@ -343,7 +343,7 @@ function poLineItemFields(options={}) { reference: {}, purchase_price: { icon: 'fa-dollar-sign', - onEdit: function(value, name, field, opts) { + onInput: function(value, name, field, opts) { updateFieldValue('auto_pricing', value === '', {}, opts); } },