-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: reuse input and tag input components from react-kit (#1083)
- Loading branch information
1 parent
5efe5a1
commit d2ed435
Showing
9 changed files
with
57 additions
and
193 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
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
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 |
---|---|---|
@@ -1,122 +1,8 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { useField, useFormikContext } from "formik"; | ||
import { KeyReturn } from "phosphor-react"; | ||
import { useEffect, useRef } from "react"; | ||
import { BaseTagsInput, BaseTagsInputProps } from "@bosonprotocol/react-kit"; | ||
import { inputTheme } from "theme"; | ||
|
||
import { Grid } from "../ui/Grid"; | ||
import { Typography } from "../ui/Typography"; | ||
import Error from "./Error"; | ||
import { FieldInput } from "./Field.styles"; | ||
import { | ||
Close, | ||
Helper, | ||
TagContainer, | ||
TagWrapper | ||
} from "./styles/TagsInput.styles"; | ||
import { TagsProps } from "./types"; | ||
|
||
const TagsInput = ({ | ||
name, | ||
placeholder, | ||
onAddTag, | ||
onRemoveTag, | ||
compareTags = (tagA: string, tagB: string) => | ||
tagA.toLowerCase() === tagB.toLowerCase(), | ||
transform = (tag: string) => tag, | ||
label | ||
}: TagsProps) => { | ||
const { validateForm } = useFormikContext(); | ||
const [field, meta, helpers] = useField<string[]>(name); | ||
const tags = field.value || []; | ||
|
||
const errorMessage = meta.error && meta.touched ? meta.error : ""; | ||
const displayError = | ||
typeof errorMessage === typeof "string" && errorMessage !== ""; | ||
|
||
const handleBlur = () => { | ||
if (!meta.touched) { | ||
helpers.setTouched(true); | ||
} | ||
}; | ||
|
||
function handleKeyDown(event: any) { | ||
if (event.key !== "Enter") return; | ||
event.preventDefault(); | ||
const value: string = event.target.value; | ||
if (!value.trim()) return; | ||
event.target.value = ""; | ||
if (!meta.touched) { | ||
helpers.setTouched(true); | ||
} | ||
|
||
if (!tags.find((tag) => compareTags(tag, value))) { | ||
const transformedValue = transform(value); | ||
const newTags = [...tags, transformedValue]; | ||
helpers.setValue(newTags); | ||
onAddTag?.(transformedValue); | ||
} | ||
} | ||
|
||
function removeTag(index: number) { | ||
const filteredTags = tags.filter((_, i) => i !== index); | ||
helpers.setValue(filteredTags); | ||
if (!meta.touched) { | ||
helpers.setTouched(true); | ||
} | ||
onRemoveTag?.(tags[index]); | ||
} | ||
useEffect(() => { | ||
validateForm(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [field.value]); | ||
const labelRef = useRef<HTMLDivElement>(null); | ||
const hitEnterWidth = useRef<HTMLDivElement>(null); | ||
return ( | ||
<> | ||
<Grid gap="0.5rem" alignItems="center"> | ||
{label && ( | ||
<Typography data-label ref={labelRef}> | ||
{label} | ||
</Typography> | ||
)} | ||
<TagContainer> | ||
<FieldInput | ||
onKeyDown={handleKeyDown} | ||
type="text" | ||
placeholder={placeholder || "Choose tags..."} | ||
name={name} | ||
onBlur={handleBlur} | ||
error={errorMessage} | ||
{...(hitEnterWidth.current?.clientWidth && { | ||
style: { | ||
paddingRight: `calc(${hitEnterWidth.current.clientWidth}px + 1rem)` | ||
} | ||
})} | ||
/> | ||
<Helper ref={hitEnterWidth}> | ||
Hit Enter <KeyReturn size={13} /> | ||
</Helper> | ||
</TagContainer> | ||
</Grid> | ||
<TagContainer> | ||
{label && ( | ||
<div | ||
style={{ | ||
visibility: "hidden", | ||
width: labelRef.current?.clientWidth | ||
}} | ||
/> | ||
)} | ||
{tags.map((tag: string, index: number) => ( | ||
<TagWrapper key={`tags-wrapper_${tag}`}> | ||
<span className="text tag">{tag}</span> | ||
<Close onClick={() => removeTag(index)}>×</Close> | ||
</TagWrapper> | ||
))} | ||
</TagContainer> | ||
<Error display={displayError} message={errorMessage} />{" "} | ||
</> | ||
); | ||
const TagsInput = (props: Omit<BaseTagsInputProps, "theme">) => { | ||
return <BaseTagsInput {...props} theme={inputTheme} />; | ||
}; | ||
|
||
export default TagsInput; |
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 was deleted.
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 |
---|---|---|
@@ -1,9 +1,38 @@ | ||
import "styled-components"; | ||
|
||
import { theme as bosonTheme } from "@bosonprotocol/react-kit"; | ||
import { | ||
BaseTagsInputProps, | ||
theme as bosonTheme | ||
} from "@bosonprotocol/react-kit"; | ||
import { colors } from "lib/styles/colors"; | ||
|
||
const theme = { | ||
...bosonTheme | ||
}; | ||
|
||
export const inputTheme = { | ||
background: colors.lightGrey, | ||
borderColor: colors.border, | ||
borderRadius: 0, | ||
focus: { | ||
caretColor: "initial" | ||
}, | ||
hover: { | ||
borderColor: "var(--secondary)" | ||
}, | ||
error: { | ||
borderColor: colors.orange, | ||
hover: { | ||
borderColor: colors.orange | ||
}, | ||
focus: { | ||
borderColor: "var(--secondary)", | ||
caretColor: colors.orange | ||
}, | ||
placeholder: { | ||
color: colors.orange | ||
} | ||
} | ||
} satisfies BaseTagsInputProps["theme"]; | ||
|
||
export default theme; |