Skip to content

Commit

Permalink
[BOOKINGSG-4362][WK] truncated the category title
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilker0725 committed Aug 25, 2023
1 parent e95bc12 commit 4fa9f7b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 24 deletions.
33 changes: 21 additions & 12 deletions src/shared/nested-dropdown-list/list-item.styles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface ListItemSelectorProps {
$multiSelect: boolean;
}

interface LabelProps {
interface TruncateProps {
$truncateType?: TruncateType;
}

Expand Down Expand Up @@ -89,7 +89,7 @@ export const Item = styled.li<ItemProps>`
}}
`;

export const Label = styled.div<LabelProps>`
export const Label = styled.div<TruncateProps>`
${TextStyleHelper.getTextStyle("Body", "regular")}
overflow: hidden;
Expand Down Expand Up @@ -182,7 +182,7 @@ export const TriangleIcon = styled(TriangleForwardFillIcon)`
color: ${Color.Primary};
`;

export const Title = styled.button`
export const TitleButton = styled.button`
${TextStyleHelper.getTextStyle("H4", "semibold")}
color: ${Color.Neutral[1]};
text-align: left;
Expand All @@ -193,15 +193,24 @@ export const Title = styled.button`
cursor: pointer;
width: 100%;
padding: 0;
overflow-wrap: anywhere;
span {
overflow: hidden;
display: -webkit-box;
text-overflow: ellipsis;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
overflow: hidden;
`;

export const Title = styled.div<TruncateProps>`
${(props) => {
switch (props.$truncateType) {
case "middle":
break;
case "end":
default:
return css`
display: -webkit-box;
text-overflow: ellipsis;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
`;
}
}}
`;

export const List = styled.ul<ListProps>`
Expand Down
26 changes: 20 additions & 6 deletions src/shared/nested-dropdown-list/list-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ import {
List,
ListItemSelector,
Title,
TitleButton,
TriangleIcon,
TruncateContainer,
TruncateFirstLine,
TruncateSecondLine,
} from "./list-item.styles";

type LabelType = "category" | "label";

interface ListItemProps<V1, V2, V3> {
item: CombinedFormattedOptionProps<V1, V2, V3>;
selectableCategory?: boolean | undefined;
Expand Down Expand Up @@ -49,6 +52,7 @@ export const ListItem = <V1, V2, V3>({
// CONST, REF, STATE
// =============================================================================
const labelRef = useRef<HTMLLIElement>();
const categoryRef = useRef<HTMLDivElement>();

// =============================================================================
// EVENT HANDLERS
Expand Down Expand Up @@ -78,15 +82,20 @@ export const ListItem = <V1, V2, V3>({
// HELPER FUNCTIONS
// =============================================================================
const hasExceededContainer = (
item: CombinedFormattedOptionProps<V1, V2, V3>
item: CombinedFormattedOptionProps<V1, V2, V3>,
type: LabelType
) => {
const displayText = item.label;

let widthOfElement = 0;
if (labelRef && labelRef.current) {
if (type === "label" && labelRef && labelRef.current) {
widthOfElement = labelRef.current.getBoundingClientRect().width;
}

if (type === "category" && categoryRef && categoryRef.current) {
widthOfElement = categoryRef.current.getBoundingClientRect().width;
}

return StringHelper.shouldTruncateToTwoLines(
displayText,
widthOfElement
Expand Down Expand Up @@ -203,9 +212,14 @@ export const ListItem = <V1, V2, V3>({
return (
<Category {...categoryProps}>
{renderCategoryIcon()}
<Title {...titleProps}>
<span>{item.label}</span>
</Title>
<TitleButton {...titleProps}>
<Title ref={categoryRef} $truncateType={itemTruncationType}>
{itemTruncationType === "middle" &&
hasExceededContainer(item, "category")
? renderTruncatedText(item)
: item.label}
</Title>
</TitleButton>
</Category>
);
};
Expand All @@ -222,7 +236,7 @@ export const ListItem = <V1, V2, V3>({
)}
<Label $truncateType={itemTruncationType}>
{itemTruncationType === "middle" &&
hasExceededContainer(item)
hasExceededContainer(item, "label")
? renderTruncatedText(item)
: renderBolded(item)}
</Label>
Expand Down
6 changes: 0 additions & 6 deletions src/shared/nested-dropdown-list/nested-dropdown-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -582,12 +582,6 @@ export const NestedDropdownList = <V1, V2, V3>({
);
};

/**
TODO:
16. renderBottomCta
19. middle truncation
*/

return (
<>
<Container
Expand Down

0 comments on commit 4fa9f7b

Please sign in to comment.