generated from abelflopes/lerna-monorepo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose form field context with id and skin
- Loading branch information
1 parent
86f9287
commit 7143b75
Showing
5 changed files
with
89 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React, { useMemo } from "react"; | ||
import styles from "./styles/index.module.scss"; | ||
import classNames from "classnames"; | ||
import { Text } from "@react-ck/text"; | ||
import { FormFieldContext, FormFieldContextProps } from "./context"; | ||
|
||
export interface FormFieldProps | ||
extends Pick<React.HTMLAttributes<HTMLDivElement>, "children" | "className"> { | ||
/** Specifies the visual style of the form-field */ | ||
skin?: FormFieldContextProps["skin"]; | ||
/** The main label for the form field */ | ||
label?: React.ReactNode; | ||
/** The description text for the form field */ | ||
description?: React.ReactNode; | ||
/** The validation message text */ | ||
validationMessage?: React.ReactNode; | ||
} | ||
|
||
/** | ||
* FormField is a component that provides a consistent layout and input peripherals. | ||
* | ||
* **WARNING**: This component is used as an internal utility - if you want to render an element such as an input, use its component directly | ||
* @param props - {@link FormFieldProps} | ||
* @returns a React element | ||
*/ | ||
|
||
export const FormField = ({ | ||
skin = "default", | ||
label, | ||
description, | ||
validationMessage, | ||
children, | ||
className, | ||
...otherProps | ||
}: Readonly<FormFieldProps>): React.ReactElement => { | ||
const id = React.useId(); | ||
|
||
const context = useMemo<FormFieldContextProps>( | ||
() => ({ | ||
skin, | ||
id, | ||
}), | ||
[id, skin], | ||
); | ||
|
||
return ( | ||
<FormFieldContext.Provider value={context}> | ||
<div {...otherProps} className={classNames(styles.root, styles[skin], className)}> | ||
{label ? ( | ||
<Text variation="small" margin="none" as={<label htmlFor={id}>{label}</label>} /> | ||
) : null} | ||
|
||
<div className={styles.content}>{children}</div> | ||
|
||
{description ? ( | ||
<Text variation="small" margin="none"> | ||
{description} | ||
</Text> | ||
) : null} | ||
|
||
{validationMessage ? ( | ||
<Text className={styles.validation_message} variation="small" margin="none"> | ||
{validationMessage} | ||
</Text> | ||
) : null} | ||
</div> | ||
</FormFieldContext.Provider> | ||
); | ||
}; |
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,13 @@ | ||
import React from "react"; | ||
|
||
export interface FormFieldContextProps { | ||
skin: "default" | "negative" | "average" | "positive" | "ghost"; | ||
id: string; | ||
} | ||
|
||
export const FormFieldContext = React.createContext<FormFieldContextProps>({ | ||
id: "", | ||
skin: "default", | ||
}); | ||
|
||
export const useFormFieldContext = (): FormFieldContextProps => React.useContext(FormFieldContext); |
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,3 @@ | ||
export * from "./FormField"; | ||
|
||
export { useFormFieldContext } from "./context"; |
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