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

fix(Comment): spread image props on CommentAvatar #3260

Merged
merged 1 commit into from
Nov 6, 2018
Merged
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
5 changes: 2 additions & 3 deletions src/elements/Image/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
customPropTypes,
getElementType,
getUnhandledProps,
htmlImageProps,
partitionHTMLProps,
SUI,
useKeyOnly,
Expand All @@ -20,8 +21,6 @@ import Dimmer from '../../modules/Dimmer'
import Label from '../Label/Label'
import ImageGroup from './ImageGroup'

const imageProps = ['alt', 'height', 'src', 'srcSet', 'width']
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved to lib because is reusable.


/**
* An image is a graphic representation of something.
* @see Icon
Expand Down Expand Up @@ -70,7 +69,7 @@ function Image(props) {
className,
)
const rest = getUnhandledProps(Image, props)
const [imgTagProps, rootProps] = partitionHTMLProps(rest, { htmlProps: imageProps })
const [imgTagProps, rootProps] = partitionHTMLProps(rest, { htmlProps: htmlImageProps })
const ElementType = getElementType(Image, props, () => {
if (
!_.isNil(dimmer) ||
Expand Down
2 changes: 2 additions & 0 deletions src/lib/htmlPropsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export const htmlInputEvents = [

export const htmlInputProps = [...htmlInputAttrs, ...htmlInputEvents]

export const htmlImageProps = ['alt', 'height', 'src', 'srcSet', 'width']

/**
* Returns an array of objects consisting of: props of html input element and rest.
* @param {object} props A ReactElement props object
Expand Down
1 change: 1 addition & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export {
htmlInputAttrs,
htmlInputEvents,
htmlInputProps,
htmlImageProps,
partitionHTMLProps,
} from './htmlPropsUtils'

Expand Down
15 changes: 12 additions & 3 deletions src/views/Comment/CommentAvatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@ import cx from 'classnames'
import PropTypes from 'prop-types'
import React from 'react'

import { createHTMLImage, customPropTypes, getElementType, getUnhandledProps } from '../../lib'
import {
createHTMLImage,
customPropTypes,
getElementType,
getUnhandledProps,
htmlImageProps,
partitionHTMLProps,
} from '../../lib'

/**
* A comment can contain an image or avatar.
*/
function CommentAvatar(props) {
const { className, src } = props

const classes = cx('avatar', className)
const rest = getUnhandledProps(CommentAvatar, props)
const [imageProps, rootProps] = partitionHTMLProps(rest, { htmlProps: htmlImageProps })
const ElementType = getElementType(CommentAvatar, props)

return (
<ElementType {...rest} className={classes}>
{createHTMLImage(src, { autoGenerateKey: false })}
<ElementType {...rootProps} className={classes}>
{createHTMLImage(src, { autoGenerateKey: false, defaultProps: imageProps })}
</ElementType>
)
}
Expand Down
4 changes: 2 additions & 2 deletions test/specs/elements/Image/Image-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react'

import Image from 'src/elements/Image/Image'
import ImageGroup from 'src/elements/Image/ImageGroup'
import { SUI } from 'src/lib'
import { htmlImageProps, SUI } from 'src/lib'
import Dimmer from 'src/modules/Dimmer/Dimmer'
import * as common from 'test/specs/commonTests'

Expand Down Expand Up @@ -56,7 +56,7 @@ describe('Image', () => {
})

describe('image props', () => {
_.forEach(['alt', 'height', 'src', 'srcSet', 'width'], (propName) => {
_.forEach(htmlImageProps, (propName) => {
it(`keeps "${propName}" on root element by default`, () => {
const wrapper = shallow(<Image {...{ [propName]: 'foo' }} />)

Expand Down
24 changes: 20 additions & 4 deletions test/specs/views/Comment/CommentAvatar-test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import faker from 'faker'
import _ from 'lodash'
import React from 'react'

import { htmlImageProps } from 'src/lib'
import CommentAvatar from 'src/views/Comment/CommentAvatar'
import * as common from 'test/specs/commonTests'

describe('CommentAvatar', () => {
common.isConformant(CommentAvatar)

describe('src', () => {
it('renders img', () => {
const url = faker.image.imageUrl()
shallow(<CommentAvatar src={url} />)
.should.contain(<img src={url} />)
it('passes to the "img" element', () => {
const src = faker.image.imageUrl()
const image = shallow(<CommentAvatar src={src} />).find('img')

image.should.have.prop('src', src)
})
})

describe('image props', () => {
_.forEach(htmlImageProps, (propName) => {
it(`passes "${propName}" to the "img" element`, () => {
const propValue = faker.lorem.word()
const image = shallow(<CommentAvatar src='foo.jpg' {...{ [propName]: propValue }} />).find(
'img',
)

image.should.have.prop(propName, propValue)
})
})
})
})