You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			115 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			TypeScript
		
	
		
		
			
		
	
	
			115 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			TypeScript
		
	
| 
											4 years ago
										 | import React, { useState } from 'react'; | ||
| 
											6 years ago
										 | 
 | ||
|  | import classNames from 'classnames'; | ||
| 
											4 years ago
										 | import { SessionIconButton } from '../icon'; | ||
| 
											6 years ago
										 | 
 | ||
| 
											4 years ago
										 | type Props = { | ||
| 
											5 years ago
										 |   label?: string; | ||
| 
											6 years ago
										 |   error?: string; | ||
| 
											5 years ago
										 |   type?: string; | ||
| 
											6 years ago
										 |   value?: string; | ||
| 
											6 years ago
										 |   placeholder: string; | ||
| 
											6 years ago
										 |   maxLength?: number; | ||
| 
											6 years ago
										 |   enableShowHide?: boolean; | ||
| 
											5 years ago
										 |   onValueChanged?: (value: string) => any; | ||
| 
											6 years ago
										 |   onEnterPressed?: any; | ||
| 
											5 years ago
										 |   autoFocus?: boolean; | ||
| 
											5 years ago
										 |   ref?: any; | ||
| 
											4 years ago
										 |   inputDataTestId?: string; | ||
|  | }; | ||
|  | 
 | ||
|  | const LabelItem = (props: { inputValue: string; label?: string }) => { | ||
|  |   return ( | ||
|  |     <label | ||
|  |       htmlFor="session-input-floating-label" | ||
|  |       className={classNames( | ||
|  |         props.inputValue !== '' | ||
|  |           ? 'session-input-with-label-container filled' | ||
|  |           : 'session-input-with-label-container' | ||
|  |       )} | ||
|  |     > | ||
|  |       {props.label} | ||
|  |     </label> | ||
|  |   ); | ||
|  | }; | ||
|  | 
 | ||
|  | const ErrorItem = (props: { error: string | undefined }) => { | ||
|  |   return ( | ||
|  |     <label | ||
|  |       htmlFor="session-input-floating-label" | ||
|  |       className={classNames('session-input-with-label-container filled error')} | ||
|  |     > | ||
|  |       {props.error} | ||
|  |     </label> | ||
|  |   ); | ||
|  | }; | ||
|  | 
 | ||
|  | const ShowHideButton = (props: { toggleForceShow: () => void }) => { | ||
|  |   return <SessionIconButton iconType="eye" iconSize="medium" onClick={props.toggleForceShow} />; | ||
|  | }; | ||
|  | 
 | ||
|  | export const SessionInput = (props: Props) => { | ||
|  |   const { | ||
|  |     autoFocus, | ||
|  |     placeholder, | ||
|  |     type, | ||
|  |     value, | ||
|  |     maxLength, | ||
|  |     enableShowHide, | ||
|  |     error, | ||
|  |     label, | ||
|  |     onValueChanged, | ||
|  |     inputDataTestId, | ||
|  |   } = props; | ||
|  |   const [inputValue, setInputValue] = useState(''); | ||
|  |   const [forceShow, setForceShow] = useState(false); | ||
|  | 
 | ||
|  |   const correctType = forceShow ? 'text' : type; | ||
|  | 
 | ||
|  |   const updateInputValue = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
|  |     e.preventDefault(); | ||
|  |     const val = e.target.value; | ||
|  |     setInputValue(val); | ||
|  |     if (onValueChanged) { | ||
|  |       onValueChanged(val); | ||
|  |     } | ||
|  |   }; | ||
|  | 
 | ||
|  |   return ( | ||
|  |     <div className="session-input-with-label-container"> | ||
|  |       {error ? ( | ||
|  |         <ErrorItem error={props.error} /> | ||
|  |       ) : ( | ||
|  |         <LabelItem inputValue={inputValue} label={label} /> | ||
|  |       )} | ||
|  |       <input | ||
|  |         id="session-input-floating-label" | ||
|  |         type={correctType} | ||
|  |         placeholder={placeholder} | ||
|  |         value={value} | ||
|  |         maxLength={maxLength} | ||
|  |         autoFocus={autoFocus} | ||
|  |         data-testid={inputDataTestId} | ||
|  |         onChange={updateInputValue} | ||
|  |         className={classNames(enableShowHide ? 'session-input-floating-label-show-hide' : '')} | ||
|  |         // just incase onChange isn't triggered
 | ||
|  |         onBlur={updateInputValue} | ||
|  |         onKeyPress={event => { | ||
|  |           if (event.key === 'Enter' && props.onEnterPressed) { | ||
|  |             props.onEnterPressed(); | ||
|  |           } | ||
| 
											6 years ago
										 |         }} | ||
|  |       /> | ||
| 
											6 years ago
										 | 
 | ||
| 
											4 years ago
										 |       {enableShowHide && ( | ||
|  |         <ShowHideButton | ||
|  |           toggleForceShow={() => { | ||
|  |             setForceShow(!forceShow); | ||
|  |           }} | ||
|  |         /> | ||
|  |       )} | ||
|  |       <hr /> | ||
|  |     </div> | ||
|  |   ); | ||
|  | }; |