textareaFoundation.ts 7.7 KB

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