-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Denys
committed
Feb 21, 2025
1 parent
9663976
commit 35e0a9f
Showing
5 changed files
with
202 additions
and
60 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -1,56 +1,39 @@ | ||
import classNames from 'classnames'; | ||
import React, { useState } from 'react'; | ||
import React from 'react'; | ||
|
||
type Props = { | ||
interface TextFieldProps { | ||
name: string; | ||
label: string; | ||
value: string; | ||
label?: string; | ||
placeholder?: string; | ||
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void; | ||
onBlur?: () => void; | ||
required?: boolean; | ||
onChange?: (newValue: string) => void; | ||
}; | ||
|
||
function getRandomDigits() { | ||
return Math.random().toFixed(16).slice(2); | ||
error?: string; | ||
} | ||
|
||
export const TextField: React.FC<Props> = ({ | ||
export const TextField: React.FC<TextFieldProps> = ({ | ||
name, | ||
label, | ||
value, | ||
label = name, | ||
placeholder = `Enter ${label}`, | ||
required = false, | ||
onChange = () => {}, | ||
onChange, | ||
onBlur, | ||
required, | ||
error, | ||
}) => { | ||
// generate a unique id once on component load | ||
const [id] = useState(() => `${name}-${getRandomDigits()}`); | ||
|
||
// To show errors only if the field was touched (onBlur) | ||
const [touched, setTouched] = useState(false); | ||
const hasError = touched && required && !value; | ||
|
||
return ( | ||
<div className="field"> | ||
<label className="label" htmlFor={id}> | ||
{label} | ||
</label> | ||
|
||
<div className={`field ${error ? 'is-danger' : ''}`}> | ||
<label className="label">{label}</label> | ||
<div className="control"> | ||
<input | ||
className={`input ${error ? 'is-danger' : ''}`} | ||
type="text" | ||
id={id} | ||
data-cy={`movie-${name}`} | ||
className={classNames('input', { | ||
'is-danger': hasError, | ||
})} | ||
placeholder={placeholder} | ||
name={name} | ||
value={value} | ||
onChange={event => onChange(event.target.value)} | ||
onBlur={() => setTouched(true)} | ||
onChange={onChange} | ||
onBlur={onBlur} | ||
required={required} | ||
/> | ||
</div> | ||
|
||
{hasError && <p className="help is-danger">{`${label} is required`}</p>} | ||
{error && <p className="help is-danger">{error}</p>} | ||
</div> | ||
); | ||
}; |