Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Refactor tupleselect to use special textinput with validation
Browse files Browse the repository at this point in the history
  • Loading branch information
khelif96 committed Oct 31, 2023
1 parent c5559ca commit 05b8d24
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 39 deletions.
16 changes: 16 additions & 0 deletions src/components/TextInputWithValidation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,20 @@ import type { TextInputWithGlyphProps } from "components/TextInputWithGlyph";

const { yellow } = palette;
type TextInputWithValidationProps = {
/**
* `onSubmit` will be called when the user submits a new input with the enter key or the plus button
* if the input is valid
* @param value - the value of the input
* @returns
*/
onSubmit?: (value: string) => void;
validator?: (value: string) => boolean;
/**
* `onChange` will be called when the user types into the input and the input is valid
* @param value - the value of the input
* @returns void
*/
onChange?: (value: string) => void;
validatorErrorMessage?: string;
label?: React.ReactNode;
placeholder?: string;
Expand All @@ -18,6 +30,7 @@ type TextInputWithValidationProps = {
const TextInputWithValidation: React.FC<TextInputWithValidationProps> =
forwardRef((props, ref) => {
const {
onChange = () => {},
onSubmit = () => {},
placeholder = "",
validator = () => true,
Expand All @@ -36,6 +49,9 @@ const TextInputWithValidation: React.FC<TextInputWithValidationProps> =
};

const handleOnChange = (value: string) => {
if (isValid) {
onChange(value);
}
setInput(value);
};

Expand Down
45 changes: 6 additions & 39 deletions src/components/TupleSelect/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { useState } from "react";
import styled from "@emotion/styled";
import IconButton from "@leafygreen-ui/icon-button";
import { palette } from "@leafygreen-ui/palette";
import { Select, Option } from "@leafygreen-ui/select";
import { Label } from "@leafygreen-ui/typography";
import Icon from "components/Icon";
import IconTooltip from "components/IconTooltip";
import TextInput from "components/TextInputWithGlyph";
import TextInput from "components/TextInputWithValidation";
import { size } from "constants/tokens";

const { yellow } = palette;
type option = {
value: string;
displayName: string;
Expand All @@ -30,20 +25,12 @@ const TupleSelect: React.FC<TupleSelectProps> = ({
validator = () => true,
validatorErrorMessage = "Invalid Input",
}) => {
const [input, setInput] = useState("");
const [selected, setSelected] = useState(options[0].value);
const isValid = validator(input);

const handleOnSubmit = () => {
if (isValid) {
onSubmit({ category: selected, value: input });
setInput("");
}
const handleOnSubmit = (input: string) => {
onSubmit({ category: selected, value: input });
};

const handleOnChange = (value: string) => {
setInput(value);
};
const selectedOption = options.find((o) => o.value === selected);

return (
Expand Down Expand Up @@ -73,31 +60,11 @@ const TupleSelect: React.FC<TupleSelectProps> = ({
id="filter-input"
aria-label={selectedOption.displayName}
data-cy="tuple-select-input"
value={input}
type="search"
onChange={(e) => handleOnChange(e.target.value)}
placeholder={selectedOption.placeHolderText}
onKeyPress={(e: React.KeyboardEvent<HTMLInputElement>) =>
e.key === "Enter" && handleOnSubmit()
}
icon={
isValid ? (
<IconButton
onClick={handleOnSubmit}
aria-label="Select plus button"
>
<Icon glyph="Plus" data-cy="tuple-select-button" />
</IconButton>
) : (
<IconTooltip
glyph="Warning"
data-cy="tuple-select-warning"
fill={yellow.base}
>
{validatorErrorMessage}
</IconTooltip>
)
}
validator={validator}
validatorErrorMessage={validatorErrorMessage}
onSubmit={handleOnSubmit}
/>
</InputGroup>
</Container>
Expand Down

0 comments on commit 05b8d24

Please sign in to comment.