This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract out validation logic into custom component
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
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
16 changes: 16 additions & 0 deletions
16
src/components/TextInputWithValidation/TextInputWithValidation.stories.tsx
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,16 @@ | ||
import { CustomMeta, CustomStoryObj } from "test_utils/types"; | ||
import TextInputWithValidation from "."; | ||
|
||
export default { | ||
title: "Components/TextInput/TextInputWithValidation", | ||
component: TextInputWithValidation, | ||
} satisfies CustomMeta<typeof TextInputWithValidation>; | ||
|
||
export const Default: CustomStoryObj<typeof TextInputWithValidation> = { | ||
render: (args) => <TextInputWithValidation {...args} />, | ||
argTypes: {}, | ||
args: { | ||
validator: (v) => v !== "bad", | ||
label: "Some search field", | ||
}, | ||
}; |
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,72 @@ | ||
import { useState, forwardRef } from "react"; | ||
import IconButton from "@leafygreen-ui/icon-button"; | ||
import { palette } from "@leafygreen-ui/palette"; | ||
import Icon from "components/Icon"; | ||
import IconTooltip from "components/IconTooltip"; | ||
import TextInputWithGlyph from "components/TextInputWithGlyph"; | ||
import type { TextInputWithGlyphProps } from "components/TextInputWithGlyph"; | ||
|
||
const { yellow } = palette; | ||
type TextInputWithValidationProps = { | ||
onSubmit?: (value: string) => void; | ||
validator?: (value: string) => boolean; | ||
validatorErrorMessage?: string; | ||
label?: React.ReactNode; | ||
placeholder?: string; | ||
} & TextInputWithGlyphProps; | ||
|
||
const TextInputWithValidation: React.FC<TextInputWithValidationProps> = | ||
forwardRef((props, ref) => { | ||
const { | ||
onSubmit = () => {}, | ||
placeholder = "", | ||
validator = () => true, | ||
validatorErrorMessage = "", | ||
...rest | ||
} = props; | ||
|
||
const [input, setInput] = useState(""); | ||
const isValid = validator(input); | ||
|
||
const handleOnSubmit = () => { | ||
if (isValid) { | ||
onSubmit(input); | ||
setInput(""); | ||
} | ||
}; | ||
|
||
const handleOnChange = (value: string) => { | ||
setInput(value); | ||
}; | ||
|
||
return ( | ||
<TextInputWithGlyph | ||
value={input} | ||
type="search" | ||
onChange={(e) => handleOnChange(e.target.value)} | ||
placeholder={placeholder} | ||
onKeyPress={(e: React.KeyboardEvent<HTMLInputElement>) => | ||
e.key === "Enter" && handleOnSubmit() | ||
} | ||
ref={ref} | ||
icon={ | ||
isValid ? ( | ||
<IconButton | ||
onClick={handleOnSubmit} | ||
aria-label="Select plus button" | ||
> | ||
<Icon glyph="Plus" /> | ||
</IconButton> | ||
) : ( | ||
<IconTooltip glyph="Warning" fill={yellow.base}> | ||
{validatorErrorMessage} | ||
</IconTooltip> | ||
) | ||
} | ||
{...rest} | ||
/> | ||
); | ||
}); | ||
|
||
TextInputWithValidation.displayName = "TextInputWithValidation"; | ||
export default TextInputWithValidation; |