'use client'; import { bool, oneOf, string } from 'prop-types'; import { TextFieldInterface } from './text-field.types'; import React, { FocusEvent, forwardRef, useState } from 'react'; export const TextField = forwardRef( ( { variant = 'filled', withAfterIcon, withBeforeIcon, supportingText, ...props }, ref, ) => { const [raised, setRaised] = useState(!!props.placeholder); const callback = (e: FocusEvent): void => { if ( e.type === 'blur' && e.target.value.length === 0 && !props.placeholder ) { setRaised(false); } else if (e.type === 'focus') { setRaised(true); } }; const iconStyles = withBeforeIcon && withAfterIcon ? 'with-before-icon with-after-icon' : withBeforeIcon ? 'with-before-icon' : withAfterIcon ? 'with-after-icon' : ''; return (
{variant === 'outlined' && (
Label
)} {withBeforeIcon && ( {withBeforeIcon && 'search'} )} { callback(event); if (props.onBlur) { props.onBlur(event); } }} onFocus={event => { callback(event); if (props.onFocus) { props.onFocus(event); } }} /> {withAfterIcon && ( {withAfterIcon && 'cancel'} )}
{supportingText !== '' && ( {supportingText} )}
); }, ); TextField.propTypes = { children: string, className: string, placeholder: string, withAfterIcon: bool, withBeforeIcon: bool, supportingText: string, variant: oneOf(['filled', 'outlined']), };