You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 2.6 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. React-Input-Autosize
  2. ====================
  3. A text input for [React](http://facebook.github.io/react/index.html) that resizes itself to the current content.
  4. ## Demo & Examples
  5. Live demo: [jedwatson.github.io/react-input-autosize](http://jedwatson.github.io/react-input-autosize/)
  6. To run the examples locally, run:
  7. ```
  8. npm install
  9. npm start
  10. ```
  11. Then open [localhost:8000](http://localhost:8000) in a browser.
  12. ## Installation
  13. The easiest way to use React-Input-Autosize is to install it from NPM and include it in your own React build process (using Browserify, rollup, webpack, etc).
  14. You can also use the umd build by including `dist/AutosizeInput.js` in your page. If you use this, make sure you have already included a umd React build.
  15. ```
  16. npm install react-input-autosize --save
  17. ```
  18. ## Usage
  19. React-Input-Autosize generates an input field, wrapped in a `<div>` tag so it can detect the size of its value. Otherwise it behaves very similarly to a standard React input.
  20. ```es6
  21. import AutosizeInput from 'react-input-autosize';
  22. <AutosizeInput
  23. name="form-field-name"
  24. value={inputValue}
  25. onChange={function(event) {
  26. // event.target.value contains the new value
  27. }}
  28. />
  29. ```
  30. ## Gotchas
  31. ### Changing the styles at runtime
  32. The styles applied to the input are only copied when the component mounts. Because of this, subsequent changes to the stylesheet may cause size to be detected incorrectly.
  33. To work around this, either re-mount the input (e.g. by providing a different `key` prop) or call the `copyInputStyles()` method after the styles change.
  34. ### CSP and the IE "clear" indicator
  35. The input will automatically inject a stylesheet that hides IE/Edge's "clear" indicator, which otherwise breaks the UI. This has the downside of being incompatible with some CSP policies.
  36. To work around this, you can pass the `injectStyles={false}` prop, but if you do this I *strongly* recommend targeting the `input` element in your own stylesheet with the following rule:
  37. ```css
  38. input::-ms-clear {display: none;}
  39. ```
  40. ### Custom font sizes
  41. If your input uses custom font sizes, you will need to provide the custom size to `AutosizeInput`.
  42. ```es6
  43. <AutosizeInput
  44. name="form-field-name"
  45. value={inputValue}
  46. style={{ fontSize: 36 }}
  47. onChange={function(event) {
  48. // event.target.value contains the new value
  49. }}
  50. />
  51. ```
  52. ### Uncontrolled input
  53. `AutosizeInput` is a [controlled input](https://facebook.github.io/react/docs/forms.html#controlled-components) and depends on the `value` prop to work as intended. It does not support being used as an uncontrolled input.
  54. ## License
  55. Copyright (c) 2018 Jed Watson. [MIT](LICENSE) License.