(react) add textItems to Field

This allow to add bullet points lists below inputs, the best example
is when we want to list multiple errors from form validation.

Fixes #147
This commit is contained in:
Nathan Vasse
2023-08-29 17:37:06 +02:00
committed by NathanVss
parent 468c8161eb
commit b530e966ae
11 changed files with 98 additions and 9 deletions

View File

@@ -0,0 +1,5 @@
---
"@openfun/cunningham-react": major
---
add textItems to Field

View File

@@ -23,6 +23,7 @@ export const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>(
checked, checked,
label, label,
text, text,
textItems,
rightText, rightText,
state, state,
...props ...props
@@ -48,7 +49,13 @@ export const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>(
"c__checkbox--disabled": props.disabled, "c__checkbox--disabled": props.disabled,
})} })}
> >
<Field text={text} rightText={rightText} compact={true} state={state}> <Field
text={text}
rightText={rightText}
compact={true}
state={state}
textItems={textItems}
>
<div className="c__checkbox__container"> <div className="c__checkbox__container">
<div className="c__checkbox__wrapper"> <div className="c__checkbox__wrapper">
<input <input
@@ -82,6 +89,7 @@ export const CheckboxGroup = ({
children, children,
state, state,
text, text,
textItems,
rightText, rightText,
}: PropsWithChildren & FieldProps) => { }: PropsWithChildren & FieldProps) => {
return ( return (
@@ -89,6 +97,7 @@ export const CheckboxGroup = ({
className="c__checkbox__group" className="c__checkbox__group"
state={state} state={state}
text={text} text={text}
textItems={textItems}
rightText={rightText} rightText={rightText}
> >
<div className="c__checkbox__group__list">{children}</div> <div className="c__checkbox__group__list">{children}</div>

View File

@@ -9,8 +9,17 @@
&__footer { &__footer {
font-size: var(--c--components--forms-field--font-size); font-size: var(--c--components--forms-field--font-size);
padding: 0.25rem calc(1rem + 2px) 0 calc(1rem + 2px); padding: 0.25rem calc(1rem + 2px) 0 calc(1rem + 2px);
display: flex;
justify-content: space-between; &__top {
display: flex;
justify-content: space-between;
}
ul {
margin: 0.20rem 0 0 0;
padding: 0 0 0 1rem;
line-height: 1rem;
}
} }
&__text, &__text-right { &__text, &__text-right {

View File

@@ -38,6 +38,11 @@ export const Error = {
args: { args: {
state: "error", state: "error",
text: "This is an optional error message", text: "This is an optional error message",
textItems: [
"Text too long",
"Wrong choice",
"Must contain at least 9 characters, uppercase and digits",
],
rightText: "Right text", rightText: "Right text",
}, },
}; };

View File

@@ -6,6 +6,7 @@ export type FieldState = "success" | "error" | "default";
export type FieldProps = { export type FieldProps = {
state?: FieldState | undefined; state?: FieldState | undefined;
text?: string | undefined; text?: string | undefined;
textItems?: string[] | undefined;
rightText?: string | undefined; rightText?: string | undefined;
fullWidth?: boolean | undefined; fullWidth?: boolean | undefined;
compact?: boolean | undefined; compact?: boolean | undefined;
@@ -18,6 +19,7 @@ export const Field = ({
children, children,
state = "default", state = "default",
text, text,
textItems,
rightText, rightText,
fullWidth, fullWidth,
compact, compact,
@@ -31,10 +33,21 @@ export const Field = ({
})} })}
> >
{children} {children}
{(text || rightText) && ( {(text || rightText || textItems) && (
<div className="c__field__footer"> <div className="c__field__footer">
<span className="c__field__text">{text}</span> {(text || rightText) && (
<span className="c__field__text__right">{rightText}</span> <div className="c__field__footer__top">
<span className="c__field__text">{text}</span>
<span className="c__field__text__right">{rightText}</span>
</div>
)}
{textItems && (
<ul className="c__field__footer__items">
{textItems.map((textItem) => (
<li key={textItem}>{textItem}</li>
))}
</ul>
)}
</div> </div>
)} )}
</div> </div>

View File

@@ -119,6 +119,26 @@ describe("<Input/>", () => {
); );
screen.getByText("Some text"); screen.getByText("Some text");
}); });
it("renders with text items", async () => {
render(
<Input
label="First name"
rightIcon={<span className="material-icons">apartment</span>}
textItems={[
"Text too long",
"Wrong choice",
"Must contain at least 9 characters, uppercase and digits",
]}
/>,
);
expect(
screen.getAllByRole("listitem").map((item) => item.textContent),
).toEqual([
"Text too long",
"Wrong choice",
"Must contain at least 9 characters, uppercase and digits",
]);
});
it("renders with text and text right", async () => { it("renders with text and text right", async () => {
render( render(
<Input <Input

View File

@@ -43,6 +43,21 @@ export const Error = {
}, },
}; };
export const ErrorItems = {
args: {
defaultValue: "Hello world",
label: "Your name",
state: "error",
icon: <span className="material-icons">person</span>,
text: "This is an optional error message",
textItems: [
"Text too long",
"Wrong choice",
"Must contain at least 9 characters, uppercase and digits",
],
},
};
export const DisabledEmpty = { export const DisabledEmpty = {
args: { args: {
label: "Your name", label: "Your name",

View File

@@ -31,6 +31,7 @@ export const Input = forwardRef<HTMLInputElement, InputProps>(
rightIcon, rightIcon,
state = "default", state = "default",
text, text,
textItems,
rightText, rightText,
fullWidth, fullWidth,
charCounter, charCounter,
@@ -76,6 +77,7 @@ export const Input = forwardRef<HTMLInputElement, InputProps>(
<Field <Field
state={state} state={state}
text={text} text={text}
textItems={textItems}
rightText={rightTextToUse} rightText={rightTextToUse}
fullWidth={fullWidth} fullWidth={fullWidth}
> >

View File

@@ -12,14 +12,14 @@ export type RadioProps = InputHTMLAttributes<HTMLInputElement> &
}; };
export const Radio = forwardRef<HTMLInputElement, RadioProps>( export const Radio = forwardRef<HTMLInputElement, RadioProps>(
({ label, text, state, ...props }: RadioProps, ref) => { ({ label, text, textItems, state, ...props }: RadioProps, ref) => {
return ( return (
<label <label
className={classNames("c__checkbox", "c__radio", { className={classNames("c__checkbox", "c__radio", {
"c__checkbox--disabled": props.disabled, "c__checkbox--disabled": props.disabled,
})} })}
> >
<Field text={text} compact={true} state={state}> <Field text={text} compact={true} state={state} textItems={textItems}>
<div className="c__checkbox__container"> <div className="c__checkbox__container">
<input type="radio" {...props} ref={ref} /> <input type="radio" {...props} ref={ref} />
{label && <div className="c__checkbox__label">{label}</div>} {label && <div className="c__checkbox__label">{label}</div>}
@@ -34,6 +34,7 @@ export const RadioGroup = ({
children, children,
state, state,
text, text,
textItems,
rightText, rightText,
style, style,
}: PropsWithChildren & FieldProps & { style?: React.CSSProperties }) => { }: PropsWithChildren & FieldProps & { style?: React.CSSProperties }) => {
@@ -42,6 +43,7 @@ export const RadioGroup = ({
className="c__radio__group c__checkbox__group" className="c__radio__group c__checkbox__group"
state={state} state={state}
text={text} text={text}
textItems={textItems}
rightText={rightText} rightText={rightText}
compact={true} compact={true}
> >

View File

@@ -58,6 +58,7 @@ export const SelectMonoAux = ({
children, children,
state = "default", state = "default",
text, text,
textItems,
rightText, rightText,
fullWidth, fullWidth,
options, options,
@@ -90,6 +91,7 @@ export const SelectMonoAux = ({
<Field <Field
state={state} state={state}
text={text} text={text}
textItems={textItems}
rightText={rightText} rightText={rightText}
fullWidth={fullWidth} fullWidth={fullWidth}
> >

View File

@@ -13,6 +13,7 @@ export const Switch = forwardRef<HTMLInputElement, SwitchProps>(
{ {
label, label,
text, text,
textItems,
state, state,
fullWidth, fullWidth,
labelSide = "left", labelSide = "left",
@@ -32,7 +33,13 @@ export const Switch = forwardRef<HTMLInputElement, SwitchProps>(
}, },
)} )}
> >
<Field text={text} compact={true} state={state} fullWidth={fullWidth}> <Field
text={text}
textItems={textItems}
compact={true}
state={state}
fullWidth={fullWidth}
>
<div className="c__checkbox__container"> <div className="c__checkbox__container">
{label && <div className="c__checkbox__label">{label}</div>} {label && <div className="c__checkbox__label">{label}</div>}
<div className="c__switch__rail__wrapper"> <div className="c__switch__rail__wrapper">