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

Mat 7681 oid component #716

Merged
merged 2 commits into from
Dec 30, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as React from "react";
import { render, screen } from "@testing-library/react";
import OidComponent from "./OidComponent";
import userEvent from "@testing-library/user-event";

describe("OidComponent", () => {
it("Should render OidComponent", async () => {
const handleChange = jest.fn();

render(
<OidComponent
value={null}
label="OID"
canEdit={true}
fieldRequired={false}
onChange={handleChange}
structureDefinition={null}
/>
);

expect(screen.getByText("OID")).toBeInTheDocument();
expect(screen.getByTestId("field-input-OID")).toBeInTheDocument();
});

it("Should validate input", async () => {
const handleChange = jest.fn();

render(
<OidComponent
value={null}
label="OID"
canEdit={true}
fieldRequired={false}
onChange={handleChange}
structureDefinition={null}
/>
);

const oidInput = screen.getByTestId("field-input-OID") as HTMLInputElement;

// valid oidInput
userEvent.type(oidInput, "urn:oid:1.2.3.4.5");
expect(oidInput.value).toBe("urn:oid:1.2.3.4.5");
expect(
screen.getByTestId("field-input-helper-text-OID")
).not.toHaveTextContent("Please enter a valid OID");

// invalid oidInput
userEvent.clear(oidInput);
userEvent.type(oidInput, "invalid OID.");
expect(oidInput).toHaveValue("invalid OID.");
expect(screen.getByTestId("field-input-helper-text-OID")).toHaveTextContent(
"Please enter a valid OID"
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React, { useState } from "react";
import { FormHelperText } from "@mui/material";
import { TypeComponentProps } from "./TypeComponentProps";
import { TextField } from "@madie/madie-design-system/dist/react";
import _ from "lodash";

const OidComponent = ({
canEdit,
fieldRequired,
label = "OID",
value,
onChange,
structureDefinition,
}: TypeComponentProps) => {
const oidRegex = /urn:oid:[0-2](\.(0|[1-9][0-9]*))+/;
const [isValid, setValid] = useState<boolean>(
value ? value.match(oidRegex) : true
);

const handleChange = (oid) => {
setValid(true);
if (oid.match(oidRegex)) {
onChange(oid);
} else if (!_.isEmpty(oid)) {
setValid(false);
}
};
return (
<TextField
label={`${label}`}
required={fieldRequired}
disabled={!canEdit}
inputProps={{
"data-testid": `field-input-${label}`,
"aria-describedby": `field-input-helper-text-${label}`,
required: fieldRequired,
"aria-required": fieldRequired,
}}
size="small"
fullWidth
value={value}
onChange={(e) => handleChange(e.target.value)}
helperText={
<FormHelperText
data-testid={`field-input-helper-text-${label}`}
error={!isValid}
>
{isValid ? "" : "Please enter a valid OID"}
</FormHelperText>
}
/>
);
};

export default OidComponent;
Loading