1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  *
    
  7.  * @flow
    
  8.  */
    
  9. 
    
  10. import * as React from 'react';
    
  11. import styles from './AutoSizeInput.css';
    
  12. 
    
  13. type Props = {
    
  14.   className?: string,
    
  15.   onFocus?: (event: FocusEvent) => void,
    
  16.   placeholder?: string,
    
  17.   testName?: ?string,
    
  18.   value: any,
    
  19.   ...
    
  20. };
    
  21. 
    
  22. export default function AutoSizeInput({
    
  23.   className,
    
  24.   onFocus,
    
  25.   placeholder = '',
    
  26.   testName,
    
  27.   value,
    
  28.   ...rest
    
  29. }: Props): React.Node {
    
  30.   // $FlowFixMe[missing-local-annot]
    
  31.   const onFocusWrapper = event => {
    
  32.     const input = event.target;
    
  33.     if (input !== null) {
    
  34.       input.selectionStart = 0;
    
  35.       input.selectionEnd = value.length;
    
  36.     }
    
  37. 
    
  38.     if (typeof onFocus === 'function') {
    
  39.       onFocus(event);
    
  40.     }
    
  41.   };
    
  42. 
    
  43.   const isEmpty = value === '' || value === '""';
    
  44. 
    
  45.   return (
    
  46.     // $FlowFixMe[cannot-spread-inexact] unsafe rest spread
    
  47.     <input
    
  48.       className={[styles.Input, className].join(' ')}
    
  49.       data-testname={testName}
    
  50.       onFocus={onFocusWrapper}
    
  51.       placeholder={placeholder}
    
  52.       style={{
    
  53.         width: `calc(${isEmpty ? placeholder.length : value.length}ch + 1px)`,
    
  54.       }}
    
  55.       value={isEmpty ? '' : value}
    
  56.       {...rest}
    
  57.     />
    
  58.   );
    
  59. }