Start sharing components as a team!Share components as a team!Join Bit to build your applications faster.Get Started Free

get-element-type

v0.86.0arrow_drop_down
v0.86.0
v0.85.0
v0.84.0
STATUS
Passing
DOWNLOADS
10,921
LICENSE
MIT
VISIBILITY
Public
PUBLISHED
9 months ago
SIZE
538 B
Returns a createElement() type based on the props of the Component. Useful for calculating what type a component should render as.
1 contributor
Use get-element-type in your project ?
Copied
npm i @bit/semantic-org.semantic-ui-react.utils.get-element-type
Set Bit as a scoped registryLearn more
npm config set '@bit:registry' https://node.bit.dev
Files
getElementType.js
37 Lines(11 sloc)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
 * Returns a createElement() type based on the props of the Component.
 * Useful for calculating what type a component should render as.
 *
 * @param {function} Component A function or ReactClass.
 * @param {object} props A ReactElement props object
 * @param {function} [getDefault] A function that returns a default element type.
 * @returns {string|function} A ReactElement type
 */
function getElementType(Component, props, getDefault) {
  const { defaultProps = {} } = Component

  // ----------------------------------------
  // user defined "as" element type

  if (props.as && props.as !== defaultProps.as) return props.as

  // ----------------------------------------
  // computed default element type

  if (getDefault) {
    const computedDefault = getDefault()
    if (computedDefault) return computedDefault
  }

  // ----------------------------------------
  // infer anchor links

  if (props.href) return 'a'

  // ----------------------------------------
  // use defaultProp or 'div'

  return defaultProps.as || 'div'
}

export default getElementType