Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MARKET-1835 [Data Grid] Create validation function for dimensions #392

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions components/bl-data-grid-component/src/use-styles.js
Original file line number Diff line number Diff line change
@@ -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 });
Expand All @@ -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 normalizeDimensionValue(value) {
return normalizePropertyValue(value, ['px', 'rem', 'em', '%', 'cm', 'mm', 'in', 'pt', 'pc', 'vh', 'vw', 'ch', 'vmin', 'vmax'], 'px');
}