-
Notifications
You must be signed in to change notification settings - Fork 13
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
11 changed files
with
224 additions
and
27 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* Copyright (c) 2022, VRAI Labs and/or its affiliates. All rights reserved. | ||
* | ||
* This software is licensed under the Apache License, Version 2.0 (the | ||
* "License") as published by the Apache Software Foundation. | ||
* | ||
* You may not use this file except in compliance with the License. You may | ||
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import React, { useCallback, useState } from "react"; | ||
import { getImageUrl } from "../../../utils"; | ||
import TooltipContainer from "../tooltip/tooltip"; | ||
|
||
import "./InputField.css"; | ||
|
||
export type InputDropdownPropTypes = { | ||
name: string; | ||
size?: "small" | "medium"; | ||
label?: string; | ||
value?: string | undefined; | ||
options: string[]; | ||
placeholder?: string; | ||
error?: string | JSX.Element; | ||
isRequired?: boolean; | ||
hideColon?: boolean; | ||
forceShowError?: boolean; | ||
disabled?: boolean; | ||
prefix?: string; | ||
autofocus?: boolean; | ||
handleChange: ( | ||
event: | ||
| React.ChangeEvent<HTMLInputElement> | ||
| React.ChangeEvent<HTMLTextAreaElement> | ||
| React.ChangeEvent<HTMLSelectElement> | ||
) => void; | ||
/** @default "bottom" */ | ||
errorPlacement?: "bottom" | "prefix-tooltip"; | ||
}; | ||
|
||
const InputDropdown: React.FC<InputDropdownPropTypes> = (props) => { | ||
const handleChange = props.handleChange; | ||
const { errorPlacement = "bottom" } = props; | ||
const [isFocused, setIsFocused] = useState<boolean>(false); | ||
const [isTouched, setIsTouched] = useState(false); | ||
|
||
const onChange = useCallback( | ||
( | ||
event: | ||
| React.ChangeEvent<HTMLInputElement> | ||
| React.ChangeEvent<HTMLTextAreaElement> | ||
| React.ChangeEvent<HTMLSelectElement> | ||
) => { | ||
setIsTouched(true); | ||
handleChange(event); | ||
}, | ||
[handleChange] | ||
); | ||
|
||
const showError = props.error && (isTouched || props.forceShowError); | ||
|
||
return ( | ||
<div className="input-field-container"> | ||
{props.label && ( | ||
<label | ||
htmlFor={props.name} | ||
className="text-small input-label"> | ||
{props.label} | ||
{props.isRequired && <span className="text-error input-label-required">*</span>} | ||
{!props.hideColon ? ":" : ""} | ||
</label> | ||
)} | ||
<div | ||
className={`input-field-inset ${isFocused ? "input-field-inset-focused" : ""} ${ | ||
showError ? "input-field-inset-error-state" : "" | ||
} ${props.prefix ? "input-field-inset-with-prefix" : ""}`}> | ||
{props.prefix && ( | ||
<div | ||
className={`input-field-prefix ${isFocused ? "input-field-prefix-focused" : ""} ${ | ||
showError ? "input-field-prefix-error" : "" | ||
}`}> | ||
{props.prefix} | ||
</div> | ||
)} | ||
<select | ||
name={props.name} | ||
id={props.name + "-text"} | ||
onChange={onChange} | ||
value={props.value} | ||
autoFocus={props.autofocus} | ||
onFocus={() => setIsFocused(true)} | ||
onBlur={() => setIsFocused(false)} | ||
className={`text-small text-black input-field ${showError ? "input-field-error-state" : ""} ${ | ||
props.size === "small" ? "input-field-small" : "" | ||
}`}> | ||
{props.options.map((option, index) => ( | ||
<option | ||
key={index} | ||
value={option}> | ||
{option} | ||
</option> | ||
))} | ||
</select> | ||
</div> | ||
{showError && errorPlacement === "bottom" && ( | ||
<div className="input-field-error block-small block-error"> | ||
<img | ||
className="input-field-error-icon" | ||
src={getImageUrl("form-field-error-icon.svg")} | ||
alt="Error in field" | ||
/> | ||
<p className="input-field-error-text text-small text-error">{props.error}</p> | ||
</div> | ||
)} | ||
{showError && errorPlacement === "prefix-tooltip" && ( | ||
<div className="input-error-prefix-tooltip"> | ||
<TooltipContainer | ||
tooltip={props.error} | ||
position="bottom"> | ||
<img | ||
className="input-field-error-icon" | ||
src={getImageUrl("form-field-error-icon.svg")} | ||
alt="Error in field" | ||
/> | ||
</TooltipContainer> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default InputDropdown; |
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
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