textareaFoundation.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. export interface TextAreaDefaultAdapter {
  11. notifyChange: noopFunction;
  12. setValue: noopFunction;
  13. toggleFocusing: noopFunction;
  14. notifyFocus: noopFunction;
  15. notifyBlur: noopFunction;
  16. notifyKeyDown: noopFunction;
  17. notifyEnterPress: noopFunction;
  18. toggleHovering(hovering: boolean): void;
  19. notifyClear(e: any): void
  20. }
  21. export interface TextAreaAdapter extends Partial<DefaultAdapter>, Partial<TextAreaDefaultAdapter> {
  22. setMinLength(length: number): void;
  23. notifyPressEnter(e: any): void;
  24. getRef(): HTMLInputElement;
  25. notifyHeightUpdate(e: any): void
  26. }
  27. export default class TextAreaFoundation extends BaseFoundation<TextAreaAdapter> {
  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: TextAreaAdapter) {
  41. super({
  42. ...TextAreaFoundation.textAreaDefaultAdapter,
  43. ...adapter
  44. });
  45. }
  46. init() {
  47. this.setInitValue();
  48. }
  49. destroy() { }
  50. setInitValue() {
  51. const {
  52. defaultValue,
  53. value
  54. } = this.getProps();
  55. let v = defaultValue;
  56. if (this._isControlledComponent()) {
  57. v = value;
  58. }
  59. this._adapter.setValue(v);
  60. }
  61. handleValueChange(v: string) {
  62. this._adapter.setValue(v);
  63. }
  64. handleChange(value: string, e: any) {
  65. const { maxLength, minLength, getValueLength } = this._adapter.getProps();
  66. let nextValue = value;
  67. if (maxLength && isFunction(getValueLength)) {
  68. nextValue = this.handleVisibleMaxLength(value);
  69. }
  70. if (minLength && isFunction(getValueLength)) {
  71. this.handleVisibleMinLength(nextValue);
  72. }
  73. if (this._isControlledComponent()) {
  74. this._adapter.notifyChange(nextValue, e);
  75. } else {
  76. this._adapter.setValue(nextValue);
  77. this._adapter.notifyChange(nextValue, e);
  78. }
  79. }
  80. /**
  81. * Modify minLength to trigger browser check for minimum length
  82. * Controlled mode is not checked
  83. * @param {String} value
  84. */
  85. handleVisibleMinLength(value: string) {
  86. const { minLength, getValueLength } = this._adapter.getProps();
  87. const { minLength: stateMinLength } = this._adapter.getStates();
  88. if (isNumber(minLength) && minLength >= 0 && isFunction(getValueLength) && isString(value)) {
  89. const valueLength = getValueLength(value);
  90. if (valueLength < minLength) {
  91. const newMinLength = value.length + (minLength - valueLength);
  92. newMinLength !== stateMinLength && this._adapter.setMinLength(newMinLength);
  93. } else {
  94. stateMinLength !== minLength && this._adapter.setMinLength(minLength);
  95. }
  96. }
  97. }
  98. /**
  99. * Handle input emoji characters beyond maxLength
  100. * Controlled mode is not checked
  101. * @param {String} value
  102. */
  103. handleVisibleMaxLength(value: string) {
  104. const { maxLength, getValueLength } = this._adapter.getProps();
  105. if (isNumber(maxLength) && maxLength >= 0 && isFunction(getValueLength) && isString(value)) {
  106. const valueLength = getValueLength(value);
  107. if (valueLength > maxLength) {
  108. console.warn('[Semi TextArea] The input character is truncated because the input length exceeds the maximum length limit');
  109. const truncatedValue = this.handleTruncateValue(value, maxLength);
  110. return truncatedValue;
  111. } else {
  112. return value;
  113. }
  114. }
  115. return undefined;
  116. }
  117. /**
  118. * Truncate textarea values based on maximum length
  119. * @param {String} value
  120. * @param {Number} maxLength
  121. * @returns {String}
  122. */
  123. handleTruncateValue(value: string, maxLength: number) {
  124. const { getValueLength } = this._adapter.getProps();
  125. if (isFunction(getValueLength)) {
  126. let truncatedValue = '';
  127. for (let i = 1, len = value.length; i <= len; i++) {
  128. const currentValue = value.slice(0, i);
  129. if (getValueLength(currentValue) > maxLength) {
  130. return truncatedValue;
  131. } else {
  132. truncatedValue = currentValue;
  133. }
  134. }
  135. return truncatedValue;
  136. } else {
  137. return value.slice(0, maxLength);
  138. }
  139. }
  140. handleFocus(e: any) {
  141. const { value } = this.getStates();
  142. this._adapter.toggleFocusing(true);
  143. this._adapter.notifyFocus(value, e);
  144. }
  145. handleBlur(e: any) {
  146. const { value } = this.getStates();
  147. this._adapter.toggleFocusing(false);
  148. this._adapter.notifyBlur(value, e);
  149. }
  150. handleKeyDown(e: any) {
  151. this._adapter.notifyKeyDown(e);
  152. if (e.keyCode === 13) {
  153. this._adapter.notifyPressEnter(e);
  154. }
  155. }
  156. resizeTextarea = (cb?: any) => {
  157. const { height } = this.getStates();
  158. const { rows } = this.getProps();
  159. const node = this._adapter.getRef();
  160. const nodeSizingData = getSizingData(node);
  161. if (!nodeSizingData) {
  162. cb && cb();
  163. return;
  164. }
  165. const newHeight = calculateNodeHeight(
  166. nodeSizingData,
  167. node.value || node.placeholder || 'x',
  168. rows
  169. // maxRows,
  170. );
  171. if (height !== newHeight) {
  172. this._adapter.notifyHeightUpdate(newHeight);
  173. node.style.height = `${newHeight}px`;
  174. return;
  175. }
  176. cb && cb();
  177. };
  178. // e: MouseEvent
  179. handleMouseEnter(e: any) {
  180. this._adapter.toggleHovering(true);
  181. }
  182. // e: MouseEvent
  183. handleMouseLeave(e: any) {
  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. }