From 1942a3a920111c7040165d395fa56234a9a73a49 Mon Sep 17 00:00:00 2001 From: Blazej Kustra Date: Tue, 5 Sep 2023 18:26:35 +0200 Subject: [PATCH 1/3] [TS migration] Migrate 'calculateAnchorPosition.js' lib to TypeScript --- src/libs/calculateAnchorPosition.js | 26 ----------------------- src/libs/calculateAnchorPosition.ts | 32 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 26 deletions(-) delete mode 100644 src/libs/calculateAnchorPosition.js create mode 100644 src/libs/calculateAnchorPosition.ts diff --git a/src/libs/calculateAnchorPosition.js b/src/libs/calculateAnchorPosition.js deleted file mode 100644 index c886c9ac3712..000000000000 --- a/src/libs/calculateAnchorPosition.js +++ /dev/null @@ -1,26 +0,0 @@ -import lodashGet from 'lodash/get'; -import CONST from '../CONST'; - -/** - * Gets the x,y position of the passed in component for the purpose of anchoring another component to it. - * - * @param {Element} anchorComponent - * @param {{horizontal: string, vertical: string}} anchorOriginValue - Optional parameter - * @return {Promise} - */ -export default function calculateAnchorPosition(anchorComponent, anchorOriginValue) { - return new Promise((resolve) => { - if (!anchorComponent) { - return resolve({horizontal: 0, vertical: 0}); - } - anchorComponent.measureInWindow((x, y, width, height) => { - if ( - lodashGet(anchorOriginValue, 'vertical') === CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.TOP && - lodashGet(anchorOriginValue, 'horizontal') === CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.LEFT - ) { - return resolve({horizontal: x, vertical: y + height + lodashGet(anchorOriginValue, 'shiftVertical', 0)}); - } - return resolve({horizontal: x + width, vertical: y}); - }); - }); -} diff --git a/src/libs/calculateAnchorPosition.ts b/src/libs/calculateAnchorPosition.ts new file mode 100644 index 000000000000..0fd96e0824fe --- /dev/null +++ b/src/libs/calculateAnchorPosition.ts @@ -0,0 +1,32 @@ +/* eslint-disable no-console */ +import {ValueOf} from 'type-fest'; +import {View} from 'react-native'; +import CONST from '../CONST'; + +type AnchorOrigin = { + horizontal: ValueOf; + vertical: ValueOf; + shiftVertical?: number; +}; + +type AnchorPosition = { + horizontal: number; + vertical: number; +}; + +/** + * Gets the x,y position of the passed in component for the purpose of anchoring another component to it. + */ +export default function calculateAnchorPosition(anchorComponent: View, anchorOrigin: AnchorOrigin): Promise { + return new Promise((resolve) => { + if (!anchorComponent) { + return resolve({horizontal: 0, vertical: 0}); + } + anchorComponent.measureInWindow((x, y, width, height) => { + if (anchorOrigin.vertical === CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.TOP && anchorOrigin.horizontal === CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.LEFT) { + return resolve({horizontal: x, vertical: y + height + (anchorOrigin?.shiftVertical ?? 0)}); + } + return resolve({horizontal: x + width, vertical: y}); + }); + }); +} From e643af0ca6e7e26efed785550038d85d839131d6 Mon Sep 17 00:00:00 2001 From: Blazej Kustra Date: Wed, 6 Sep 2023 09:35:17 +0200 Subject: [PATCH 2/3] Make the function more safe --- src/libs/calculateAnchorPosition.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/calculateAnchorPosition.ts b/src/libs/calculateAnchorPosition.ts index 0fd96e0824fe..36949d99c008 100644 --- a/src/libs/calculateAnchorPosition.ts +++ b/src/libs/calculateAnchorPosition.ts @@ -23,7 +23,7 @@ export default function calculateAnchorPosition(anchorComponent: View, anchorOri return resolve({horizontal: 0, vertical: 0}); } anchorComponent.measureInWindow((x, y, width, height) => { - if (anchorOrigin.vertical === CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.TOP && anchorOrigin.horizontal === CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.LEFT) { + if (anchorOrigin?.vertical === CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.TOP && anchorOrigin?.horizontal === CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.LEFT) { return resolve({horizontal: x, vertical: y + height + (anchorOrigin?.shiftVertical ?? 0)}); } return resolve({horizontal: x + width, vertical: y}); From 26dc12444663a7f2f2a76b3b40ceb28561e02634 Mon Sep 17 00:00:00 2001 From: Blazej Kustra Date: Mon, 11 Sep 2023 16:15:05 +0200 Subject: [PATCH 3/3] Make anchorOrigin optional --- src/libs/calculateAnchorPosition.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/calculateAnchorPosition.ts b/src/libs/calculateAnchorPosition.ts index 36949d99c008..39fb3032ee09 100644 --- a/src/libs/calculateAnchorPosition.ts +++ b/src/libs/calculateAnchorPosition.ts @@ -17,7 +17,7 @@ type AnchorPosition = { /** * Gets the x,y position of the passed in component for the purpose of anchoring another component to it. */ -export default function calculateAnchorPosition(anchorComponent: View, anchorOrigin: AnchorOrigin): Promise { +export default function calculateAnchorPosition(anchorComponent: View, anchorOrigin?: AnchorOrigin): Promise { return new Promise((resolve) => { if (!anchorComponent) { return resolve({horizontal: 0, vertical: 0});