UNPKG

@zohodesk/utils

Version:

Unified Component Library - Utils

35 lines (32 loc) 1.5 kB
import PropTypes from 'prop-types'; /** * tagAttributes - is used to spread these attributes for events customize and override few attributes. */ export const tagAttributes = "tagAttributes"; const attributesList = ["tabIndex"]; const dataAttribute = "^data|on"; export const tagAttributesPropType = function (props, propName, componentName) { if (Object.prototype.toString.call(props) !== "[object Object]") { console.error(`Warning: Failed prop type: Invalid prop \`${propName}\` of type \`${props ? typeof props : undefined}\` supplied to \`${componentName}\`, expected \`object\`.`); } let curPattern = new RegExp(`^(${attributesList.join("|")})$`); let curPattern2 = new RegExp(dataAttribute); Object.keys(props[propName]).forEach(keyName => { if (!(curPattern.test(keyName) || curPattern2.test(keyName))) { console.warn(`Warning: these attributes ( ${keyName} ) aren't mentioned in ${propName}, component: ${componentName}`); } }); }; export const tagAttributesShape = shapeOf => (props, propName, componentName) => { let propObject = props[propName]; propObject && Object.keys(propObject).forEach(keyName => { if (!shapeOf.hasOwnProperty(keyName)) { console.error(`Warning: these key ( ${keyName} ) aren't allowed in ${propName}, component: ${componentName}`); return; } shapeOf[keyName](propObject, keyName, `${componentName} prop: ${propName}.${keyName} `); }); }; export default { [tagAttributes]: tagAttributesPropType };