import { useId } from 'react' import * as React from 'react' import { Textarea as UITextarea } from '@planview/pv-uikit' import type { TextareaProps as UITextareaProps } from '@planview/pv-uikit' import Field from '../field' import styled from 'styled-components' import type { CounterProps } from '../utils/limit-counter' import { useCounterLogic } from '../hooks/useCounterLogic' export type TextareaProps = Omit< UITextareaProps, 'error' | 'innerRef' | 'currentValue' | 'onClear' | 'value' | 'warning' > & Pick< React.ComponentPropsWithRef, 'label' | 'error' | 'withAsterisk' | 'labelPosition' | 'warning' > & { /** Value of the field */ value?: string } & CounterProps const StyledTextarea = styled(UITextarea)` textarea { display: block; } ` /** * Implementation combines [Form Field specification](https://design.planview.com/components/forms/form-field) * with [Textarea specification](https://design.planview.com/components/forms/textarea). * * `import { Textarea } from '@planview/pv-form'` */ const Textarea: React.FC = ({ id: incomingId, label, labelPosition, error, warning, withAsterisk, defaultValue, showCounter, maxLength: maxLengthIncoming, limit, value, onChange, ...textareaProps }) => { const internalId = `pv-form-${useId()}` const id = incomingId || internalId const [uncontrolledLength, setUncontrolledLength] = React.useState( defaultValue?.length || 0 ) const changeHandler = React.useCallback( (changedVal: string, e: React.ChangeEvent) => { setUncontrolledLength(changedVal.length) onChange && onChange(changedVal, e) }, [onChange] ) const length = value?.length || uncontrolledLength const { maxLength, count, fieldError, handleBeforeInput, handleBlur, handleFocus, handlePaste, } = useCounterLogic({ length, maxLength: maxLengthIncoming, showCounter, limit, onBeforeInput: textareaProps.nativeProps?.onBeforeInput, onPaste: textareaProps.nativeProps?.onPaste, onFocus: textareaProps.onFocus, onBlur: textareaProps.onBlur, }) return ( {(fieldProps) => ( )} ) } export { Textarea } export default Textarea