Skip to content

Commit

Permalink
task/wp-105: create common utils function (#850)
Browse files Browse the repository at this point in the history
* task/wp-105: create common utils function

* add check is not a string

* prettier

* run prettier

* removed null return, renamed function

* prettier

---------

Co-authored-by: Sal Tijerina <[email protected]>
  • Loading branch information
2 people authored and chandra-tacc committed Sep 13, 2023
1 parent d36ba9f commit ccccf91
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
12 changes: 2 additions & 10 deletions client/src/components/_common/Button/Button.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Icon from '../Icon';

import styles from './Button.module.css';
import LoadingSpinner from '_common/LoadingSpinner';
import emptyStringValidator from '_common/CommonUtils';

export const TYPE_MAP = {
primary: 'primary',
Expand All @@ -25,15 +26,6 @@ export const SIZES = [''].concat(Object.keys(SIZE_MAP));

export const ATTRIBUTES = ['button', 'submit', 'reset'];

function isNotEmptyString(props, propName, componentName) {
if (!props[propName] || props[propName].replace(/ /g, '') === '') {
return new Error(
`No text passed to <${componentName}> prop "${propName}". Validation failed.`
);
}
return null;
}

const Button = ({
children,
className,
Expand Down Expand Up @@ -125,7 +117,7 @@ const Button = ({
);
};
Button.propTypes = {
children: isNotEmptyString,
children: emptyStringValidator,
className: PropTypes.string,
iconNameBefore: PropTypes.string,
iconNameAfter: PropTypes.string,
Expand Down
19 changes: 19 additions & 0 deletions client/src/components/_common/CommonUtils.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Checks that the field is a non-empty string
* @param {Object} props -
* @param {String} propName - name of the property
* @param {String} componentName - name of the component
* @returns {String} Message if error, otherwise null
*/

export default function emptyStringValidator(props, propName, componentName) {
if (
!props[propName] ||
typeof props[propName] !== 'string' ||
props[propName].replace(/ /g, '') === ''
) {
return new Error(
`No text passed to <${componentName}> prop "${propName}". Validation failed.`
);
}
}
10 changes: 2 additions & 8 deletions client/src/components/_common/Sidebar/Sidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ import { NavLink as RRNavLink } from 'react-router-dom';
import { Nav, NavItem, NavLink } from 'reactstrap';
import Icon from '_common/Icon';
import styles from './Sidebar.module.css';

function isNotEmptyString(props, propName, componentName) {
if (!props[propName] || props[propName].replace(/ /g, '') === '') {
return new Error(`No text passed to ${componentName}. Validation failed.`);
}
return null;
}
import emptyStringValidator from '_common/CommonUtils';

const SidebarItem = ({ to, iconName, label, children, disabled, hidden }) => {
return (
Expand Down Expand Up @@ -38,7 +32,7 @@ const SidebarItem = ({ to, iconName, label, children, disabled, hidden }) => {
SidebarItem.propTypes = {
to: PropTypes.string.isRequired,
iconName: PropTypes.string.isRequired,
label: isNotEmptyString,
label: emptyStringValidator,
children: PropTypes.node,
disabled: PropTypes.bool,
hidden: PropTypes.bool,
Expand Down

0 comments on commit ccccf91

Please sign in to comment.