| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {TextAreaProps} from 'isl-components/TextArea'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import * as stylex from '@stylexjs/stylex'; |
| b69ab31 | | | 11 | import {TextArea} from 'isl-components/TextArea'; |
| b69ab31 | | | 12 | import {forwardRef, useEffect, type ForwardedRef} from 'react'; |
| b69ab31 | | | 13 | import {notEmpty} from 'shared/utils'; |
| b69ab31 | | | 14 | import {assert} from '../utils'; |
| b69ab31 | | | 15 | |
| b69ab31 | | | 16 | const styles = stylex.create({ |
| b69ab31 | | | 17 | minHeight: { |
| b69ab31 | | | 18 | overflow: 'hidden', |
| b69ab31 | | | 19 | minHeight: '26px', |
| b69ab31 | | | 20 | }, |
| b69ab31 | | | 21 | }); |
| b69ab31 | | | 22 | |
| b69ab31 | | | 23 | /** |
| b69ab31 | | | 24 | * Wrap `TextArea` to auto-resize to minimum height and optionally disallow newlines. |
| b69ab31 | | | 25 | * Like a `TextField` that has text wrap inside. |
| b69ab31 | | | 26 | */ |
| b69ab31 | | | 27 | export const MinHeightTextField = forwardRef( |
| b69ab31 | | | 28 | ( |
| b69ab31 | | | 29 | props: TextAreaProps & { |
| b69ab31 | | | 30 | onInput: (event: {currentTarget: HTMLTextAreaElement}) => unknown; |
| b69ab31 | | | 31 | keepNewlines?: boolean; |
| b69ab31 | | | 32 | xstyle?: stylex.StyleXStyles; |
| b69ab31 | | | 33 | containerXstyle?: stylex.StyleXStyles; |
| b69ab31 | | | 34 | }, |
| b69ab31 | | | 35 | ref: ForwardedRef<HTMLTextAreaElement>, |
| b69ab31 | | | 36 | ) => { |
| b69ab31 | | | 37 | const {onInput, keepNewlines, xstyle, ...rest} = props; |
| b69ab31 | | | 38 | |
| b69ab31 | | | 39 | // ref could also be a callback ref; don't bother supporting that right now. |
| b69ab31 | | | 40 | assert(typeof ref === 'object', 'MinHeightTextArea requires ref object'); |
| b69ab31 | | | 41 | |
| b69ab31 | | | 42 | // whenever the value is changed, recompute & apply the minimum height |
| b69ab31 | | | 43 | useEffect(() => { |
| b69ab31 | | | 44 | const textarea = ref?.current; |
| b69ab31 | | | 45 | if (textarea) { |
| b69ab31 | | | 46 | const resize = () => { |
| b69ab31 | | | 47 | textarea.style.height = ''; |
| b69ab31 | | | 48 | const scrollheight = textarea.scrollHeight; |
| b69ab31 | | | 49 | textarea.style.height = `${scrollheight}px`; |
| b69ab31 | | | 50 | textarea.rows = 1; |
| b69ab31 | | | 51 | }; |
| b69ab31 | | | 52 | resize(); |
| b69ab31 | | | 53 | const obs = new ResizeObserver(resize); |
| b69ab31 | | | 54 | obs.observe(textarea); |
| b69ab31 | | | 55 | return () => obs.unobserve(textarea); |
| b69ab31 | | | 56 | } |
| b69ab31 | | | 57 | }, [props.value, ref]); |
| b69ab31 | | | 58 | |
| b69ab31 | | | 59 | return ( |
| b69ab31 | | | 60 | <TextArea |
| b69ab31 | | | 61 | ref={ref} |
| b69ab31 | | | 62 | {...rest} |
| b69ab31 | | | 63 | xstyle={[styles.minHeight, xstyle].filter(notEmpty)} |
| b69ab31 | | | 64 | onInput={e => { |
| b69ab31 | | | 65 | const newValue = e.currentTarget?.value; |
| b69ab31 | | | 66 | const result = keepNewlines |
| b69ab31 | | | 67 | ? newValue |
| b69ab31 | | | 68 | : // remove newlines so this acts like a textField rather than a textArea |
| b69ab31 | | | 69 | newValue.replace(/(\r|\n)/g, ''); |
| b69ab31 | | | 70 | onInput({ |
| b69ab31 | | | 71 | currentTarget: { |
| b69ab31 | | | 72 | value: result, |
| b69ab31 | | | 73 | } as HTMLTextAreaElement, |
| b69ab31 | | | 74 | }); |
| b69ab31 | | | 75 | }} |
| b69ab31 | | | 76 | /> |
| b69ab31 | | | 77 | ); |
| b69ab31 | | | 78 | }, |
| b69ab31 | | | 79 | ); |