textareaFoundation.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import BaseFoundation, { DefaultAdapter, noopFunction } from '../base/foundation';
  2. import {
  3. noop,
  4. isFunction,
  5. isNumber,
  6. isString
  7. } from 'lodash-es';
  8. import calculateNodeHeight from './util/calculateNodeHeight';
  9. import getSizingData from './util/getSizingData';
  10. export interface TextAreaDefaultAdpter {
  11. notifyChange: noopFunction;
  12. setValue: noopFunction;
  13. toggleFocusing: noopFunction;
  14. notifyFocus: noopFunction;
  15. notifyBlur: noopFunction;
  16. notifyKeyDown: noopFunction;
  17. notifyEnterPress: noopFunction;
  18. toggleHovering(hoving: boolean): void;
  19. notifyClear(e: any): void;
  20. }
  21. export interface TextAreaAdpter extends Partial<DefaultAdapter>, Partial<TextAreaDefaultAdpter> {
  22. setMinLength(length: number): void;
  23. notifyPressEnter(e: any): void;
  24. getRef(): any;
  25. notifyHeightUpdate(e: any): void;
  26. }
  27. export default class TextAreaFoundation extends BaseFoundation<TextAreaAdpter> {
  28. static get textAreaDefaultAdapter() {
  29. return {
  30. notifyChange: noop,
  31. setValue: noop,
  32. toggleFocusing: noop,
  33. toggleHovering: noop,
  34. notifyFocus: noop,
  35. notifyBlur: noop,
  36. notifyKeyDown: noop,
  37. notifyEnterPress: noop
  38. };
  39. }
  40. constructor(adapter: TextAreaAdpter) {
  41. super({
  42. ...TextAreaFoundation.textAreaDefaultAdapter,
  43. ...adapter
  44. });
  45. }
  46. init() {
  47. this.setInitValue();
  48. }
  49. // eslint-disable-next-line
  50. destroy() { }
  51. setInitValue() {
  52. const {
  53. defaultValue,
  54. value
  55. } = this.getProps();
  56. let v = defaultValue;
  57. if (this._isControlledComponent()) {
  58. v = value;
  59. }
  60. this._adapter.setValue(v);
  61. }
  62. handleValueChange(v: string) {
  63. this._adapter.setValue(v);
  64. }
  65. handleChange(value: string, e: any) {
  66. const { maxLength, minLength, getValueLength } = this._adapter.getProps();
  67. let nextValue = value;
  68. if (maxLength && isFunction(getValueLength)) {
  69. nextValue = this.handleVisibleMaxLength(value);
  70. }
  71. if (minLength && isFunction(getValueLength)) {
  72. this.handleVisibleMinLength(nextValue);
  73. }
  74. if (this._isControlledComponent()) {
  75. this._adapter.notifyChange(nextValue, e);
  76. } else {
  77. this._adapter.setValue(nextValue);
  78. this._adapter.notifyChange(nextValue, e);
  79. }
  80. }
  81. /**
  82. * Modify minLength to trigger browser check for minimum length
  83. * Controlled mode is not checked
  84. * @param {String} value
  85. */
  86. handleVisibleMinLength(value: string) {
  87. const { minLength, getValueLength } = this._adapter.getProps();
  88. const { minLength: stateMinLength } = this._adapter.getStates();
  89. if (isNumber(minLength) && minLength >= 0 && isFunction(getValueLength) && isString(value)) {
  90. const valueLength = getValueLength(value);
  91. if (valueLength < minLength) {
  92. const newMinLength = value.length + (minLength - valueLength);
  93. newMinLength !== stateMinLength && this._adapter.setMinLength(newMinLength);
  94. } else {
  95. stateMinLength !== minLength && this._adapter.setMinLength(minLength);
  96. }
  97. }
  98. }
  99. /**
  100. * Handle input emoji characters beyond maxLength
  101. * Controlled mode is not checked
  102. * @param {String} value
  103. */
  104. handleVisibleMaxLength(value: string) {
  105. const { maxLength, getValueLength } = this._adapter.getProps();
  106. if (isNumber(maxLength) && maxLength >= 0 && isFunction(getValueLength) && isString(value)) {
  107. const valueLength = getValueLength(value);
  108. if (valueLength > maxLength) {
  109. // eslint-disable-next-line max-len
  110. console.warn('[Semi TextArea] The input character is truncated because the input length exceeds the maximum length limit');
  111. const truncatedValue = this.handleTruncateValue(value, maxLength);
  112. return truncatedValue;
  113. } else {
  114. return value;
  115. }
  116. }
  117. return undefined;
  118. }
  119. /**
  120. * Truncate textarea values based on maximum length
  121. * @param {String} value
  122. * @param {Number} maxLength
  123. * @returns {String}
  124. */
  125. handleTruncateValue(value: string, maxLength: number) {
  126. const { getValueLength } = this._adapter.getProps();
  127. if (isFunction(getValueLength)) {
  128. let truncatedValue = '';
  129. for (let i = 1, len = value.length; i <= len; i++) {
  130. const currentValue = value.slice(0, i);
  131. if (getValueLength(currentValue) > maxLength) {
  132. return truncatedValue;
  133. } else {
  134. truncatedValue = currentValue;
  135. }
  136. }
  137. return truncatedValue;
  138. } else {
  139. return value.slice(0, maxLength);
  140. }
  141. }
  142. handleFocus(e: any) {
  143. const { value } = this.getStates();
  144. this._adapter.toggleFocusing(true);
  145. this._adapter.notifyFocus(value, e);
  146. }
  147. handleBlur(e: any) {
  148. const { value } = this.getStates();
  149. this._adapter.toggleFocusing(false);
  150. this._adapter.notifyBlur(value, e);
  151. }
  152. handleKeyDown(e: any) {
  153. this._adapter.notifyKeyDown(e);
  154. if (e.keyCode === 13) {
  155. this._adapter.notifyPressEnter(e);
  156. }
  157. }
  158. resizeTextarea = (cb: any) => {
  159. const { height } = this.getStates();
  160. const { rows } = this.getProps();
  161. const node = this._adapter.getRef().current;
  162. const nodeSizingData = getSizingData(node);
  163. if (!nodeSizingData) {
  164. cb && cb();
  165. return;
  166. }
  167. const newHeight = calculateNodeHeight(
  168. nodeSizingData,
  169. node.value || node.placeholder || 'x',
  170. rows
  171. // maxRows,
  172. );
  173. if (height !== newHeight) {
  174. this._adapter.notifyHeightUpdate(newHeight);
  175. node.style.height = `${newHeight}px`;
  176. return;
  177. }
  178. cb && cb();
  179. };
  180. handleMouseEnter(e) {
  181. this._adapter.toggleHovering(true);
  182. }
  183. handleMouseLeave(e) {
  184. this._adapter.toggleHovering(false);
  185. }
  186. isAllowClear() {
  187. const { value, isFocus, isHover } = this._adapter.getStates();
  188. const { showClear, disabled, readonly } = this._adapter.getProps();
  189. const allowClear = value && showClear && !disabled && (isFocus || isHover) && !readonly;
  190. return allowClear;
  191. }
  192. handleClear(e) {
  193. const { isFocus } = this.getStates();
  194. if (this._isControlledComponent('value')) {
  195. this._adapter.setState({
  196. isFocus: false,
  197. });
  198. } else {
  199. this._adapter.setState({
  200. value: '',
  201. isFocus: false,
  202. });
  203. }
  204. if (isFocus) {
  205. this._adapter.notifyBlur('', e);
  206. }
  207. this._adapter.notifyChange('', e);
  208. this._adapter.notifyClear(e);
  209. this.stopPropagation(e);
  210. }
  211. }