index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { useEvent, useState } from 'functional-mini/component';
  2. import { mountComponent } from '../../_util/component';
  3. import { useComponentEvent } from '../../_util/hooks/useComponentEvent';
  4. import useLayoutEffect from '../../_util/hooks/useLayoutEffect';
  5. import { hasValue, useMergedState } from '../../_util/hooks/useMergedState';
  6. import { triggerRefEvent } from '../../_util/hooks/useReportRef';
  7. var Textarea = function (props) {
  8. var isControlled = hasValue(props.controlled)
  9. ? !!props.controlled
  10. : hasValue(props.value);
  11. var option = {
  12. value: props.value,
  13. };
  14. if (!isControlled && hasValue(props.value)) {
  15. option = {
  16. defaultValue: props.value,
  17. };
  18. }
  19. var _a = useMergedState(props.defaultValue, option), value = _a[0], updateValue = _a[1];
  20. var _b = useState(false), selfFocus = _b[0], setSelfFocus = _b[1];
  21. var triggerEvent = useComponentEvent(props).triggerEvent;
  22. triggerRefEvent();
  23. useLayoutEffect(function (mount) {
  24. if (!isControlled && !mount) {
  25. updateValue(props.value);
  26. }
  27. }, [props.value]);
  28. useEvent('onChange', function (e) {
  29. var newValue = e.detail.value;
  30. if (!isControlled) {
  31. updateValue(newValue);
  32. }
  33. else {
  34. }
  35. triggerEvent('change', newValue, e);
  36. }, []);
  37. useEvent('onFocus', function (e) {
  38. var newValue = e.detail.value;
  39. setSelfFocus(true);
  40. triggerEvent('focus', newValue, e);
  41. }, []);
  42. useEvent('onBlur', function (e) {
  43. var newValue = e.detail.value;
  44. setSelfFocus(false);
  45. triggerEvent('blur', newValue, e);
  46. }, []);
  47. useEvent('onConfirm', function (e) {
  48. var newValue = e.detail.value;
  49. triggerEvent('confirm', newValue, e);
  50. }, []);
  51. useEvent('onClear', function (e) {
  52. if (!isControlled) {
  53. updateValue('');
  54. }
  55. triggerEvent('change', '', e);
  56. }, []);
  57. useEvent('update', function (e) {
  58. if (isControlled) {
  59. return;
  60. }
  61. updateValue(e);
  62. }, []);
  63. return {
  64. state: {
  65. value: value,
  66. controlled: isControlled,
  67. },
  68. selfFocus: selfFocus,
  69. };
  70. };
  71. mountComponent(Textarea, {
  72. value: null,
  73. defaultValue: null,
  74. placeholder: null,
  75. placeholderClassName: null,
  76. placeholderStyle: null,
  77. autoHeight: null,
  78. showCount: null,
  79. allowClear: null,
  80. controlled: null,
  81. enableNative: false,
  82. inputClassName: null,
  83. disabled: null,
  84. inputStyle: null,
  85. focusStyle: null,
  86. name: null,
  87. confirmType: null,
  88. focus: null,
  89. confirmHold: null,
  90. });