-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
[TS migration] Migrate 'DisplayNames' component to TypeScript #30843
Changes from 1 commit
887e028
3f22243
d9bbc47
2febc6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import lodashGet from 'lodash/get'; | ||
import React, {Fragment, useCallback, useRef} from 'react'; | ||
import {View} from 'react-native'; | ||
import _ from 'underscore'; | ||
import {Text as RNText, View} from 'react-native'; | ||
import Text from '@components/Text'; | ||
import Tooltip from '@components/Tooltip'; | ||
import styles from '@styles/styles'; | ||
import {defaultProps, propTypes} from './displayNamesPropTypes'; | ||
import DisplayNamesTooltipItem from './DisplayNamesTooltipItem'; | ||
import DisplayNamesProps from './types'; | ||
|
||
function DisplayNamesWithToolTip(props) { | ||
const containerRef = useRef(null); | ||
const childRefs = useRef([]); | ||
const isEllipsisActive = lodashGet(containerRef.current, 'offsetWidth') < lodashGet(containerRef.current, 'scrollWidth'); | ||
type HTMLElementWithText = HTMLElement & RNText; | ||
|
||
function DisplayNamesWithToolTip({shouldUseFullTitle, fullTitle, displayNamesWithTooltips, textStyles = [], numberOfLines = 1}: DisplayNamesProps) { | ||
const containerRef = useRef<HTMLElementWithText>(null); | ||
const childRefs = useRef<HTMLElementWithText[]>([]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just wondering if this will be always a html element ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kubabutkiewicz If I type it with just Text it gives errors, because Text ref doesn't have |
||
const isEllipsisActive = !!containerRef.current?.offsetWidth && !!containerRef.current?.scrollWidth && containerRef.current.offsetWidth < containerRef.current.scrollWidth; | ||
|
||
/** | ||
* We may need to shift the Tooltip horizontally as some of the inline text wraps well with ellipsis, | ||
|
@@ -25,9 +25,9 @@ function DisplayNamesWithToolTip(props) { | |
* @param {Number} index Used to get the Ref to the node at the current index | ||
* @returns {Number} Distance to shift the tooltip horizontally | ||
*/ | ||
const getTooltipShiftX = useCallback((index) => { | ||
const getTooltipShiftX = useCallback((index: number) => { | ||
// Only shift the tooltip in case the containerLayout or Refs to the text node are available | ||
if (!containerRef || !childRefs.current[index]) { | ||
if (!containerRef.current || !childRefs.current[index]) { | ||
return; | ||
} | ||
const {width: containerWidth, left: containerLeft} = containerRef.current.getBoundingClientRect(); | ||
|
@@ -46,13 +46,14 @@ function DisplayNamesWithToolTip(props) { | |
return ( | ||
// Tokenization of string only support prop numberOfLines on Web | ||
<Text | ||
style={[...props.textStyles, styles.pRelative, props.numberOfLines === 1 ? styles.noWrap : {}]} | ||
numberOfLines={props.numberOfLines || undefined} | ||
ref={(el) => (containerRef.current = el)} | ||
style={[textStyles, styles.pRelative, numberOfLines === 1 ? styles.noWrap : {}]} | ||
numberOfLines={numberOfLines || undefined} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just wondering why the spread syntax was removed here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jjcoffee This way |
||
ref={containerRef} | ||
> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this logic change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kubabutkiewicz There are two reasons:
|
||
{props.shouldUseFullTitle | ||
? props.fullTitle | ||
: _.map(props.displayNamesWithTooltips, ({displayName, accountID, avatar, login}, index) => ( | ||
{shouldUseFullTitle | ||
? fullTitle | ||
: displayNamesWithTooltips.map(({displayName, accountID, avatar, login}, index) => ( | ||
// eslint-disable-next-line react/no-array-index-key | ||
<Fragment key={index}> | ||
<DisplayNamesTooltipItem | ||
index={index} | ||
|
@@ -61,16 +62,15 @@ function DisplayNamesWithToolTip(props) { | |
displayName={displayName} | ||
login={login} | ||
avatar={avatar} | ||
textStyles={props.textStyles} | ||
textStyles={textStyles} | ||
childRefs={childRefs} | ||
addComma={index < props.displayNamesWithTooltips.length - 1} | ||
/> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why removing that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kubabutkiewicz I haven't found it's being used in the component it's passed to |
||
{index < props.displayNamesWithTooltips.length - 1 && <Text style={props.textStyles}>, </Text>} | ||
{index < displayNamesWithTooltips.length - 1 && <Text style={textStyles}>, </Text>} | ||
</Fragment> | ||
))} | ||
{Boolean(isEllipsisActive) && ( | ||
<View style={styles.displayNameTooltipEllipsis}> | ||
<Tooltip text={props.fullTitle}> | ||
<Tooltip text={fullTitle}> | ||
{/* There is some Gap for real ellipsis so we are adding 4 `.` to cover */} | ||
<Text>....</Text> | ||
</Tooltip> | ||
|
@@ -80,8 +80,6 @@ function DisplayNamesWithToolTip(props) { | |
); | ||
} | ||
|
||
DisplayNamesWithToolTip.propTypes = propTypes; | ||
DisplayNamesWithToolTip.defaultProps = defaultProps; | ||
DisplayNamesWithToolTip.displayName = 'DisplayNamesWithTooltip'; | ||
|
||
export default DisplayNamesWithToolTip; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react'; | ||
import {StyleProp, TextStyle} from 'react-native'; | ||
import Text from '@components/Text'; | ||
import styles from '@styles/styles'; | ||
|
||
type DisplayNamesWithoutTooltipProps = { | ||
/** The full title of the DisplayNames component (not split up) */ | ||
fullTitle?: string; | ||
|
||
/** Arbitrary styles of the displayName text */ | ||
textStyles?: StyleProp<TextStyle>; | ||
|
||
/** Number of lines before wrapping */ | ||
numberOfLines?: number; | ||
}; | ||
|
||
function DisplayNamesWithoutTooltip({textStyles = [], numberOfLines = 1, fullTitle = ''}: DisplayNamesWithoutTooltipProps) { | ||
return ( | ||
<Text | ||
style={[textStyles, numberOfLines === 1 ? styles.pre : styles.preWrap]} | ||
numberOfLines={numberOfLines} | ||
> | ||
{fullTitle} | ||
</Text> | ||
); | ||
} | ||
|
||
DisplayNamesWithoutTooltip.displayName = 'DisplayNamesWithoutTooltip'; | ||
|
||
export default DisplayNamesWithoutTooltip; |
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from 'react'; | ||
import Text from '@components/Text'; | ||
import DisplayNamesProps from './types'; | ||
|
||
// As we don't have to show tooltips of the Native platform so we simply render the full display names list. | ||
function DisplayNames({accessibilityLabel, fullTitle, textStyles = [], numberOfLines = 1}: DisplayNamesProps) { | ||
return ( | ||
<Text | ||
accessibilityLabel={accessibilityLabel} | ||
style={textStyles} | ||
numberOfLines={numberOfLines} | ||
> | ||
{fullTitle} | ||
VickyStash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</Text> | ||
); | ||
} | ||
|
||
DisplayNames.displayName = 'DisplayNames'; | ||
|
||
export default DisplayNames; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NAB: This comment is inaccurate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to merge now. If you would please do this in a follow up that would be great.