Skip to content

Commit d117cf6

Browse files
committed
feat(ElementType): new wrapper for all elements
1 parent 6ef5af5 commit d117cf6

35 files changed

+697
-275
lines changed

src/elements/Container/Container.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ import React from 'react'
55
import {
66
childrenUtils,
77
customPropTypes,
8-
getElementType,
8+
ElementType,
99
getUnhandledProps,
1010
META,
1111
SUI,
1212
useKeyOnly,
1313
useTextAlignProp,
14+
withComputedType,
1415
} from '../../lib'
1516

1617
/**
1718
* A container limits content to a maximum width.
1819
*/
19-
function Container(props) {
20+
const InnerContainer = (props) => {
2021
const {
2122
children,
2223
className,
@@ -33,8 +34,7 @@ function Container(props) {
3334
'container',
3435
className,
3536
)
36-
const rest = getUnhandledProps(Container, props)
37-
const ElementType = getElementType(Container, props)
37+
const rest = getUnhandledProps(InnerContainer, props, { passAs: true })
3838

3939
return (
4040
<ElementType {...rest} className={classes}>
@@ -43,12 +43,7 @@ function Container(props) {
4343
)
4444
}
4545

46-
Container._meta = {
47-
name: 'Container',
48-
type: META.TYPES.ELEMENT,
49-
}
50-
51-
Container.propTypes = {
46+
InnerContainer.propTypes = {
5247
/** An element type to render as (string or function). */
5348
as: customPropTypes.as,
5449

@@ -71,4 +66,11 @@ Container.propTypes = {
7166
textAlign: PropTypes.oneOf(SUI.TEXT_ALIGNMENTS),
7267
}
7368

69+
const Container = withComputedType()(InnerContainer)
70+
71+
Container._meta = {
72+
name: 'Container',
73+
type: META.TYPES.ELEMENT,
74+
}
75+
7476
export default Container

src/elements/Image/Image.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import {
77
childrenUtils,
88
createShorthandFactory,
99
customPropTypes,
10-
getElementType,
10+
ElementType,
1111
getUnhandledProps,
1212
META,
1313
SUI,
1414
useKeyOnly,
1515
useKeyOrValueAndKey,
1616
useValueAndKey,
1717
useVerticalAlignProp,
18+
withComputedType,
1819
} from '../../lib'
1920
import Dimmer from '../../modules/Dimmer'
2021
import Label from '../Label/Label'
@@ -25,8 +26,9 @@ import ImageGroup from './ImageGroup'
2526
* An image is a graphic representation of something.
2627
* @see Icon
2728
*/
28-
function Image(props) {
29+
const InnerImage = (props) => {
2930
const {
31+
as,
3032
alt,
3133
avatar,
3234
bordered,
@@ -50,7 +52,6 @@ function Image(props) {
5052
src,
5153
verticalAlign,
5254
width,
53-
wrapped,
5455
ui,
5556
} = props
5657

@@ -72,22 +73,19 @@ function Image(props) {
7273
'image',
7374
className,
7475
)
75-
const rest = getUnhandledProps(Image, props)
76-
const ElementType = getElementType(Image, props, () => {
77-
if (!_.isNil(dimmer) || !_.isNil(label) || !_.isNil(wrapped) || !childrenUtils.isNil(children)) return 'div'
78-
})
76+
const rest = getUnhandledProps(InnerImage, props)
7977

8078
if (!childrenUtils.isNil(children)) {
81-
return <ElementType {...rest} className={classes}>{children}</ElementType>
79+
return <ElementType {...rest} as={as} className={classes}>{children}</ElementType>
8280
}
8381
if (!childrenUtils.isNil(content)) {
84-
return <ElementType {...rest} className={classes}>{content}</ElementType>
82+
return <ElementType {...rest} as={as} className={classes}>{content}</ElementType>
8583
}
8684

87-
const rootProps = { ...rest, className: classes }
85+
const rootProps = { ...rest, as, className: classes }
8886
const imgTagProps = { alt, src, height, width }
8987

90-
if (ElementType === 'img') return <ElementType {...rootProps} {...imgTagProps} />
88+
if (as === 'img') return <ElementType {...rootProps} {...imgTagProps} />
9189

9290
return (
9391
<ElementType {...rootProps} href={href}>
@@ -98,14 +96,7 @@ function Image(props) {
9896
)
9997
}
10098

101-
Image.Group = ImageGroup
102-
103-
Image._meta = {
104-
name: 'Image',
105-
type: META.TYPES.ELEMENT,
106-
}
107-
108-
Image.propTypes = {
99+
InnerImage.propTypes = {
109100
/** An element type to render as (string or function). */
110101
as: customPropTypes.as,
111102

@@ -197,11 +188,24 @@ Image.propTypes = {
197188
wrapped: PropTypes.bool,
198189
}
199190

200-
Image.defaultProps = {
191+
InnerImage.defaultProps = {
201192
as: 'img',
202193
ui: true,
203194
}
204195

196+
const computeType = ({ children, dimmer, label, wrapped }) => {
197+
if (!_.isNil(dimmer) || !_.isNil(label) || !_.isNil(wrapped) || !childrenUtils.isNil(children)) return 'div'
198+
}
199+
200+
const Image = withComputedType(computeType)(InnerImage)
201+
202+
Image.Group = ImageGroup
203+
204+
Image._meta = {
205+
name: 'Image',
206+
type: META.TYPES.ELEMENT,
207+
}
208+
205209
Image.create = createShorthandFactory(Image, value => ({ src: value }))
206210

207211
export default Image

src/elements/List/List.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import React, { Component } from 'react'
66
import {
77
childrenUtils,
88
customPropTypes,
9-
getElementType,
9+
ElementType,
1010
getUnhandledProps,
1111
META,
1212
SUI,
1313
useKeyOnly,
1414
useKeyOrValueAndKey,
1515
useValueAndKey,
1616
useVerticalAlignProp,
17+
withComputedType,
1718
} from '../../lib'
1819
import ListContent from './ListContent'
1920
import ListDescription from './ListDescription'
@@ -25,7 +26,7 @@ import ListList from './ListList'
2526
/**
2627
* A list groups related content.
2728
*/
28-
class List extends Component {
29+
class InnerList extends Component {
2930
static propTypes = {
3031
/** An element type to render as (string or function). */
3132
as: customPropTypes.as,
@@ -96,18 +97,6 @@ class List extends Component {
9697
verticalAlign: PropTypes.oneOf(SUI.VERTICAL_ALIGNMENTS),
9798
}
9899

99-
static _meta = {
100-
name: 'List',
101-
type: META.TYPES.ELEMENT,
102-
}
103-
104-
static Content = ListContent
105-
static Description = ListDescription
106-
static Header = ListHeader
107-
static Icon = ListIcon
108-
static Item = ListItem
109-
static List = ListList
110-
111100
handleItemOverrides = predefinedProps => ({
112101
onClick: (e, itemProps) => {
113102
_.invoke(predefinedProps, 'onClick', e, itemProps)
@@ -154,8 +143,7 @@ class List extends Component {
154143
'list',
155144
className,
156145
)
157-
const rest = getUnhandledProps(List, this.props)
158-
const ElementType = getElementType(List, this.props)
146+
const rest = getUnhandledProps(InnerList, this.props, { passAs: true })
159147

160148
if (!childrenUtils.isNil(children)) {
161149
return <ElementType {...rest} role='list' className={classes}>{children}</ElementType>
@@ -173,4 +161,18 @@ class List extends Component {
173161
}
174162
}
175163

164+
const List = withComputedType()(InnerList)
165+
166+
List._meta = {
167+
name: 'List',
168+
type: META.TYPES.ELEMENT,
169+
}
170+
171+
List.Content = ListContent
172+
List.Description = ListDescription
173+
List.Header = ListHeader
174+
List.Icon = ListIcon
175+
List.Item = ListItem
176+
List.List = ListList
177+
176178
export default List

src/elements/List/ListContent.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@ import {
66
childrenUtils,
77
createShorthandFactory,
88
customPropTypes,
9-
getElementType,
9+
ElementType,
1010
getUnhandledProps,
1111
META,
1212
SUI,
1313
useValueAndKey,
1414
useVerticalAlignProp,
15+
withComputedType,
1516
} from '../../lib'
1617
import ListDescription from './ListDescription'
1718
import ListHeader from './ListHeader'
1819

1920
/**
2021
* A list item can contain a content.
2122
*/
22-
function ListContent(props) {
23+
const InnerListContent = (props) => {
2324
const {
2425
children,
2526
className,
@@ -36,8 +37,7 @@ function ListContent(props) {
3637
'content',
3738
className,
3839
)
39-
const rest = getUnhandledProps(ListContent, props)
40-
const ElementType = getElementType(ListContent, props)
40+
const rest = getUnhandledProps(InnerListContent, props, { passAs: true })
4141

4242
if (!childrenUtils.isNil(children)) return <ElementType {...rest} className={classes}>{children}</ElementType>
4343

@@ -50,13 +50,7 @@ function ListContent(props) {
5050
)
5151
}
5252

53-
ListContent._meta = {
54-
name: 'ListContent',
55-
parent: 'List',
56-
type: META.TYPES.ELEMENT,
57-
}
58-
59-
ListContent.propTypes = {
53+
InnerListContent.propTypes = {
6054
/** An element type to render as (string or function). */
6155
as: customPropTypes.as,
6256

@@ -82,6 +76,14 @@ ListContent.propTypes = {
8276
verticalAlign: PropTypes.oneOf(SUI.VERTICAL_ALIGNMENTS),
8377
}
8478

79+
const ListContent = withComputedType()(InnerListContent)
80+
81+
ListContent._meta = {
82+
name: 'ListContent',
83+
parent: 'List',
84+
type: META.TYPES.ELEMENT,
85+
}
86+
8587
ListContent.create = createShorthandFactory(ListContent, content => ({ content }))
8688

8789
export default ListContent

src/elements/List/ListDescription.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ import {
66
childrenUtils,
77
createShorthandFactory,
88
customPropTypes,
9-
getElementType,
9+
ElementType,
1010
getUnhandledProps,
1111
META,
12+
withComputedType,
1213
} from '../../lib'
1314

1415
/**
1516
* A list item can contain a description.
1617
*/
17-
function ListDescription(props) {
18+
const InnerListDescription = (props) => {
1819
const { children, className, content } = props
1920
const classes = cx(className, 'description')
20-
const rest = getUnhandledProps(ListDescription, props)
21-
const ElementType = getElementType(ListDescription, props)
21+
const rest = getUnhandledProps(InnerListDescription, props, { passAs: true })
2222

2323
return (
2424
<ElementType {...rest} className={classes}>
@@ -27,13 +27,7 @@ function ListDescription(props) {
2727
)
2828
}
2929

30-
ListDescription._meta = {
31-
name: 'ListDescription',
32-
parent: 'List',
33-
type: META.TYPES.ELEMENT,
34-
}
35-
36-
ListDescription.propTypes = {
30+
InnerListDescription.propTypes = {
3731
/** An element type to render as (string or function). */
3832
as: customPropTypes.as,
3933

@@ -47,6 +41,14 @@ ListDescription.propTypes = {
4741
content: customPropTypes.contentShorthand,
4842
}
4943

44+
const ListDescription = withComputedType()(InnerListDescription)
45+
46+
ListDescription._meta = {
47+
name: 'ListDescription',
48+
parent: 'List',
49+
type: META.TYPES.ELEMENT,
50+
}
51+
5052
ListDescription.create = createShorthandFactory(ListDescription, content => ({ content }))
5153

5254
export default ListDescription

0 commit comments

Comments
 (0)