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

fix: Enhance URL handling in table by rendering URL column types with <a> tag. #37179

Merged
merged 15 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
61230b2
refactor: replace string type with ColumnTypes for columnType prop in…
rahulbarwal Nov 1, 2024
dd2a392
refactor: simplify AutoToolTipComponent by removing LinkWrapper and e…
rahulbarwal Nov 1, 2024
5caa9ba
feat: enhance BasicCell to render URLs as links using ColumnTypes
rahulbarwal Nov 1, 2024
a82baf3
refactor: optimize BasicCell to use useMemo for content rendering
rahulbarwal Nov 1, 2024
ee45872
refactor: replace string type with CompactModeTypes in BaseCellCompon…
rahulbarwal Nov 1, 2024
054ab9c
test: add unit tests for BasicCell component functionality
rahulbarwal Nov 1, 2024
be453a1
feat: enhance BasicCell to improve URL link security with rel and tar…
rahulbarwal Nov 1, 2024
2ffbe75
[Bug]: Unable to right click on the link present in URL column type o…
rahulbarwal Nov 1, 2024
83d6004
test: update AutoTooltipComponent test to handle non-empty tooltip title
rahulbarwal Nov 1, 2024
a267986
fix: prevent pointer events on URL links in BasicCell to improve user…
rahulbarwal Nov 4, 2024
308a73d
Merge branch 'release' of https://github.com/appsmithorg/appsmith int…
rahulbarwal Nov 4, 2024
58b7dd1
fix: simplify URL handling in AutoToolTipComponent to enhance clarity…
rahulbarwal Nov 5, 2024
f8ec7e0
Merge branch 'release' of https://github.com/appsmithorg/appsmith int…
rahulbarwal Nov 5, 2024
b47908e
test: remove redundant URL click test from AutoTooltipComponent tests…
rahulbarwal Nov 5, 2024
b7c5d17
test: update TableV2 URL column spec to check href and target attribu…
rahulbarwal Nov 5, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ export enum IMAGE_VERTICAL_ALIGN {
}

export interface BaseCellComponentProps {
compactMode: string;
compactMode: CompactModeTypes;
isHidden: boolean;
allowCellWrapping?: boolean;
horizontalAlignment?: CellAlignment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ interface Props {
children: React.ReactNode;
title: string;
tableWidth?: number;
columnType?: string;
columnType?: ColumnTypes;
className?: string;
compactMode?: string;
allowCellWrapping?: boolean;
Expand All @@ -152,51 +152,35 @@ interface Props {
isCellDisabled?: boolean;
}

function LinkWrapper(props: Props) {
const content = useToolTip(props.children, props.title);

return (
<CellWrapper
allowCellWrapping={props.allowCellWrapping}
cellBackground={props.cellBackground}
className="cell-wrapper"
compactMode={props.compactMode}
fontStyle={props.fontStyle}
horizontalAlignment={props.horizontalAlignment}
isCellDisabled={props.isCellDisabled}
isCellVisible={props.isCellVisible}
isHidden={props.isHidden}
isHyperLink
isTextType
onClick={(e: React.MouseEvent<HTMLDivElement>) => {
e.stopPropagation();
window.open(props.url, "_blank");
}}
textColor={props.textColor}
textSize={props.textSize}
verticalAlignment={props.verticalAlignment}
>
<div className="link-text">{content}</div>
<OpenNewTabIconWrapper className="hidden-icon">
<OpenNewTabIcon />
</OpenNewTabIconWrapper>
</CellWrapper>
);
}

export function AutoToolTipComponent(props: Props) {
const content = useToolTip(
props.children,
props.title,
props.columnType === ColumnTypes.BUTTON,
);

if (props.columnType === ColumnTypes.URL && props.title) {
return <LinkWrapper {...props} />;
}
const isHyperLink = props.columnType === ColumnTypes.URL;
let contentToRender;

switch (props.columnType) {
case ColumnTypes.BUTTON:
if (props.title) {
return content;
}

if (props.columnType === ColumnTypes.BUTTON && props.title) {
return content;
break;
case ColumnTypes.URL:
contentToRender = (
<>
<div className="link-text">{content}</div>
<OpenNewTabIconWrapper className="hidden-icon">
<OpenNewTabIcon />
</OpenNewTabIconWrapper>
</>
);
break;
default:
contentToRender = content;
}

return (
Expand All @@ -212,12 +196,21 @@ export function AutoToolTipComponent(props: Props) {
isCellDisabled={props.isCellDisabled}
isCellVisible={props.isCellVisible}
isHidden={props.isHidden}
isHyperLink={isHyperLink}
isTextType
onClick={
isHyperLink
? (e: React.MouseEvent<HTMLDivElement>) => {
e.stopPropagation();
window.open(props.url, "_blank");
}
: undefined
}
textColor={props.textColor}
textSize={props.textSize}
verticalAlignment={props.verticalAlignment}
>
{content}
{contentToRender}
</CellWrapper>
</ColumnWrapper>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from "react";
import { render, screen, fireEvent } from "@testing-library/react";
import "@testing-library/jest-dom";
import { BasicCell, type PropType } from "./BasicCell";
import { ColumnTypes } from "widgets/TableWidgetV2/constants";
import { CompactModeTypes } from "widgets/TableWidget/component/Constants";

describe("BasicCell Component", () => {
const defaultProps: PropType = {
value: "Test Value",
onEdit: jest.fn(),
isCellEditable: false,
hasUnsavedChanges: false,
columnType: ColumnTypes.TEXT,
url: "",
compactMode: CompactModeTypes.DEFAULT,
isHidden: false,
isCellVisible: true,
accentColor: "",
tableWidth: 100,
disabledEditIcon: false,
disabledEditIconMessage: "",
};

it("renders the value", () => {
render(<BasicCell {...defaultProps} />);
expect(screen.getByText("Test Value")).toBeInTheDocument();
});

it("renders a link when columnType is URL", () => {
render(
<BasicCell
{...defaultProps}
columnType={ColumnTypes.URL}
url="http://example.com"
/>,
);
const link = screen.getByText("Test Value");

expect(link).toBeInTheDocument();
expect(link).toHaveAttribute("href", "http://example.com");
});

it("calls onEdit when double-clicked", () => {
render(<BasicCell {...defaultProps} isCellEditable />);
fireEvent.doubleClick(screen.getByText("Test Value"));
expect(defaultProps.onEdit).toHaveBeenCalled();
});

it("forwards ref to the div element", () => {
const ref = React.createRef<HTMLDivElement>();

render(<BasicCell {...defaultProps} ref={ref} />);
expect(ref.current).toBeInstanceOf(HTMLDivElement);
});
});
rahulbarwal marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { Ref } from "react";
import React, { useCallback } from "react";
import React, { useCallback, useMemo } from "react";
import { Tooltip } from "@blueprintjs/core";
import styled from "styled-components";
import type { BaseCellComponentProps } from "../Constants";
import type { BaseCellComponentProps, CompactModeTypes } from "../Constants";
import { TABLE_SIZES } from "../Constants";
import { TooltipContentWrapper } from "../TableStyledWrappers";
import AutoToolTipComponent from "./AutoToolTipComponent";
import { importSvg } from "@appsmith/ads-old";
import { ColumnTypes } from "widgets/TableWidgetV2/constants";

const EditIcon = importSvg(
async () => import("assets/icons/control/edit-variant1.svg"),
Expand Down Expand Up @@ -55,7 +56,7 @@ const Content = styled.div`
const StyledEditIcon = styled.div<{
accentColor?: string;
backgroundColor?: string;
compactMode: string;
compactMode: CompactModeTypes;
disabledEditIcon: boolean;
}>`
position: absolute;
Expand All @@ -74,12 +75,12 @@ const StyledEditIcon = styled.div<{
}
`;

type PropType = BaseCellComponentProps & {
export type PropType = BaseCellComponentProps & {
accentColor: string;
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: any;
columnType: string;
columnType: ColumnTypes;
tableWidth: number;
isCellEditable?: boolean;
isCellEditMode?: boolean;
Expand Down Expand Up @@ -128,6 +129,14 @@ export const BasicCell = React.forwardRef(
},
[onEdit, disabledEditIcon, isCellEditable],
);
const contentToRender = useMemo(() => {
switch (columnType) {
case ColumnTypes.URL:
return <a href={url}>{value}</a>;
default:
return value;
}
}, [columnType, url, value]);
rahulbarwal marked this conversation as resolved.
Show resolved Hide resolved

return (
<Wrapper
Expand Down Expand Up @@ -157,7 +166,7 @@ export const BasicCell = React.forwardRef(
url={url}
verticalAlignment={verticalAlignment}
>
<Content ref={contentRef}>{value}</Content>
<Content ref={contentRef}>{contentToRender}</Content>
</StyledAutoToolTipComponent>
{isCellEditable && (
<StyledEditIcon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type RenderDefaultPropsType = BaseCellComponentProps & {
// TODO: Fix this the next time the file is edited
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: any;
columnType: string;
columnType: ColumnTypes;
tableWidth: number;
isCellEditable: boolean;
isCellEditMode?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "../Constants";
import { CellWrapper } from "../TableStyledWrappers";
import { BasicCell } from "./BasicCell";
import type { ColumnTypes } from "widgets/TableWidget/component/Constants";

const StyledSelectComponent = styled(SelectComponent)<{
accentColor: string;
Expand Down Expand Up @@ -61,7 +62,7 @@ type SelectProps = BaseCellComponentProps & {
alias: string;
accentColor: string;
autoOpen: boolean;
columnType: string;
columnType: ColumnTypes;
borderRadius: string;
options?: DropdownOption[];
onFilterChange: (
Expand Down
Loading