inputFoundation.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. init() {}
  13. destroy() {}
  14. handleFocus(e: any) {
  15. this.storeCursor();
  16. this._adapter.notifyFocus(e);
  17. }
  18. handleChange(v: string) {
  19. this.storeCursor();
  20. this._adapter.notifyChange(v);
  21. }
  22. handleBlur(e: any) {
  23. this.clearCursor();
  24. this._adapter.notifyBlur(e);
  25. }
  26. storeCursor() {
  27. const inputNode = this.getCache('inputNode');
  28. if (inputNode) {
  29. const { selectionStart: start } = inputNode;
  30. // const beforeStr = typeof value === 'string' ? value.substr(0, start) : null;
  31. // const afterStr = typeof value === 'string' ? value.substr(start, value.length - start + 1) : null;
  32. // console.log(start, beforeStr, afterStr);
  33. this.setCache('cursorIndex', start);
  34. }
  35. }
  36. restoreCursor() {
  37. const inputNode = this.getCache('inputNode');
  38. const cursorIndex = this.getCache('cursorIndex');
  39. if (inputNode && !isNullOrUndefined(cursorIndex)) {
  40. inputNode.selectionStart = cursorIndex;
  41. inputNode.selectionEnd = cursorIndex;
  42. }
  43. }
  44. clearCursor() {
  45. this.setCache('cursorIndex', null);
  46. this.setCache('beforeStr', null);
  47. this.setCache('afterStr', null);
  48. }
  49. }
  50. export default TimePickerFoundation;