Skip to content

Commit

Permalink
fix(fileuploaderitem): use refs for ellipsis, add fowardRef to Text (
Browse files Browse the repository at this point in the history
…#17104)

* fix: changed from document to useRef

* fix: fixed ref in Text component

* test: updated snapshots

---------

Co-authored-by: Nikhil Tomar <63502271+2nikhiltom@users.noreply.github.com>
  • Loading branch information
guidari and 2nikhiltom committed Aug 21, 2024
1 parent 6eb536d commit 5e4632c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
2 changes: 2 additions & 0 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10020,6 +10020,7 @@ Map {
},
},
"unstable_Text" => Object {
"$$typeof": Symbol(react.forward_ref),
"propTypes": Object {
"as": Object {
"args": Array [
Expand Down Expand Up @@ -10052,6 +10053,7 @@ Map {
"type": "oneOf",
},
},
"render": [Function],
},
"unstable_TextDirection" => Object {
"propTypes": Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function FileUploaderItem({
className,
...other
}: FileUploaderItemProps) {
const textRef = useRef<HTMLParagraphElement>(null);
const [isEllipsisApplied, setIsEllipsisApplied] = useState(false);
const prefix = usePrefix();
const { current: id } = useRef(uuid || uid());
Expand All @@ -108,8 +109,7 @@ function FileUploaderItem({
};

useLayoutEffect(() => {
const element = document.querySelector(`[title="${name}"]`);
isEllipsisActive(element);
isEllipsisActive(textRef.current);
}, [prefix, name]);

return (
Expand All @@ -122,6 +122,7 @@ function FileUploaderItem({
className={`${prefix}--file-filename-tooltip`}>
<button className={`${prefix}--file-filename-button`} type="button">
<Text
ref={textRef}
as="p"
title={name}
className={`${prefix}--file-filename-button`}
Expand All @@ -133,6 +134,7 @@ function FileUploaderItem({
</div>
) : (
<Text
ref={textRef}
as="p"
title={name}
className={`${prefix}--file-filename`}
Expand Down
17 changes: 8 additions & 9 deletions packages/react/src/components/Text/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
*/

import PropTypes from 'prop-types';
import React, { ReactNode, useContext } from 'react';
import React, { ReactNode, useContext, useRef } from 'react';
import { PolymorphicProps } from '../../types/common';
import { TextDir } from './TextDirection';
import { TextDirectionContext } from './TextDirectionContext';
import { useMergeRefs } from '@floating-ui/react';

export interface TextBaseProps {
dir?: TextDir | undefined;
Expand All @@ -19,12 +20,10 @@ export type TextProps<T extends React.ElementType> = PolymorphicProps<
T,
TextBaseProps
>;
function Text<T extends React.ElementType>({
as,
children,
dir = 'auto',
...rest
}: TextProps<T>) {
const Text = React.forwardRef(function Text<T extends React.ElementType>(
{ as, children, dir = 'auto', ...rest }: TextProps<T>,
ref: React.Ref<HTMLElement>
) {
// TODO: Update with context typing once its been converted to TS
const context = useContext<any>(TextDirectionContext);
const textProps: { dir?: TextDir } = {};
Expand Down Expand Up @@ -59,12 +58,12 @@ function Text<T extends React.ElementType>({

return (
<TextDirectionContext.Provider value={value}>
<BaseComponent {...rest} {...textProps}>
<BaseComponent ref={ref} {...rest} {...textProps}>
{children}
</BaseComponent>
</TextDirectionContext.Provider>
);
}
});

Text.propTypes = {
/**
Expand Down

0 comments on commit 5e4632c

Please sign in to comment.