1
1
import { Accessibility } from '@fluentui/accessibility'
2
+ import {
3
+ getElementType ,
4
+ getUnhandledProps ,
5
+ useAccessibility ,
6
+ useStyles ,
7
+ } from '@fluentui/react-bindings'
2
8
import * as customPropTypes from '@fluentui/react-proptypes'
3
9
import * as PropTypes from 'prop-types'
4
10
import * as React from 'react'
5
11
import Image , { ImageProps } from '../Image/Image'
6
12
import Label , { LabelProps } from '../Label/Label'
7
13
import Status , { StatusProps } from '../Status/Status'
8
- import { WithAsProp , ShorthandValue , withSafeTypeForAs } from '../../types'
9
- import {
10
- createShorthandFactory ,
11
- UIComponent ,
12
- UIComponentProps ,
13
- commonPropTypes ,
14
- SizeValue ,
15
- ShorthandFactory ,
16
- } from '../../utils'
14
+ import { WithAsProp , ShorthandValue , withSafeTypeForAs , ProviderContextPrepared } from '../../types'
15
+ import { createShorthandFactory , UIComponentProps , commonPropTypes , SizeValue } from '../../utils'
16
+ // @ts -ignore
17
+ import { ThemeContext } from 'react-fela'
17
18
18
19
export interface AvatarProps extends UIComponentProps {
19
20
/**
@@ -40,91 +41,117 @@ export interface AvatarProps extends UIComponentProps {
40
41
getInitials ?: ( name : string ) => string
41
42
}
42
43
43
- class Avatar extends UIComponent < WithAsProp < AvatarProps > , any > {
44
- static create : ShorthandFactory < AvatarProps >
45
-
46
- static className = 'ui-avatar'
47
-
48
- static displayName = 'Avatar'
49
-
50
- static propTypes = {
51
- ...commonPropTypes . createCommon ( {
52
- children : false ,
53
- content : false ,
44
+ const Avatar = React . forwardRef < HTMLDivElement , WithAsProp < AvatarProps > > ( ( props , ref ) => {
45
+ const {
46
+ className,
47
+ design,
48
+ name,
49
+ status,
50
+ image,
51
+ label,
52
+ getInitials,
53
+ size,
54
+ styles,
55
+ variables,
56
+ } = props
57
+
58
+ const { rtl } : ProviderContextPrepared = React . useContext ( ThemeContext )
59
+ const [ classes , resolvedStyles ] = useStyles ( Avatar . displayName , {
60
+ mapPropsToStyles : ( ) => ( {
61
+ size,
54
62
} ) ,
55
- name : PropTypes . string ,
56
- image : customPropTypes . itemShorthandWithoutJSX ,
57
- label : customPropTypes . itemShorthand ,
58
- size : customPropTypes . size ,
59
- status : customPropTypes . itemShorthand ,
60
- getInitials : PropTypes . func ,
61
- }
62
-
63
- static defaultProps = {
64
- size : 'medium' ,
65
- getInitials ( name : string ) {
66
- if ( ! name ) {
67
- return ''
68
- }
69
-
70
- const reducedName = name
71
- . replace ( / \s * \( .* ?\) \s * / g, ' ' )
72
- . replace ( / \s * { .* ?} \s * / g, ' ' )
73
- . replace ( / \s * \[ .* ?] \s * / g, ' ' )
74
-
75
- const initials = reducedName
76
- . split ( ' ' )
77
- . filter ( item => item !== '' )
78
- . map ( item => item . charAt ( 0 ) )
79
- . reduce ( ( accumulator , currentValue ) => accumulator + currentValue )
80
-
81
- if ( initials . length > 2 ) {
82
- return initials . charAt ( 0 ) + initials . charAt ( initials . length - 1 )
83
- }
84
- return initials
85
- } ,
86
- } as AvatarProps
87
-
88
- renderComponent ( { accessibility, ElementType, classes, unhandledProps, styles, variables } ) {
89
- const { name, status, image, label, getInitials, size } = this . props as AvatarProps
90
-
91
- return (
92
- < ElementType { ...accessibility . attributes . root } { ...unhandledProps } className = { classes . root } >
93
- { Image . create ( image , {
63
+ mapInlineToStyles : ( ) => ( {
64
+ className,
65
+ design,
66
+ styles,
67
+ variables,
68
+ } ) ,
69
+ rtl,
70
+ } )
71
+ const getA11Props = useAccessibility ( props . accessibility , {
72
+ debugName : Avatar . displayName ,
73
+ rtl,
74
+ } )
75
+ const ElementType = getElementType ( props )
76
+ const unhandledProps = getUnhandledProps ( ( Avatar as any ) . handledProps /* TODO */ , props )
77
+
78
+ return (
79
+ < ElementType { ...getA11Props ( 'root' , { className : classes . root , ref, ...unhandledProps } ) } >
80
+ { Image . create ( image , {
81
+ defaultProps : ( ) => ( {
82
+ fluid : true ,
83
+ avatar : true ,
84
+ title : name ,
85
+ styles : resolvedStyles . image ,
86
+ } ) ,
87
+ } ) }
88
+ { ! image &&
89
+ Label . create ( label || { } , {
94
90
defaultProps : ( ) => ( {
95
- fluid : true ,
96
- avatar : true ,
91
+ content : getInitials ( name ) ,
92
+ circular : true ,
97
93
title : name ,
98
- styles : styles . image ,
94
+ styles : resolvedStyles . label ,
99
95
} ) ,
100
96
} ) }
101
- { ! image &&
102
- Label . create ( label || { } , {
103
- defaultProps : ( ) => ( {
104
- content : getInitials ( name ) ,
105
- circular : true ,
106
- title : name ,
107
- styles : styles . label ,
108
- } ) ,
109
- } ) }
110
- { Status . create ( status , {
111
- defaultProps : ( ) => ( {
112
- size,
113
- styles : styles . status ,
114
- variables : {
115
- borderColor : variables . statusBorderColor ,
116
- borderWidth : variables . statusBorderWidth ,
117
- } ,
118
- } ) ,
119
- } ) }
120
- </ ElementType >
121
- )
122
- }
97
+ { Status . create ( status , {
98
+ defaultProps : ( ) => ( {
99
+ size,
100
+ styles : resolvedStyles . status ,
101
+ // variables: {
102
+ // borderColor: variables.statusBorderColor,
103
+ // borderWidth: variables.statusBorderWidth,
104
+ // },
105
+ // TODO: Fix me please PLEASE PLEAASSEEE
106
+ } ) ,
107
+ } ) }
108
+ </ ElementType >
109
+ )
110
+ } )
111
+ ; ( Avatar as any ) . className = 'ui-avatar'
112
+ Avatar . displayName = 'Avatar'
113
+ ; ( Avatar as any ) . propTypes = {
114
+ ...commonPropTypes . createCommon ( {
115
+ children : false ,
116
+ content : false ,
117
+ } ) ,
118
+ name : PropTypes . string ,
119
+ image : customPropTypes . itemShorthandWithoutJSX ,
120
+ label : customPropTypes . itemShorthand ,
121
+ size : customPropTypes . size ,
122
+ status : customPropTypes . itemShorthand ,
123
+ getInitials : PropTypes . func ,
124
+ }
125
+ Avatar . defaultProps = {
126
+ size : 'medium' ,
127
+ getInitials ( name : string ) {
128
+ if ( ! name ) {
129
+ return ''
130
+ }
131
+
132
+ const reducedName = name
133
+ . replace ( / \s * \( .* ?\) \s * / g, ' ' )
134
+ . replace ( / \s * { .* ?} \s * / g, ' ' )
135
+ . replace ( / \s * \[ .* ?] \s * / g, ' ' )
136
+
137
+ const initials = reducedName
138
+ . split ( ' ' )
139
+ . filter ( item => item !== '' )
140
+ . map ( item => item . charAt ( 0 ) )
141
+ . reduce ( ( accumulator , currentValue ) => accumulator + currentValue )
142
+
143
+ if ( initials . length > 2 ) {
144
+ return initials . charAt ( 0 ) + initials . charAt ( initials . length - 1 )
145
+ }
146
+ return initials
147
+ } ,
123
148
}
124
149
150
+ // @ts -ignore
125
151
Avatar . create = createShorthandFactory ( { Component : Avatar , mappedProp : 'name' } )
126
152
127
153
/**
128
154
* An Avatar is a graphical representation of a user.
129
155
*/
156
+ // @ts -ignore
130
157
export default withSafeTypeForAs < typeof Avatar , AvatarProps > ( Avatar )
0 commit comments