Skip to content

Commit

Permalink
add input field (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
mazmassa authored May 5, 2023
1 parent 5f257fb commit 14c2196
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/components/Input/Input.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Input } from "./Input";

export default { title: "Components/Input", component: Input };

export const _Input = {
render: () => (
<>
<Input placeholder={"something"} />
<br />

<Input placeholder={"something"} error="that's an error" />
<br />

<Input placeholder={"something"} warning="that's a warning" />
</>
),
};
53 changes: 53 additions & 0 deletions src/components/Input/Input.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import "@testing-library/jest-dom";
import { render, screen, fireEvent } from "@testing-library/react";
import { Input } from ".";

describe("Components | Fields | Input", () => {
test("it should render", () => {
render(<Input />);

let input = screen.getByTestId("input-field");

expect(input).toBeInTheDocument();
});

test("it should set a new placeholder content", () => {
render(<Input placeholder={"something"} />);

let input = screen.getByPlaceholderText("something");

expect(input).toBeInTheDocument();
});

test("it should show an error message", () => {
render(<Input error={"euh"} />);

let message = screen.getByTestId("input-field-message");
let input = screen.getByTestId("input-field");

expect(message).toBeInTheDocument();
expect(message.getAttribute("class")).toContain("text-s-error");
expect(input.getAttribute("class")).toContain("border-s-error");
});

test("it should show an warning message", () => {
render(<Input warning={"warn"} />);

let message = screen.getByTestId("input-field-message");
let input = screen.getByTestId("input-field");

expect(message).toBeInTheDocument();
expect(message.getAttribute("class")).toContain("text-s-warning");
expect(input.getAttribute("class")).toContain("border-s-warning");
});

test("it should change border color on focusIn", () => {
render(<Input />);

let input = screen.getByTestId("input-field");

fireEvent.focus(input);

expect(input.getAttribute("class")).toContain("default-input");
});
});
58 changes: 58 additions & 0 deletions src/components/Input/Input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { useState, ComponentPropsWithoutRef } from "react";

export interface InputProps extends ComponentPropsWithoutRef<"input"> {
placeholder?: string | undefined;
error?: string | undefined;
warning?: string | undefined;
}

export function Input(props: InputProps) {
const { error, warning, ...rest } = props;
const [field, setField] = useState("");

const errorClass = error ? "border-s-error" : "";
const warningClass = warning ? "border-s-warning" : "";
const messageClass = errorClass || warningClass;

function handleOnChange(e) {
setField(e.target.value);
}

return (
<div className="flex-row">
<div className="grid-cols-2">
<div className="inline h-12">
<input
data-testid="input-field"
className={`w-full default-input mb-1 ${messageClass}`}
type="text"
value={field}
onChange={handleOnChange}
{...rest}
/>
<InputMessage error={error} warning={warning} />
</div>
</div>
</div>
);
}

export function InputMessage(props: InputProps) {
const { error, warning } = props;

const errorClass = error ? "mas-h3 text-s-error" : "";
const warningClass = warning ? "mas-h3 text-s-warning" : "";

const messageClass = errorClass || warningClass;
const hasMessage = error || warning;

if (hasMessage) {
return (
<div data-testid="input-field-message" className={messageClass}>
{error || warning}
</div>
);
} else {
return null;
}
}
1 change: 1 addition & 0 deletions src/components/Input/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./Input";

0 comments on commit 14c2196

Please sign in to comment.