| 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 {ForwardedRef} from 'react'; |
| b69ab31 | | | 9 | import type {ReactProps} from './utils'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | import * as stylex from '@stylexjs/stylex'; |
| b69ab31 | | | 12 | import {forwardRef} from 'react'; |
| b69ab31 | | | 13 | import {textFieldStyles} from './TextField'; |
| b69ab31 | | | 14 | |
| b69ab31 | | | 15 | const styles = stylex.create({ |
| b69ab31 | | | 16 | horizontalGrowContainer: { |
| b69ab31 | | | 17 | display: 'inline-grid', |
| b69ab31 | | | 18 | maxWidth: '600px', |
| b69ab31 | | | 19 | alignItems: 'center', |
| b69ab31 | | | 20 | '::after': { |
| b69ab31 | | | 21 | width: 'auto', |
| b69ab31 | | | 22 | minWidth: '1em', |
| b69ab31 | | | 23 | content: 'attr(data-value)', |
| b69ab31 | | | 24 | visibility: 'hidden', |
| b69ab31 | | | 25 | whiteSpace: 'pre-wrap', |
| b69ab31 | | | 26 | gridArea: '1 / 2', |
| b69ab31 | | | 27 | height: '26px', |
| b69ab31 | | | 28 | padding: '0 9px', |
| b69ab31 | | | 29 | }, |
| b69ab31 | | | 30 | }, |
| b69ab31 | | | 31 | |
| b69ab31 | | | 32 | horizontalGrow: { |
| b69ab31 | | | 33 | width: 'auto', |
| b69ab31 | | | 34 | minWidth: '1em', |
| b69ab31 | | | 35 | gridArea: '1 / 2', |
| b69ab31 | | | 36 | }, |
| b69ab31 | | | 37 | }); |
| b69ab31 | | | 38 | |
| b69ab31 | | | 39 | /** |
| b69ab31 | | | 40 | * Like a normal text field / {@link TextField}, but grows horizontally to fit the text. |
| b69ab31 | | | 41 | */ |
| b69ab31 | | | 42 | export const HorizontallyGrowingTextField = forwardRef( |
| b69ab31 | | | 43 | ( |
| b69ab31 | | | 44 | props: ReactProps<HTMLInputElement> & { |
| b69ab31 | | | 45 | value?: string; |
| b69ab31 | | | 46 | placeholder?: string; |
| b69ab31 | | | 47 | }, |
| b69ab31 | | | 48 | ref: ForwardedRef<HTMLInputElement>, |
| b69ab31 | | | 49 | ) => { |
| b69ab31 | | | 50 | const {onInput, ...otherProps} = props; |
| b69ab31 | | | 51 | |
| b69ab31 | | | 52 | return ( |
| b69ab31 | | | 53 | <div {...stylex.props(styles.horizontalGrowContainer)} data-value={otherProps.value}> |
| b69ab31 | | | 54 | <input |
| b69ab31 | | | 55 | {...stylex.props(textFieldStyles.input, styles.horizontalGrow)} |
| b69ab31 | | | 56 | type="text" |
| b69ab31 | | | 57 | ref={ref} |
| b69ab31 | | | 58 | onInput={e => { |
| b69ab31 | | | 59 | if ((e.currentTarget.parentNode as HTMLDivElement)?.dataset) { |
| b69ab31 | | | 60 | // Use `dataset` + `content: attr(data-value)` to size an ::after element, |
| b69ab31 | | | 61 | // which auto-expands the containing div to fit the text. |
| b69ab31 | | | 62 | (e.currentTarget.parentNode as HTMLDivElement).dataset.value = e.currentTarget.value; |
| b69ab31 | | | 63 | } |
| b69ab31 | | | 64 | onInput?.(e); |
| b69ab31 | | | 65 | }} |
| b69ab31 | | | 66 | {...otherProps} |
| b69ab31 | | | 67 | /> |
| b69ab31 | | | 68 | </div> |
| b69ab31 | | | 69 | ); |
| b69ab31 | | | 70 | }, |
| b69ab31 | | | 71 | ); |