fix: pass getDoc function instead of document.doc to keep the reactivity

(cherry picked from commit db0c0d98bc88710fc8377204e7a927627f9fe8d6)
This commit is contained in:
Shariq Ansari 2025-05-26 16:46:24 +05:30 committed by Mergify
parent b3c1ebb24d
commit d820ff1572

View File

@ -127,9 +127,10 @@ export function getScript(doctype, view = 'Form') {
} }
setupHelperMethods(FormClass, document) setupHelperMethods(FormClass, document)
const getDoc = () => document.doc
if (isChildDoctype) { if (isChildDoctype) {
instance.doc = createDocProxy(document.doc, parentInstance, instance) instance.doc = createDocProxy(getDoc, parentInstance, instance)
if (!parentInstance._childInstances) { if (!parentInstance._childInstances) {
parentInstance._childInstances = [] parentInstance._childInstances = []
@ -137,22 +138,21 @@ export function getScript(doctype, view = 'Form') {
parentInstance._childInstances.push(instance) parentInstance._childInstances.push(instance)
} else { } else {
instance.doc = createDocProxy(document.doc, instance) instance.doc = createDocProxy(getDoc, instance)
} }
return instance return instance
} }
function setupHelperMethods(FormClass, document) { function setupHelperMethods(FormClass) {
if (typeof FormClass.prototype.getRow !== 'function') { if (typeof FormClass.prototype.getRow !== 'function') {
FormClass.prototype.getRow = function (parentField, idx) { FormClass.prototype.getRow = function (parentField, idx) {
let data = document.doc
idx = idx || this.currentRowIdx idx = idx || this.currentRowIdx
let dt = null let dt = null
if (this instanceof Array) { if (this instanceof Array) {
const { getFields } = getMeta(data.doctype) const { getFields } = getMeta(this.doc.doctype)
let fields = getFields() let fields = getFields()
let field = fields.find((f) => f.fieldname === parentField) let field = fields.find((f) => f.fieldname === parentField)
dt = field?.options?.replace(/\s+/g, '') dt = field?.options?.replace(/\s+/g, '')
@ -162,13 +162,13 @@ export function getScript(doctype, view = 'Form') {
} }
} }
if (!data[parentField]) { if (!this.doc[parentField]) {
console.warn( console.warn(
__('⚠️ No data found for parent field: {0}', [parentField]), __('⚠️ No data found for parent field: {0}', [parentField]),
) )
return null return null
} }
const row = data[parentField].find((r) => r.idx === idx) const row = this.doc[parentField].find((r) => r.idx === idx)
if (!row) { if (!row) {
console.warn( console.warn(
@ -180,7 +180,7 @@ export function getScript(doctype, view = 'Form') {
return null return null
} }
row.parent = row.parent || data.name row.parent = row.parent || this.doc.name
if (this instanceof Array && dt) { if (this instanceof Array && dt) {
return createDocProxy( return createDocProxy(
@ -223,11 +223,19 @@ export function getScript(doctype, view = 'Form') {
return FormClass return FormClass
} }
function createDocProxy(data, instance, childInstance = null) { function createDocProxy(source, instance, childInstance = null) {
return new Proxy(data, { const isFunction = typeof source === 'function'
const getCurrentData = () => (isFunction ? source() : source)
return new Proxy(
{},
{
get(target, prop) { get(target, prop) {
const currentDocData = getCurrentData()
if (!currentDocData) return undefined
if (prop === 'trigger') { if (prop === 'trigger') {
if ('trigger' in data) { if (currentDocData && 'trigger' in currentDocData) {
console.warn( console.warn(
__( __(
'⚠️ Avoid using "trigger" as a field name — it conflicts with the built-in trigger() method.', '⚠️ Avoid using "trigger" as a field name — it conflicts with the built-in trigger() method.',
@ -253,13 +261,32 @@ export function getScript(doctype, view = 'Form') {
) )
} }
return target[prop] return currentDocData[prop]
}, },
set(target, prop, value) { set(target, prop, value) {
target[prop] = value const currentDocData = getCurrentData()
if (!currentDocData) return false
currentDocData[prop] = value
return true return true
}, },
}) has(target, prop) {
const currentDocData = getCurrentData()
if (!currentDocData) return false
return prop in currentDocData
},
ownKeys(target) {
const currentDocData = getCurrentData()
if (!currentDocData) return []
return Reflect.ownKeys(currentDocData)
},
getOwnPropertyDescriptor(target, prop) {
const currentDocData = getCurrentData()
if (!currentDocData) return undefined
return Reflect.getOwnPropertyDescriptor(currentDocData, prop)
},
},
)
} }
return { return {