'use client' import { ChangeEvent, ChangeEventHandler, ForwardedRef, forwardRef, InputHTMLAttributes, ReactNode, useEffect, useState, } from 'react' import { PktIcon } from '../icon/Icon' import { PktInputWrapper } from '../inputwrapper/InputWrapper' export interface IPktTextinput extends InputHTMLAttributes { id: string ariaDescribedby?: string ariaLabelledby?: string autocomplete?: string counter?: boolean counterMaxLength?: number disabled?: boolean errorMessage?: string | ReactNode | ReactNode[] hasError?: boolean helptext?: string | ReactNode | ReactNode[] helptextDropdown?: string | ReactNode | ReactNode[] helptextDropdownButton?: string iconNameRight?: string inline?: boolean fullwidth?: boolean label: string name?: string optionalTag?: boolean optionalText?: string requiredTag?: boolean requiredText?: string tagText?: string | null placeholder?: string prefix?: string suffix?: string type?: string useWrapper?: boolean value?: string omitSearchIcon?: boolean min?: number | string max?: number | string minLength?: number maxLength?: number step?: string size?: number readonly?: boolean required?: boolean dataTestid?: string onChange?: ChangeEventHandler skipForwardTestid?: boolean } export const PktTextinput = forwardRef( ( { id, ariaDescribedby, ariaLabelledby, autocomplete = 'off', counter, counterMaxLength, className, disabled = false, errorMessage, hasError = false, helptext, helptextDropdown, helptextDropdownButton, iconNameRight, inline = false, fullwidth = false, label, name, optionalTag = false, optionalText, requiredTag = false, requiredText, tagText = null, placeholder, prefix, suffix, type = 'text', useWrapper = true, omitSearchIcon = false, value, minLength, maxLength, min, max, step, size, readonly: readOnly, required, dataTestid, onChange, skipForwardTestid = false, ...props }: IPktTextinput, ref: ForwardedRef, ) => { const classNames = [className, 'pkt-textinput'].join(' ') const shouldShowSearchIcon = type === 'search' && !iconNameRight && !omitSearchIcon const isoValue = type === 'date' && value ? value.slice(0, 10) : value const [counterCurrent, setCounterCurrent] = useState(value?.length || 0) const inputId = `${id}` // id til input element og sendes inn til htmlFor i InputWrapper const labelId = `${inputId}-label` // id til label elementet og sendes inn til aria-labelledby i input (genereres i InputWrapper) const labelledBy = ariaLabelledby || labelId const handleChange = (event: ChangeEvent) => { if (counter) { setCounterCurrent(event.currentTarget?.value?.length || 0) } if (onChange) { onChange(event) } } useEffect(() => { if (value !== undefined) { setCounterCurrent(value?.length || 0) } }, [value]) return (
{prefix &&
{prefix}
} counterMaxLength ? 'pkt-input--counter-error' : '' }`} type={type} name={`${name || id}`} value={isoValue} id={inputId} placeholder={placeholder} autoComplete={autocomplete} disabled={disabled} aria-invalid={hasError} aria-errormessage={`${id}-error`} aria-labelledby={labelledBy} min={min} max={max} onChange={handleChange} step={step} minLength={minLength} maxLength={maxLength} size={size} readOnly={readOnly} required={required} /> {suffix && (

{suffix} {iconNameRight && } {shouldShowSearchIcon && }

)} {!suffix && iconNameRight && } {!suffix && shouldShowSearchIcon && }
) }, ) PktTextinput.displayName = 'PktTextinput'