Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ElementType): new wrapper for all elements #2306

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/elements/Container/Container.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import React from 'react'
import {
childrenUtils,
customPropTypes,
getElementType,
ElementType,
getUnhandledProps,
META,
SUI,
useKeyOnly,
useTextAlignProp,
withComputedType,
} from '../../lib'

/**
* A container limits content to a maximum width.
*/
function Container(props) {
const InnerContainer = (props) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. As Container is wrapped with HOC, we can't user prototype.name. So, we can use arrow functions for components
  2. _meta should be exported on the default export, now it's an enhanced component. However, I'm not happy about Inner- prefix

const {
children,
className,
Expand All @@ -33,8 +34,7 @@ function Container(props) {
'container',
className,
)
const rest = getUnhandledProps(Container, props)
const ElementType = getElementType(Container, props)
const rest = getUnhandledProps(InnerContainer, props, { passAs: true })

return (
<ElementType {...rest} className={classes}>
Expand All @@ -43,12 +43,7 @@ function Container(props) {
)
}

Container._meta = {
name: 'Container',
type: META.TYPES.ELEMENT,
}

Container.propTypes = {
InnerContainer.propTypes = {
/** An element type to render as (string or function). */
as: customPropTypes.as,

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

const Container = withComputedType()(InnerContainer)

Container._meta = {
name: 'Container',
type: META.TYPES.ELEMENT,
}

export default Container
44 changes: 24 additions & 20 deletions src/elements/Image/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
childrenUtils,
createShorthandFactory,
customPropTypes,
getElementType,
ElementType,
getUnhandledProps,
META,
partitionHTMLProps,
Expand All @@ -16,6 +16,7 @@ import {
useKeyOrValueAndKey,
useValueAndKey,
useVerticalAlignProp,
withComputedType,
} from '../../lib'
import Dimmer from '../../modules/Dimmer'
import Label from '../Label/Label'
Expand All @@ -27,8 +28,9 @@ const imageProps = ['alt', 'height', 'src', 'srcSet', 'width']
* An image is a graphic representation of something.
* @see Icon
*/
function Image(props) {
const InnerImage = (props) => {
const {
as,
avatar,
bordered,
centered,
Expand All @@ -48,7 +50,6 @@ function Image(props) {
size,
spaced,
verticalAlign,
wrapped,
ui,
} = props

Expand All @@ -70,37 +71,27 @@ function Image(props) {
'image',
className,
)
const rest = getUnhandledProps(Image, props)
const rest = getUnhandledProps(InnerImage, props)
const [imgTagProps, rootProps] = partitionHTMLProps(rest, { htmlProps: imageProps })
const ElementType = getElementType(Image, props, () => {
if (!_.isNil(dimmer) || !_.isNil(label) || !_.isNil(wrapped) || !childrenUtils.isNil(children)) return 'div'
})

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

if (ElementType === 'img') return <ElementType {...rootProps} {...imgTagProps} className={classes} />
if (as === 'img') return <ElementType {...rootProps} {...imgTagProps} as={as} className={classes} />
return (
<ElementType {...rootProps} className={classes} href={href}>
<ElementType {...rootProps} as={as} className={classes} href={href}>
{Dimmer.create(dimmer)}
{Label.create(label)}
<img {...imgTagProps} />
</ElementType>
)
}

Image.Group = ImageGroup

Image._meta = {
name: 'Image',
type: META.TYPES.ELEMENT,
}

Image.propTypes = {
InnerImage.propTypes = {
/** An element type to render as (string or function). */
as: customPropTypes.as,

Expand Down Expand Up @@ -174,11 +165,24 @@ Image.propTypes = {
wrapped: PropTypes.bool,
}

Image.defaultProps = {
InnerImage.defaultProps = {
as: 'img',
ui: true,
}

const computeType = ({ children, dimmer, label, wrapped }) => {
if (!_.isNil(dimmer) || !_.isNil(label) || !_.isNil(wrapped) || !childrenUtils.isNil(children)) return 'div'
}

const Image = withComputedType(computeType)(InnerImage)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type computing function moved to HOC


Image.Group = ImageGroup

Image._meta = {
name: 'Image',
type: META.TYPES.ELEMENT,
}

Image.create = createShorthandFactory(Image, value => ({ src: value }))

export default Image
34 changes: 18 additions & 16 deletions src/elements/List/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import React, { Component } from 'react'
import {
childrenUtils,
customPropTypes,
getElementType,
ElementType,
getUnhandledProps,
META,
SUI,
useKeyOnly,
useKeyOrValueAndKey,
useValueAndKey,
useVerticalAlignProp,
withComputedType,
} from '../../lib'
import ListContent from './ListContent'
import ListDescription from './ListDescription'
Expand All @@ -25,7 +26,7 @@ import ListList from './ListList'
/**
* A list groups related content.
*/
class List extends Component {
class InnerList extends Component {
static propTypes = {
/** An element type to render as (string or function). */
as: customPropTypes.as,
Expand Down Expand Up @@ -96,18 +97,6 @@ class List extends Component {
verticalAlign: PropTypes.oneOf(SUI.VERTICAL_ALIGNMENTS),
}

static _meta = {
name: 'List',
type: META.TYPES.ELEMENT,
}

static Content = ListContent
static Description = ListDescription
static Header = ListHeader
static Icon = ListIcon
static Item = ListItem
static List = ListList

handleItemOverrides = predefinedProps => ({
onClick: (e, itemProps) => {
_.invoke(predefinedProps, 'onClick', e, itemProps)
Expand Down Expand Up @@ -154,8 +143,7 @@ class List extends Component {
'list',
className,
)
const rest = getUnhandledProps(List, this.props)
const ElementType = getElementType(List, this.props)
const rest = getUnhandledProps(InnerList, this.props, { passAs: true })

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

const List = withComputedType()(InnerList)

List._meta = {
name: 'List',
type: META.TYPES.ELEMENT,
}

List.Content = ListContent
List.Description = ListDescription
List.Header = ListHeader
List.Icon = ListIcon
List.Item = ListItem
List.List = ListList

export default List
24 changes: 13 additions & 11 deletions src/elements/List/ListContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ import {
childrenUtils,
createShorthandFactory,
customPropTypes,
getElementType,
ElementType,
getUnhandledProps,
META,
SUI,
useValueAndKey,
useVerticalAlignProp,
withComputedType,
} from '../../lib'
import ListDescription from './ListDescription'
import ListHeader from './ListHeader'

/**
* A list item can contain a content.
*/
function ListContent(props) {
const InnerListContent = (props) => {
const {
children,
className,
Expand All @@ -36,8 +37,7 @@ function ListContent(props) {
'content',
className,
)
const rest = getUnhandledProps(ListContent, props)
const ElementType = getElementType(ListContent, props)
const rest = getUnhandledProps(InnerListContent, props, { passAs: true })

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

Expand All @@ -50,13 +50,7 @@ function ListContent(props) {
)
}

ListContent._meta = {
name: 'ListContent',
parent: 'List',
type: META.TYPES.ELEMENT,
}

ListContent.propTypes = {
InnerListContent.propTypes = {
/** An element type to render as (string or function). */
as: customPropTypes.as,

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

const ListContent = withComputedType()(InnerListContent)

ListContent._meta = {
name: 'ListContent',
parent: 'List',
type: META.TYPES.ELEMENT,
}

ListContent.create = createShorthandFactory(ListContent, content => ({ content }))

export default ListContent
24 changes: 13 additions & 11 deletions src/elements/List/ListDescription.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import {
childrenUtils,
createShorthandFactory,
customPropTypes,
getElementType,
ElementType,
getUnhandledProps,
META,
withComputedType,
} from '../../lib'

/**
* A list item can contain a description.
*/
function ListDescription(props) {
const InnerListDescription = (props) => {
const { children, className, content } = props
const classes = cx(className, 'description')
const rest = getUnhandledProps(ListDescription, props)
const ElementType = getElementType(ListDescription, props)
const rest = getUnhandledProps(InnerListDescription, props, { passAs: true })

return (
<ElementType {...rest} className={classes}>
Expand All @@ -27,13 +27,7 @@ function ListDescription(props) {
)
}

ListDescription._meta = {
name: 'ListDescription',
parent: 'List',
type: META.TYPES.ELEMENT,
}

ListDescription.propTypes = {
InnerListDescription.propTypes = {
/** An element type to render as (string or function). */
as: customPropTypes.as,

Expand All @@ -47,6 +41,14 @@ ListDescription.propTypes = {
content: customPropTypes.contentShorthand,
}

const ListDescription = withComputedType()(InnerListDescription)

ListDescription._meta = {
name: 'ListDescription',
parent: 'List',
type: META.TYPES.ELEMENT,
}

ListDescription.create = createShorthandFactory(ListDescription, content => ({ content }))

export default ListDescription
Loading