-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathindex.tsx
103 lines (87 loc) · 2.7 KB
/
index.tsx
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import * as PropTypes from 'prop-types'
import * as React from 'react'
import Avatar, { AvatarStyle } from './avatar'
import { OptionContext, allOptions } from './options'
export { default as Avatar, AvatarStyle } from './avatar'
export { Option, OptionContext, allOptions } from './options'
import {default as PieceComponent} from './avatar/piece';
export interface Props {
avatarStyle: string
className?: string;
style?: React.CSSProperties
topType?: string
accessoriesType?: string
hairColor?: string
facialHairType?: string
facialHairColor?: string
clotheType?: string
clotheColor?: string
graphicType?: string
eyeType?: string
eyebrowType?: string
mouthType?: string
skinColor?: string
pieceType?:string
pieceSize?:string
viewBox?:string
}
export default class AvatarComponent extends React.Component<Props> {
static childContextTypes = {
optionContext: PropTypes.instanceOf(OptionContext)
}
private optionContext: OptionContext = new OptionContext(allOptions)
getChildContext () {
return { optionContext: this.optionContext }
}
UNSAFE_componentWillMount () {
this.updateOptionContext(this.props)
}
UNSAFE_componentWillReceiveProps (nextProps: Props) {
this.updateOptionContext(nextProps)
}
render () {
const { avatarStyle, style, className } = this.props
return <Avatar avatarStyle={avatarStyle as AvatarStyle} style={style} className={className} />
}
private updateOptionContext (props: Props) {
const data: { [index: string]: string } = {}
for (const option of allOptions) {
const value = props[option.key]
if (!value) {
continue
}
data[option.key] = value
}
this.optionContext.setData(data)
}
}
export class Piece extends React.Component<Props> {
static childContextTypes = {
optionContext: PropTypes.instanceOf(OptionContext)
}
private optionContext: OptionContext = new OptionContext(allOptions)
getChildContext () {
return { optionContext: this.optionContext }
}
UNSAFE_componentWillMount () {
this.updateOptionContext(this.props)
}
UNSAFE_componentWillReceiveProps (nextProps: Props) {
this.updateOptionContext(nextProps)
}
render () {
const { avatarStyle, style, pieceType, pieceSize, viewBox } = this.props
return <PieceComponent avatarStyle={avatarStyle as AvatarStyle} style={style} pieceType={pieceType} pieceSize={pieceSize} viewBox={viewBox}/>
}
private updateOptionContext (props: Props) {
const data: { [index: string]: string } = {}
for (const option of allOptions) {
const value = props[option.key]
if (!value) {
continue
}
data[option.key] = value
}
this.optionContext.setData(data)
}
}