Skip to content

Commit

Permalink
chore: added helperMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
aizad-deriv committed Jan 23, 2024
1 parent 4453a1c commit 3bd81be
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
20 changes: 20 additions & 0 deletions lib/components/Input/HelperMessage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from "react";
import clsx from "clsx";
import "./Input.scss";

export interface HelperMessageProps {
message?: string;
hasError?: boolean;
}

const HelperMessage = ({ message, hasError }: HelperMessageProps) => (
<p
className={clsx("deriv-helper-message", {
"deriv-helper-message__error": hasError,
})}
>
{message}
</p>
);

export default HelperMessage;
17 changes: 17 additions & 0 deletions lib/components/Input/Input.scss
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ $border: 1px solid;
color: $error_field;
}
}

&--helper-message {
position: absolute;
left: 16px;
margin-top: 2px;
}
}

.deriv-input--field:disabled + .deriv-input--label {
Expand All @@ -79,3 +85,14 @@ $border: 1px solid;
.deriv-input--field__error:focus + .deriv-input--label {
color: $error_field;
}

.deriv-helper-message {
font-size: 12px;
font-style: normal;
font-weight: 400;
line-height: 18px;
color: $inactive_color;
&__error {
color: $error_field;
}
}
31 changes: 24 additions & 7 deletions lib/components/Input/index.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
import React, { ChangeEvent, ComponentProps, useState } from "react";
import React, { ChangeEvent, ComponentProps, ReactNode, useState } from "react";
import clsx from "clsx";
import HelperMessage, { HelperMessageProps } from "./HelperMessage";
import "./Input.scss";

interface InputProps
extends Omit<ComponentProps<"input">, "style" | "placeholder"> {
extends Omit<ComponentProps<"input">, "style" | "placeholder">,
HelperMessageProps {
label?: string;
helperMessage?: string;
error?: boolean;
hasError?: boolean;
hasRightPlaceholder?: ReactNode;
}

export const Input = ({ label, id, error, ...rest }: InputProps) => {
export const Input = ({
label,
id,
hasError,
message,
hasRightPlaceholder,
...rest
}: InputProps) => {
const [value, setValue] = useState("");
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
setValue(e.target.value);
rest.onChange?.(e);
};

return (
<div className="deriv-input">
<input
className={clsx("deriv-input--field", {
"deriv-input--field__error": error,
"deriv-input--field__error": hasError,
className: rest.className,
})}
id={id}
value={value}
Expand All @@ -28,12 +39,18 @@ export const Input = ({ label, id, error, ...rest }: InputProps) => {
/>
<label
className={clsx("deriv-input--label", {
"deriv-input--label__error": error,
"deriv-input--label__error": hasError,
})}
htmlFor={id}
>
{label}
</label>
{hasRightPlaceholder && (
<div className="deriv-input--right-content">{hasRightPlaceholder}</div>
)}
<div className="deriv-input--helper-message">
{message && <HelperMessage message={message} hasError={hasError} />}
</div>
</div>
);
};

0 comments on commit 3bd81be

Please sign in to comment.