-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
3,664 additions
and
770 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
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
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,83 @@ | ||
import type { Field } from "../data-types"; | ||
import { | ||
FauxInput, | ||
Field as GardenField, | ||
Hint, | ||
Input, | ||
Label, | ||
Message, | ||
} from "@zendeskgarden/react-forms"; | ||
import * as tagsInput from "@zag-js/tags-input"; | ||
import { useMachine, normalizeProps } from "@zag-js/react"; | ||
import styled from "styled-components"; | ||
import type { ComponentProps } from "react"; | ||
import { Tag } from "@zendeskgarden/react-tags"; | ||
|
||
interface CcFieldProps { | ||
field: Field; | ||
} | ||
|
||
const EMAIL_REGEX = | ||
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | ||
|
||
export default function CcField({ field }: CcFieldProps): JSX.Element { | ||
const { label, value, name, error, description, id } = field; | ||
const initialValue = value | ||
? value.split(",").map((email) => email.trim()) | ||
: []; | ||
const [state, send] = useMachine( | ||
tagsInput.machine({ | ||
id, | ||
value: initialValue, | ||
allowEditTag: false, | ||
validate: ({ inputValue, values }) => | ||
!values.includes(inputValue) && EMAIL_REGEX.test(inputValue), | ||
}) | ||
); | ||
|
||
const api = tagsInput.connect(state, send, normalizeProps); | ||
|
||
return ( | ||
<GardenField {...api.rootProps}> | ||
<Label {...(api.labelProps as ComponentProps<typeof Label>)}> | ||
{label} | ||
</Label> | ||
{description && <Hint>{description}</Hint>} | ||
<Control {...api.controlProps} validation={error ? "error" : undefined}> | ||
{api.value.map((email, index) => ( | ||
<span key={index}> | ||
<Tag {...api.getTagProps({ index, value: email })} size="large"> | ||
<span>{email}</span> | ||
<Tag.Close | ||
{...(api.getTagDeleteTriggerProps({ | ||
index, | ||
value: email, | ||
}) as ComponentProps<typeof Tag.Close>)} | ||
/> | ||
</Tag> | ||
</span> | ||
))} | ||
<StyledInput | ||
isBare | ||
{...(api.inputProps as ComponentProps<typeof StyledInput>)} | ||
/> | ||
</Control> | ||
{error && <Message validation="error">{error}</Message>} | ||
{api.value.map((email) => ( | ||
<input key={email} type="hidden" name={name} value={email} /> | ||
))} | ||
</GardenField> | ||
); | ||
} | ||
|
||
const Control = styled(FauxInput)` | ||
display: flex; | ||
flex-direction: row; | ||
flex-wrap: wrap; | ||
gap: ${(p) => p.theme.space.md}; | ||
`; | ||
|
||
const StyledInput = styled(Input)` | ||
width: revert; | ||
flex: 1; | ||
`; |
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
Oops, something went wrong.