From e044b0d638570d1809da5fd1e3b5622ecefa3993 Mon Sep 17 00:00:00 2001 From: Zakhar Mychka Date: Thu, 16 Mar 2023 20:22:05 +0200 Subject: [PATCH 1/2] create normalizeDimenstionProperty function --- .../bl-data-grid-component/src/use-styles.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/components/bl-data-grid-component/src/use-styles.js b/components/bl-data-grid-component/src/use-styles.js index 14d89dca9..e0ebcfb8d 100644 --- a/components/bl-data-grid-component/src/use-styles.js +++ b/components/bl-data-grid-component/src/use-styles.js @@ -1,6 +1,7 @@ import { useMemo } from 'react'; const ONLY_NUMERIC_REGEX = /^\d+$/; +const SUFFIX_REGEX = /[a-zA-Z%]+$/; export const useStyles = (style, width, height) => useMemo(() => { const validStyles = validateStyles({ width, height }); @@ -25,3 +26,23 @@ const validateStyles = dimensions => { return acc; }, {}); }; + +function normalizePropertyValue(value, allowedSuffixes, defaultSuffix) { + const suffix = value.match(SUFFIX_REGEX); + + if (suffix && allowedSuffixes.includes(suffix[0])) { + return value; + } + + if (suffix && !allowedSuffixes.includes(suffix[0])) { + return parseFloat(value) + defaultSuffix; + } + + if (ONLY_NUMERIC_REGEX.test(value)) { + return value + defaultSuffix; + } +} + +function normalizeDimenstionProperty(value) { + return normalizePropertyValue(value, ['px', 'rem', 'em', '%', 'cm', 'mm', 'in', 'pt', 'pc', 'vh', 'vw', 'ch', 'vmin', 'vmax'], 'px'); +} From f0ed19a22aa26f13e71346a1ad36b98556a541a0 Mon Sep 17 00:00:00 2001 From: Zakhar Mychka Date: Fri, 24 Mar 2023 18:31:07 +0200 Subject: [PATCH 2/2] minor fix --- components/bl-data-grid-component/src/use-styles.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/bl-data-grid-component/src/use-styles.js b/components/bl-data-grid-component/src/use-styles.js index e0ebcfb8d..64cc7be2a 100644 --- a/components/bl-data-grid-component/src/use-styles.js +++ b/components/bl-data-grid-component/src/use-styles.js @@ -43,6 +43,6 @@ function normalizePropertyValue(value, allowedSuffixes, defaultSuffix) { } } -function normalizeDimenstionProperty(value) { +function normalizeDimensionValue(value) { return normalizePropertyValue(value, ['px', 'rem', 'em', '%', 'cm', 'mm', 'in', 'pt', 'pc', 'vh', 'vw', 'ch', 'vmin', 'vmax'], 'px'); }