inputFoundation.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  2. import isNullOrUndefined from '../utils/isNullOrUndefined';
  3. export interface TimeInputAdapter extends DefaultAdapter{
  4. notifyChange: (e: any) => void;
  5. notifyFocus: (e: any) => void;
  6. notifyBlur: (e: any) => void;
  7. }
  8. class TimePickerFoundation extends BaseFoundation<TimeInputAdapter> {
  9. constructor(adapter: TimeInputAdapter) {
  10. super({ ...adapter });
  11. }
  12. // eslint-disable-next-line @typescript-eslint/no-empty-function
  13. init() {}
  14. // eslint-disable-next-line @typescript-eslint/no-empty-function
  15. destroy() {}
  16. handleFocus(e: any) {
  17. this.storeCursor();
  18. this._adapter.notifyFocus(e);
  19. }
  20. handleChange(v: string) {
  21. this.storeCursor();
  22. this._adapter.notifyChange(v);
  23. }
  24. handleBlur(e: any) {
  25. this.clearCursor();
  26. this._adapter.notifyBlur(e);
  27. }
  28. storeCursor() {
  29. const inputNode = this.getCache('inputNode');
  30. if (inputNode) {
  31. const { selectionStart: start } = inputNode;
  32. // const beforeStr = typeof value === 'string' ? value.substr(0, start) : null;
  33. // const afterStr = typeof value === 'string' ? value.substr(start, value.length - start + 1) : null;
  34. // console.log(start, beforeStr, afterStr);
  35. this.setCache('cursorIndex', start);
  36. }
  37. }
  38. restoreCursor() {
  39. const inputNode = this.getCache('inputNode');
  40. const cursorIndex = this.getCache('cursorIndex');
  41. if (inputNode && !isNullOrUndefined(cursorIndex)) {
  42. inputNode.selectionStart = cursorIndex;
  43. inputNode.selectionEnd = cursorIndex;
  44. }
  45. }
  46. clearCursor() {
  47. this.setCache('cursorIndex', null);
  48. this.setCache('beforeStr', null);
  49. this.setCache('afterStr', null);
  50. }
  51. }
  52. export default TimePickerFoundation;