-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UI v2] feat: Adds cron input component
- Loading branch information
1 parent
3c5112d
commit b39afff
Showing
6 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { fn } from "@storybook/test"; | ||
import { useState } from "react"; | ||
import { CronInput } from "./cron-input"; | ||
|
||
const meta: Meta<typeof CronInput> = { | ||
title: "UI/CronInput", | ||
render: () => <CronInputStory />, | ||
}; | ||
|
||
export default meta; | ||
|
||
export const story: StoryObj = { name: "CronInput" }; | ||
|
||
const CronInputStory = () => { | ||
const [input, setInput] = useState("* * * * *"); | ||
|
||
return ( | ||
<CronInput | ||
value={input} | ||
onChange={(e) => setInput(e.target.value)} | ||
getIsCronValid={fn} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { render, screen } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import { useState } from "react"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
import { CronInput, type CronInputProps } from "./cron-input"; | ||
|
||
describe("CronInput", () => { | ||
const TestCronInput = ({ getIsCronValid }: CronInputProps) => { | ||
const [value, setValue] = useState(""); | ||
return ( | ||
<CronInput | ||
value={value} | ||
onChange={(e) => setValue(e.target.value)} | ||
getIsCronValid={getIsCronValid} | ||
/> | ||
); | ||
}; | ||
|
||
it("renders a valid cron message", async () => { | ||
// SETUP | ||
const user = userEvent.setup(); | ||
const mockGetIsCronValid = vi.fn(); | ||
render(<TestCronInput getIsCronValid={mockGetIsCronValid} />); | ||
|
||
// TEST | ||
await user.type(screen.getByRole("textbox"), "* * * * *"); | ||
|
||
// ASSERT | ||
expect(screen.getByText("Every minute")).toBeVisible(); | ||
expect(mockGetIsCronValid).toHaveBeenLastCalledWith(true); | ||
}); | ||
|
||
it("renders an valid cron message", async () => { | ||
// SETUP | ||
const user = userEvent.setup(); | ||
const mockGetIsCronValid = vi.fn(); | ||
render(<TestCronInput getIsCronValid={mockGetIsCronValid} />); | ||
|
||
// TEST | ||
await user.type(screen.getByRole("textbox"), "abcd"); | ||
|
||
// ASSERT | ||
expect(screen.getByText("Invalid expression")).toBeVisible(); | ||
expect(mockGetIsCronValid).toHaveBeenLastCalledWith(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Input, type InputProps } from "@/components/ui/input"; | ||
import { Typography } from "@/components/ui/typography"; | ||
import clsx from "clsx"; | ||
import cronParser from "cron-parser"; | ||
import cronstrue from "cronstrue"; | ||
import { useState } from "react"; | ||
|
||
const verifyCronValue = (cronValue: string) => { | ||
let description = ""; | ||
let isCronValid = false; | ||
try { | ||
cronParser.parseExpression(cronValue); | ||
description = cronstrue.toString(cronValue); | ||
isCronValid = true; | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
} catch (e) { | ||
isCronValid = false; | ||
description = "Invalid expression"; | ||
} | ||
return { | ||
description, | ||
isCronValid, | ||
}; | ||
}; | ||
|
||
export type CronInputProps = InputProps & { | ||
/** Used to indicate the container if Cron is valid */ | ||
getIsCronValid?: (isValid: boolean) => void; | ||
}; | ||
|
||
export const CronInput = ({ | ||
getIsCronValid = () => true, | ||
onChange, | ||
...props | ||
}: CronInputProps) => { | ||
const [description, setDescription] = useState( | ||
verifyCronValue(String(props.value)).description, | ||
); | ||
const [isCronValid, setIsCronValid] = useState( | ||
verifyCronValue(String(props.value)).isCronValid, | ||
); | ||
|
||
const handleChange: React.ChangeEventHandler<HTMLInputElement> = (event) => { | ||
if (onChange) { | ||
onChange(event); | ||
const { description, isCronValid } = verifyCronValue(event.target.value); | ||
setDescription(description); | ||
setIsCronValid(isCronValid); | ||
getIsCronValid(isCronValid); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="flex flex-col gap-1"> | ||
<Input {...props} onChange={handleChange} /> | ||
<Typography | ||
variant="bodySmall" | ||
className={clsx(isCronValid ? "text-muted-foreground" : "text-red-500")} | ||
> | ||
{description} | ||
</Typography> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { CronInput } from "./cron-input"; |