1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/ui/app/components/ui/jazzicon/jazzicon.component.js
Mark Stacey cb995d66da
Re-render jazzicon component when diameter changes (#8114)
Previously when the `diameter` prop of the `jazzicon` component was
changed, the new diameter would be ignored. The jazzicon is now
redrawn upon each change, as you would expect.

I don't think it's possible for this bug to manifest itself in the
extension. This was discovered through tinkering with the Storybook
for this component.
2020-02-26 09:42:33 -04:00

66 lines
1.6 KiB
JavaScript

import React, { createRef, PureComponent } from 'react'
import PropTypes from 'prop-types'
import jazzicon from 'jazzicon'
import iconFactoryGenerator from '../../../../lib/icon-factory'
const iconFactory = iconFactoryGenerator(jazzicon)
/**
* Wrapper around the jazzicon library to return a React component, as the library returns an
* HTMLDivElement which needs to be appended.
*/
export default class Jazzicon extends PureComponent {
static propTypes = {
address: PropTypes.string.isRequired,
className: PropTypes.string,
diameter: PropTypes.number,
style: PropTypes.object,
}
static defaultProps = {
diameter: 46,
}
container = createRef()
componentDidMount () {
this.appendJazzicon()
}
componentDidUpdate (prevProps) {
const { address: prevAddress, diameter: prevDiameter } = prevProps
const { address, diameter } = this.props
if (address !== prevAddress || diameter !== prevDiameter) {
this.removeExistingChildren()
this.appendJazzicon()
}
}
removeExistingChildren () {
const { children } = this.container.current
for (let i = 0; i < children.length; i++) {
this.container.current.removeChild(children[i])
}
}
appendJazzicon () {
const { address, diameter } = this.props
const image = iconFactory.iconForAddress(address, diameter)
this.container.current.appendChild(image)
}
render () {
const { className, style } = this.props
return (
<div
className={className}
ref={this.container}
style={style}
/>
)
}
}