import React, { useEffect, useMemo, useRef, useState } from 'react' import styled, { css } from 'styled-components' import { theme, states, spacingPx, spacing, text, useControlledOrDefault, } from '@planview/pv-utilities' import { measureTextWidth } from './measureText' export type NaturalLanguageInputProps = Omit< React.ComponentPropsWithoutRef<'input'>, 'onChange' | 'value' | 'defaultValue' | 'placeholder' | 'readOnly' > & { /** Placeholder value */ placeholder?: string /** Controlled value of input */ value?: string /** Uncontrolled value of input */ defaultValue?: string /** Callback when value changes */ onChange?: (value: string) => void /** * Should the input render in an error state? This will only alert the user * when they are focused on the field. You should ensure the value that remains * on blur is a valid value and revert to a previous valid value if necessary. */ error?: boolean /** Should the input render as read only? */ readOnly?: boolean } const NaturalInput = styled.input<{ $error?: boolean; readOnly?: boolean }>` box-sizing: border-box; ${text.semibold}; font-size: inherit; border: none; background: none; padding: 0 ${spacingPx.small}; margin: 0; width: min-content; white-space: nowrap; color: ${theme.textPrimary}; &::placeholder { color: ${theme.textPlaceholder}; } &::selection { color: ${theme.textInverse}; background-color: ${theme.backgroundPrimary}; } ${(props) => !props.readOnly && css` border-bottom: dashed 1px ${theme.borderNormal}; &:hover, &:focus { color: ${theme.textPrimary}; border-color: ${theme.borderHover}; } `} &:focus { ${(props) => states.focus({ focusVisible: true, error: props.$error })} } ` /* Extra padding in case measurement isn't exact */ const SAFETY_MARGIN = 4 /** * Input component for natural language forms. [Design Specification](https://design.planview.com/components/forms/natural-language-form/anatomy#interactive-text-input-trigger) * * This component is meant to be used in conjunction with both `NaturalLanguageForm` and `FormattedMessage` from `react-intl`. * * ```ts * import { NaturalLanguageInput } from '@planview/pv-form' * ``` */ export const NaturalLanguageInput = ({ value: valueIncoming, defaultValue, onChange, error, readOnly, ...rest }: NaturalLanguageInputProps) => { const ref = useRef(null) const [fontSize, setFontSize] = useState(16) const [value, setValue] = useControlledOrDefault( valueIncoming, defaultValue ?? '', onChange ) const displayedValue = value || rest.placeholder || '' const width = useMemo( () => measureTextWidth(displayedValue, fontSize, '600') + spacing.small * 2 + SAFETY_MARGIN, [displayedValue, fontSize] ) // We quickly check to make sure we are calculating the font size correctly // and it won't change state unless the value changes, which is why we can ignore // the dependencies array // eslint-disable-next-line react-hooks/exhaustive-deps useEffect(() => { if (ref.current && typeof window !== 'undefined') { const size = parseInt( window.getComputedStyle(ref.current).fontSize, 10 ) if (!isNaN(size) && fontSize !== size) { setFontSize(size) } } }) return ( setValue(e.target.value)} $error={error} readOnly={readOnly} {...rest} style={{ ...rest?.style, width, }} /> ) }