Skip to content

Commit

Permalink
feat: redact hide (#115) (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
simboonlong authored Sep 19, 2023
1 parent 9ea9211 commit b1e6542
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
10 changes: 9 additions & 1 deletion src/components/common/RedactableValue.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ export default {
};

export const RedactableValueDefault = (): ReactElement => {
return <RedactableValue value="Some value" />;
return (
<RedactableValue
value="Some value"
editable={false}
onRedactionRequested={() => {
console.log("Please redact");
}}
/>
);
};

export const RedactableValueIconDefault = (): ReactElement => {
Expand Down
14 changes: 6 additions & 8 deletions src/components/common/RedactableValue.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ import { DEFAULT_NO_VALUE_MSG, DEFAULT_REDACTED_MSG, RedactableValue } from "./R
import { screen, render, fireEvent } from "@testing-library/react";

describe("redactablevalue component", () => {
it("should display value only when editable is not set", () => {
const callback = jest.fn();
render(<RedactableValue value="Text to display" onRedactionRequested={callback} />);

expect(screen.getByText("Text to display")).toBeInTheDocument();
expect(screen.queryByTitle("Redact handler")).not.toBeInTheDocument();
});

it("should display value only when editable is set to false", () => {
const callback = jest.fn();
render(<RedactableValue value="Text to display" onRedactionRequested={callback} editable={false} />);
Expand Down Expand Up @@ -93,4 +85,10 @@ describe("redactablevalue component", () => {

expect(screen.getByText(CUSTOM_NO_VALUE_MSG)).toBeInTheDocument();
});

it("should hide value when isValueHidden", () => {
const callback = jest.fn();
render(<RedactableValue value={`foo`} isValueHidden onRedactionRequested={callback} editable />);
expect(screen.queryByTestId("redactable-value")).not.toBeInTheDocument();
});
});
41 changes: 23 additions & 18 deletions src/components/common/RedactableValue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ const IconRedact: FunctionComponent = () => {
};

export interface RedactValueProps {
value?: string | number;
onRedactionRequested?: () => void;
editable?: boolean;
value?: string | number | Record<string, unknown> | Record<string, unknown>[];
isValueHidden?: boolean;
onRedactionRequested: () => void;
editable: boolean;
iconRedact?: React.ReactElement;
redactedMessage?: string;
noValueMessage?: string;
className?: string;
}

export const DEFAULT_REDACTED_MSG = "**Redacted**";
Expand All @@ -36,38 +38,41 @@ export const DEFAULT_NO_VALUE_MSG = "**Field value does not exist**";
/**
* RedactableValue component is almost a duplicate of ObfuscatableValue component
* ObfuscatableValue component started from OpenCerts, and may be in use on existing certificates, hence we do not want to meddle with the existing functionality
* RedactableValue component displays a value and a cross when editable props is set to true, allows custom redact icon, hints at redacted values
*/
export const RedactableValue: FunctionComponent<RedactValueProps> = ({
value,
isValueHidden = false,
onRedactionRequested = noop,
editable = false,
iconRedact = <IconRedact />,
redactedMessage,
noValueMessage,
className = "",
}) => {
const [isRedacted, setRedacted] = useState(false);

if (isRedacted)
return (
<span style={{ display: "inline-block", color: "#454B50" }}>
{redactedMessage ? redactedMessage : DEFAULT_REDACTED_MSG}
</span>
);
if (!value)
return (
<span style={{ display: "inline-block", color: "#454B50" }}>
{noValueMessage ? noValueMessage : DEFAULT_NO_VALUE_MSG}
</span>
);
const getMessage = () => {
switch (true) {
case isRedacted:
return redactedMessage ? redactedMessage : DEFAULT_REDACTED_MSG;
case !value:
return noValueMessage ? noValueMessage : DEFAULT_NO_VALUE_MSG;
default:
return value;
}
};

return (
<>
<span style={{ display: "inline-block", marginRight: "8px" }}>{value}</span>
{!isValueHidden && (
<span className={`inline-block text-[#454B50]`} data-testid="redactable-value">
{getMessage()}
</span>
)}
{editable && (
<span
title="Redact handler"
style={{ display: "inline-block", cursor: "pointer" }}
className={`inline-block cursor-pointer ${className}`}
onClick={() => {
if (editable) {
onRedactionRequested();
Expand Down

0 comments on commit b1e6542

Please sign in to comment.