textareaFoundation.ts 7.1 KB

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