UNPKG

@zohodesk/utils

Version:

Unified Component Library - Utils

35 lines (32 loc) 1.63 kB
import PropTypes from 'prop-types'; /** * tagGeneralAttributes - is to spread the global attributes in component. */ export const tagGeneralAttributes = "tagGeneralAttributes"; const attributesList = ["accesskey", "contentEditable", "dir", "draggable", "hidden", "lang", "tabIndex", "title", "spellcheck", "translate"]; const dataAttribute = "^data|on"; export const tagGeneralAttributesPropType = 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 tagGeneralAttributes, component: ${componentName}`); } }); }; export const tagGeneralAttributesShape = 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 { [tagGeneralAttributes]: tagGeneralAttributesPropType };