Skip to content

Commit

Permalink
feat(MidEllipsis): We can now spread any prop and ref to the first child
Browse files Browse the repository at this point in the history
and so use data-testid for example
  • Loading branch information
JF-Cozy committed May 23, 2023
1 parent c7414b3 commit 8082cec
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions react/MidEllipsis/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { forwardRef } from 'react'
import cx from 'classnames'
import PropTypes from 'prop-types'

Expand All @@ -14,32 +14,34 @@ import PropTypes from 'prop-types'
* */
const LRM = <>&#8206;</>

const MidEllipsis = props => {
const { text, className, children } = props

if (text && typeof text !== 'string')
throw new Error('The "text" prop of MidEllipsis can only be a string')

if (children && typeof children !== 'string')
throw new Error('The children of MidEllipsis can only be a string')

const str = text || children

const partLength = Math.round(str.length / 2)
const firstPart = str.substr(0, partLength)
const lastPart = str.substr(partLength, str.length)

return (
<div className={cx('u-midellipsis', className)}>
<span>{firstPart}</span>
<span>
{LRM}
{lastPart}
{LRM}
</span>
</div>
)
}
const MidEllipsis = forwardRef(
({ text, className, children, ...props }, ref) => {
if (text && typeof text !== 'string')
throw new Error('The "text" prop of MidEllipsis can only be a string')

if (children && typeof children !== 'string')
throw new Error('The children of MidEllipsis can only be a string')

const str = text || children

const partLength = Math.round(str.length / 2)
const firstPart = str.substr(0, partLength)
const lastPart = str.substr(partLength, str.length)

return (
<div className={cx('u-midellipsis', className)} ref={ref} {...props}>
<span>{firstPart}</span>
<span>
{LRM}
{lastPart}
{LRM}
</span>
</div>
)
}
)

MidEllipsis.displayName = 'MidEllipsis'

MidEllipsis.propTypes = {
text: PropTypes.string,
Expand Down

0 comments on commit 8082cec

Please sign in to comment.