Here is a possible implementation of what you are trying to do
<TextField v-model="newItem.unitPriceExcl" @textChange="calcPricing" keyboardType="number" @focus="focus" @blur="blur" hint="Unit Price Excl"></TextField>
<TextField v-model="newItem.unitPriceIncl" @textChange="calcPricing" keyboardType="number" @focus="focus" @blur="blur" hint="Unit Price Incl"></TextField>
...
data: () => ({ focusedElement: null })
methods: {
focus({ object }) {
this.focusedElement = object;
},
// You don't have to handle blur depending on your logic, but I find it more consistent
blur({ object }) {
if (this.focusedElement !== object) return;
this.focusedElement = null;
}
}
...
If you don't really want to know which element has the focus, but rather from which element the modification come from. You can do it this way :
<TextField v-model="newItem.unitPriceExcl" @textChange="calcPricing('unitPriceExl')" keyboardType="number" hint="Unit Price Excl"></TextField>
...
methods: {
calcPricing(name) {
return (args) => {
// Your logic goes here, you have access to name, and to args
}
}
}
...
Side note: you can also use some native methods to find which view is currently focused. However note that it's not faster, and also that it's not recommended, the main idea is to used the NS common api.
<TextField v-model="newItem.unitPriceExcl" @textChange="calcPricing" keyboardType="number" hint="Unit Price Excl" ref="unitPriceExcl"></TextField>
...
let UIResponder;
if (isIOS) {
UIResponder = (UIResponder as any).extend({
currentFirstResponder() {
this.currentFirstResponder = null;
UIApplication.sharedApplication.sendActionToFromForEvent('findFirstResponder', null, null, null);
return this.currentFirstResponder;
},
findFirstResponder(application: UIApplication) {
this.currentFirstResponder = new WeakRef(self)
}
}, {
exposedMethods: {
currentFirstResponder: { returns: UIView, params: [ ] }
}
})
}
...
methods: {
getFocusedView() {
if (isAndroid) {
const activity = application.android.foregroundActivity;
if (activity) {
return activity.getCurrentFocus()
}
} else if (isIOS) {
return UIResponder.currentFirstResponder;
}
return null;
},
isFocused(object) {
if (object.nativeView && object.nativeView.nativeView) return false;
return this.getFocusedView() === object.nativeView.nativeView;
},
calcPricing(args) {
if (this.isFocused(this.$refs.unitPriceExcl)) {
console.log('unitPriceExcl is selected');
}
},
}
...