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

feat(text): refの追加 #1420

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
156 changes: 90 additions & 66 deletions packages/wiz-ui-react/src/components/base/text/components/text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import {
whiteSpaceStyle,
} from "@wizleap-inc/wiz-ui-styles/commons";
import clsx from "clsx";
import { CSSProperties, ReactNode, memo } from "react";
import {
CSSProperties,
ReactNode,
forwardRef,
memo,
ForwardedRef,
} from "react";

import { BaseProps } from "@/types";
type Props = BaseProps & {
Expand All @@ -36,73 +42,91 @@ type Props = BaseProps & {
children?: ReactNode;
};

const _Text = ({
className,
style,
as = "p",
htmlFor,
color = "gray.900",
fontSize = "md",
lineHeight,
bold,
maxLines,
whiteSpace = "normal",
isBlurred = false,
breakAll,
textAlign = "start",
lineThrough = false,
display,
children,
}: Props) => {
const textStyle: CSSProperties = {
...style,
...(maxLines && {
overflow: "hidden",
display: "-webkit-box",
WebkitBoxOrient: "vertical",
WebkitLineClamp: maxLines,
}),
};
const textClass = clsx([
className,
styles.textStyle,
styles.textFontWeightStyle[bold ? "bold" : "default"],
styles.textAlignStyle[textAlign],
(maxLines || breakAll) && styles.textWordBreakStyle,
lineHeight
? lineHeightStyle[lineHeight]
: styles.textDefaultLineHeightStyle,
fontSizeStyle[fontSize],
colorStyle[color],
whiteSpaceStyle[whiteSpace],
lineThrough && styles.textLineThroughStyle,
isBlurred && styles.textDummyStyle,
display && styles.textDisplayStyle[display],
]);
switch (as) {
case "p": {
return (
<p className={textClass} style={textStyle}>
{children}
</p>
);
}
case "label": {
return (
<label htmlFor={htmlFor} className={textClass} style={textStyle}>
{children}
</label>
);
}
case "span": {
return (
<span className={textClass} style={textStyle}>
{children}
</span>
);
const _Text = forwardRef(
(
{
className,
style,
as = "p",
htmlFor,
color = "gray.900",
fontSize = "md",
lineHeight,
bold,
maxLines,
whiteSpace = "normal",
isBlurred = false,
breakAll,
textAlign = "start",
lineThrough = false,
display,
children,
}: Props,
ref: ForwardedRef<HTMLParagraphElement | HTMLSpanElement | HTMLLabelElement>
) => {
const textStyle: CSSProperties = {
...style,
...(maxLines && {
overflow: "hidden",
display: "-webkit-box",
WebkitBoxOrient: "vertical",
WebkitLineClamp: maxLines,
}),
};
const textClass = clsx([
className,
styles.textStyle,
styles.textFontWeightStyle[bold ? "bold" : "default"],
styles.textAlignStyle[textAlign],
(maxLines || breakAll) && styles.textWordBreakStyle,
lineHeight
? lineHeightStyle[lineHeight]
: styles.textDefaultLineHeightStyle,
fontSizeStyle[fontSize],
colorStyle[color],
whiteSpaceStyle[whiteSpace],
lineThrough && styles.textLineThroughStyle,
isBlurred && styles.textDummyStyle,
display && styles.textDisplayStyle[display],
]);
switch (as) {
case "p": {
return (
<p
className={textClass}
style={textStyle}
ref={ref as ForwardedRef<HTMLParagraphElement>}
>
{children}
</p>
);
}
case "label": {
return (
<label
htmlFor={htmlFor}
className={textClass}
style={textStyle}
ref={ref as ForwardedRef<HTMLLabelElement>}
>
{children}
</label>
);
}
case "span": {
return (
<span
className={textClass}
style={textStyle}
ref={ref as ForwardedRef<HTMLSpanElement>}
>
{children}
</span>
);
}
}
}
};
);

_Text.displayName = ComponentName.Text;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
COLOR_MAP_ACCESSORS,
WHITE_SPACE_MAP_ACCESSORS,
} from "@wizleap-inc/wiz-ui-constants";
import { useEffect, useRef } from "react";

import { WizText } from "../components/text";

Expand Down Expand Up @@ -170,3 +171,72 @@ export const MaxLines: Story = {
maxLines: 5,
},
};

export const ParagraphWithRef: Story = {
render: (args) => {
const text = "これはテキストです。\nこれはテキストです。";
const textRef = useRef<HTMLParagraphElement>(null);
useEffect(() => {
if (textRef.current) {
console.log({
offsetWidth: textRef.current?.offsetWidth,
scrollWidth: textRef.current?.scrollWidth,
});
}
}, [text]);
return (
<WizText ref={textRef} {...args}>
{text}
</WizText>
);
},
args: {
as: "p",
},
};

export const SpanWithRef: Story = {
render: (args) => {
const text = "これはテキストです。\nこれはテキストです。";
const textRef = useRef<HTMLSpanElement>(null);
useEffect(() => {
if (textRef.current) {
console.log({
offsetWidth: textRef.current?.offsetWidth,
scrollWidth: textRef.current?.scrollWidth,
});
}
}, [text]);
return (
<WizText ref={textRef} {...args}>
{text}
</WizText>
);
},
args: {
as: "span",
},
};

export const LabelWithRef: Story = {
render: (args) => {
const text = "これはテキストです。\nこれはテキストです。";
const textRef = useRef<HTMLLabelElement>(null);
useEffect(() => {
if (textRef.current) {
console.log({
offsetWidth: textRef.current?.offsetWidth,
scrollWidth: textRef.current?.scrollWidth,
});
}
}, [text]);
return (
<WizText ref={textRef} {...args}>
{text}
</WizText>
);
},
args: {
as: "label",
},
};
Loading