|
| 1 | +import PropTypes from 'prop-types' |
| 2 | + |
| 3 | +function typeToString(prop) { |
| 4 | + const meta = prop.__meta |
| 5 | + if (!meta) return 'unknown' |
| 6 | + const propName = meta.type.name |
| 7 | + if (propName === 'arrayOf') { |
| 8 | + return `[${typeToString(meta.type.value)}]` |
| 9 | + } |
| 10 | + if (propName === 'objectOf') { |
| 11 | + return `{${typeToString(meta.type.value)}}` |
| 12 | + } |
| 13 | + if (propName === 'shape' || propName === 'exact') { |
| 14 | + return `{ ${Object.entries(meta.type.value) |
| 15 | + .map(([name, type]) => `${name}: ${typeToString(type)}`) |
| 16 | + .join(' , ')} }` |
| 17 | + } |
| 18 | + if (propName === 'union') { |
| 19 | + return `${meta.type.value.map(type => typeToString(type)).join(' | ')}` |
| 20 | + } |
| 21 | + if (propName === 'enum') { |
| 22 | + return `${meta.type.value.map(type => type.value).join(' | ')}` |
| 23 | + } |
| 24 | + return propName |
| 25 | +} |
| 26 | + |
| 27 | +const createType = typeDesc => { |
| 28 | + const checkPropType = (...args) => typeDesc.propType(...args) |
| 29 | + checkPropType.__meta = { |
| 30 | + ...typeDesc, |
| 31 | + toString() { |
| 32 | + return typeToString(checkPropType) |
| 33 | + }, |
| 34 | + } |
| 35 | + Object.defineProperties(checkPropType, { |
| 36 | + isRequired: { |
| 37 | + get() { |
| 38 | + return createType({ |
| 39 | + ...typeDesc, |
| 40 | + propType: typeDesc.propType.isRequired, |
| 41 | + required: true, |
| 42 | + }) |
| 43 | + }, |
| 44 | + }, |
| 45 | + defaultTo: { |
| 46 | + get() { |
| 47 | + return value => |
| 48 | + createType({ ...typeDesc, defaultValue: { value: String(value) } }) |
| 49 | + }, |
| 50 | + }, |
| 51 | + desc: { |
| 52 | + get() { |
| 53 | + return description => createType({ ...typeDesc, description }) |
| 54 | + }, |
| 55 | + }, |
| 56 | + }) |
| 57 | + return checkPropType |
| 58 | +} |
| 59 | + |
| 60 | +const type = (propType, name, { value = null } = {}) => { |
| 61 | + return createType({ |
| 62 | + propType, |
| 63 | + type: { |
| 64 | + name, |
| 65 | + value, |
| 66 | + }, |
| 67 | + required: false, |
| 68 | + defaultValue: undefined, |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +export const getMetadata = descriptors => |
| 73 | + Object.keys(descriptors).reduce( |
| 74 | + (props, key) => ({ |
| 75 | + ...props, |
| 76 | + [key]: descriptors[key].__meta, |
| 77 | + }), |
| 78 | + {}, |
| 79 | + ) |
| 80 | + |
| 81 | +export const array = type(PropTypes.array, 'array') |
| 82 | +export const bool = type(PropTypes.bool, 'boolean') |
| 83 | +export const func = type(PropTypes.func, 'function') |
| 84 | +export const number = type(PropTypes.number, 'number') |
| 85 | +export const object = type(PropTypes.object, 'object') |
| 86 | +export const string = type(PropTypes.string, 'string') |
| 87 | +export const symbol = type(PropTypes.symbol, 'symbol') |
| 88 | +export const any = type(PropTypes.any, 'any') |
| 89 | +export const arrayOf = value => |
| 90 | + type(PropTypes.arrayOf(value), 'arrayOf', { value }) |
| 91 | +export const element = type(PropTypes.element, 'element') |
| 92 | +export const elementType = type(PropTypes.elementType, 'elementType') |
| 93 | +export const instanceOf = value => |
| 94 | + type(PropTypes.instanceOf(value), 'instanceOf', { value }) |
| 95 | +export const node = type(PropTypes.node, 'node') |
| 96 | +export const objectOf = value => |
| 97 | + type(PropTypes.objectOf(value), 'objectOf', { value }) |
| 98 | +export const oneOf = values => |
| 99 | + type(PropTypes.oneOf(values), 'enum', { |
| 100 | + value: values.map(value => ({ value })), |
| 101 | + }) |
| 102 | +export const oneOfType = types => |
| 103 | + type(PropTypes.oneOfType(types), 'union', { value: types }) |
| 104 | +export const shape = shape => |
| 105 | + type(PropTypes.shape(shape), 'shape', { |
| 106 | + value: shape, |
| 107 | + }) |
| 108 | +export const exact = shape => |
| 109 | + type(PropTypes.exact(shape), 'exact', { |
| 110 | + value: shape, |
| 111 | + }) |
| 112 | + |
| 113 | +const PropDesc = { |
| 114 | + array, |
| 115 | + bool, |
| 116 | + func, |
| 117 | + number, |
| 118 | + object, |
| 119 | + string, |
| 120 | + symbol, |
| 121 | + any, |
| 122 | + arrayOf, |
| 123 | + element, |
| 124 | + elementType, |
| 125 | + instanceOf, |
| 126 | + node, |
| 127 | + objectOf, |
| 128 | + oneOf, |
| 129 | + oneOfType, |
| 130 | + shape, |
| 131 | + exact, |
| 132 | + getMetadata, |
| 133 | + checkPropTypes: PropTypes.checkPropTypes, |
| 134 | + resetWarningCache: PropTypes.resetWarningCache, |
| 135 | +} |
| 136 | + |
| 137 | +PropDesc.PropTypes = PropDesc |
| 138 | + |
| 139 | +export default PropDesc |
0 commit comments