Repository URL to install this package:
|
Version:
1.1.16 ▾
|
import React from 'react'
import { isObj, isFunction, isNil, hasPrototype } from 'exotic'
// @note - this is a list of identifiers too... selectors tho
import { toComponentName } from 'modules/identifier'
// import { toComponentName } from '../deps/toComponentName'
import { hasNoSelectors } from './selectors'
const matchAllNonWords = /\W+/g
let count = 0
let viewRegistry = new WeakMap()
function getCountFor(thisArg) {
if (viewRegistry.has(thisArg)) {
return viewRegistry.get(thisArg)
} else {
count += 1
viewRegistry.set(thisArg, count)
return count
}
}
function getSelector() {
const name = toComponentName(this)
let selector = name
if (!isNil(this.props.selector)) {
selector += '-' + this.props.selector + ' x'
}
// if (!isNil(this.props.key)) {
// selector += '-' + this.props.key + ' x'
// }
// encodeURIComponent(selector)
selector = selector.replace(matchAllNonWords, '_')
return selector + getCountFor(this)
}
/**
* @description todo
* @param {string} id id can be a string, or the Target class from decorator (decorator options vs none)
* @return {Function | String | ClassDecorator}
*/
function styledSelector(id) {
function extendTarget(Target) {
// if (!hasPrototype(Target)) {
// // validate
// throw new Error('must pass in a class, passed in ' + JSON.stringify(Target))
// }
if (isFunction(Target.getSelector)) {
// already insantiated
return Target
}
// all react instances are frozen
if (!Object.isExtensible(Target)) {
return getSelector.call(Target, Target)
}
// on class
if (isObj(Target.prototype)) {
Target.prototype.getSelector = getSelector
}
// on instance & static
Target.getSelector = getSelector
return Target
}
if (isObj(id)) {
return extendTarget(id)
} else {
// if isString(id)
return extendTarget
}
}
export {
styledSelector,
styledSelector as decorateWithStyledSelector,
styledSelector as withSelector,
}
export default styledSelector