11import cx from 'clsx'
2+ import _ from 'lodash'
23import PropTypes from 'prop-types'
3- import React , { Component } from 'react'
4+ import React from 'react'
45
56import {
67 childrenUtils ,
@@ -9,6 +10,7 @@ import {
910 getUnhandledProps ,
1011 SUI ,
1112 useKeyOnly ,
13+ useEventCallback ,
1214} from '../../lib'
1315import Image from '../../elements/Image'
1416import CardContent from './CardContent'
@@ -20,80 +22,79 @@ import CardMeta from './CardMeta'
2022/**
2123 * A card displays site content in a manner similar to a playing card.
2224 */
23- export default class Card extends Component {
24- handleClick = ( e ) => {
25- const { onClick } = this . props
25+ const Card = React . forwardRef ( function ( props , ref ) {
26+ const {
27+ centered,
28+ children,
29+ className,
30+ color,
31+ content,
32+ description,
33+ extra,
34+ fluid,
35+ header,
36+ href,
37+ image,
38+ link,
39+ meta,
40+ onClick,
41+ raised,
42+ } = props
43+
44+ const classes = cx (
45+ 'ui' ,
46+ color ,
47+ useKeyOnly ( centered , 'centered' ) ,
48+ useKeyOnly ( fluid , 'fluid' ) ,
49+ useKeyOnly ( link , 'link' ) ,
50+ useKeyOnly ( raised , 'raised' ) ,
51+ 'card' ,
52+ className ,
53+ )
54+ const rest = getUnhandledProps ( Card , props )
55+ const ElementType = getElementType ( Card , props , ( ) => {
56+ if ( onClick ) {
57+ return 'a'
58+ }
59+ } )
2660
27- if ( onClick ) onClick ( e , this . props )
28- }
61+ const handleClick = useEventCallback ( ( e ) => {
62+ _ . invoke ( props , 'onClick' , e , props )
63+ } )
2964
30- render ( ) {
31- const {
32- centered,
33- children,
34- className,
35- color,
36- content,
37- description,
38- extra,
39- fluid,
40- header,
41- href,
42- image,
43- link,
44- meta,
45- onClick,
46- raised,
47- } = this . props
48-
49- const classes = cx (
50- 'ui' ,
51- color ,
52- useKeyOnly ( centered , 'centered' ) ,
53- useKeyOnly ( fluid , 'fluid' ) ,
54- useKeyOnly ( link , 'link' ) ,
55- useKeyOnly ( raised , 'raised' ) ,
56- 'card' ,
57- className ,
65+ if ( ! childrenUtils . isNil ( children ) ) {
66+ return (
67+ < ElementType { ...rest } className = { classes } href = { href } onClick = { handleClick } ref = { ref } >
68+ { children }
69+ </ ElementType >
5870 )
59- const rest = getUnhandledProps ( Card , this . props )
60- const ElementType = getElementType ( Card , this . props , ( ) => {
61- if ( onClick ) return 'a'
62- } )
63-
64- if ( ! childrenUtils . isNil ( children ) ) {
65- return (
66- < ElementType { ...rest } className = { classes } href = { href } onClick = { this . handleClick } >
67- { children }
68- </ ElementType >
69- )
70- }
71- if ( ! childrenUtils . isNil ( content ) ) {
72- return (
73- < ElementType { ...rest } className = { classes } href = { href } onClick = { this . handleClick } >
74- { content }
75- </ ElementType >
76- )
77- }
78-
71+ }
72+ if ( ! childrenUtils . isNil ( content ) ) {
7973 return (
80- < ElementType { ...rest } className = { classes } href = { href } onClick = { this . handleClick } >
81- { Image . create ( image , {
82- autoGenerateKey : false ,
83- defaultProps : {
84- ui : false ,
85- wrapped : true ,
86- } ,
87- } ) }
88- { ( description || header || meta ) && (
89- < CardContent description = { description } header = { header } meta = { meta } />
90- ) }
91- { extra && < CardContent extra > { extra } </ CardContent > }
74+ < ElementType { ...rest } className = { classes } href = { href } onClick = { handleClick } ref = { ref } >
75+ { content }
9276 </ ElementType >
9377 )
9478 }
95- }
9679
80+ return (
81+ < ElementType { ...rest } className = { classes } href = { href } onClick = { handleClick } ref = { ref } >
82+ { Image . create ( image , {
83+ autoGenerateKey : false ,
84+ defaultProps : {
85+ ui : false ,
86+ wrapped : true ,
87+ } ,
88+ } ) }
89+ { ( description || header || meta ) && (
90+ < CardContent description = { description } header = { header } meta = { meta } />
91+ ) }
92+ { extra && < CardContent extra > { extra } </ CardContent > }
93+ </ ElementType >
94+ )
95+ } )
96+
97+ Card . displayName = 'Card'
9798Card . propTypes = {
9899 /** An element type to render as (string or function). */
99100 as : PropTypes . elementType ,
@@ -155,3 +156,5 @@ Card.Description = CardDescription
155156Card . Group = CardGroup
156157Card . Header = CardHeader
157158Card . Meta = CardMeta
159+
160+ export default Card
0 commit comments