前端转vue
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TextBoxHelper.ts 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { DirectiveBinding, nextTick, ref, watch } from 'vue';
  2. // 定义 PlaceholderAdorner 等装饰器类的模拟,这里简化处理
  3. class PlaceholderAdorner {
  4. constructor(private textBox: HTMLInputElement, private placeholder: string) {}
  5. }
  6. class TextLengthAdorner {
  7. constructor(private textBox: HTMLInputElement) {}
  8. }
  9. class CircleAdorner {
  10. constructor(private textBox: HTMLInputElement) {}
  11. }
  12. // 定义 InputType 枚举
  13. export enum InputType {
  14. String = 0,
  15. Date = 1,
  16. Int = 2,
  17. Decimal = 3,
  18. Email = 4,
  19. IDCard = 5,
  20. }
  21. // 定义 FocusHandledResult 枚举
  22. enum FocusHandledResult {
  23. System,
  24. Self,
  25. }
  26. // 定义 RegexValidationRules 类
  27. class RegexValidationRules {
  28. private _rules: { [key: string]: [string, string] } = {};
  29. private static _instance: RegexValidationRules;
  30. private constructor() {
  31. this.addRule('Date', '^(?:(?:(?:(?:(?:1[6-9]|[2-9]\\d)(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:0229))|(?:(?:(?:1[6-9]|[2-9]\\d)\\d{2})(?:(?:(?:0[13578]|1[02])31)|(?:(?:0[13-9]|1[0-2])(?:29|30))|(?:(?:0[1-9])|(?:1[0-2]))(?:0[1-9]|1\\d|2[0-8]))))$', '日期格式 20120101。');
  32. this.addRule('Decimal', '^[0-9]+\\.{0,1}[0-9]{0,2}$', '请输入数字,格式 000.00。');
  33. this.addRule('Int', '^\\d+$', '请输入整数数字。');
  34. this.addRule('Email', '^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$', '请输入邮件地址类型。');
  35. this.addRule('IDCard', '^\\d{17}[\\d|x|X]$|^\\d{15}$', '请输入有效身份证号。');
  36. this.addRule('String', null, '');
  37. }
  38. static get Instance() {
  39. if (!this._instance) {
  40. this._instance = new RegexValidationRules();
  41. }
  42. return this._instance;
  43. }
  44. addRule(name: string, regex: string | null, errorMsg: string) {
  45. if (this._rules[name]) {
  46. delete this._rules[name];
  47. }
  48. this._rules[name] = [regex, errorMsg];
  49. }
  50. removeRule(name: string) {
  51. if (this._rules[name]) {
  52. delete this._rules[name];
  53. }
  54. }
  55. bindingRule(element: HTMLElement, name: string) {
  56. if (!element || !this._rules[name]) {
  57. return false;
  58. }
  59. // 这里模拟绑定验证规则
  60. return false;
  61. }
  62. }
  63. // 定义 TextBoxHelper 类
  64. export class TextBoxHelper {
  65. // Placeholder 相关
  66. static placeholderOffset = ref<{ x: number; y: number }>({ x: 4, y: 2 });
  67. static placeholder = ref('');
  68. static onPlaceholderChanged(el: HTMLInputElement) {
  69. nextTick(() => {
  70. const updatePlaceholder = () => {
  71. const isShow = el.value.length === 0;
  72. // 这里简化处理装饰器的添加和移除
  73. if (isShow) {
  74. new PlaceholderAdorner(el, this.placeholder.value);
  75. }
  76. };
  77. el.addEventListener('input', updatePlaceholder);
  78. updatePlaceholder();
  79. });
  80. }
  81. // IsMultiLine 相关
  82. static isMultiLine = ref(false);
  83. static onIsMultiLineChanged(el: HTMLInputElement) {
  84. if (this.isMultiLine.value) {
  85. el.setAttribute('wrap', 'soft');
  86. el.setAttribute('rows', '2');
  87. } else {
  88. el.removeAttribute('wrap');
  89. el.setAttribute('rows', '1');
  90. }
  91. }
  92. // 其他功能类似实现,这里省略部分功能的完整实现,仅展示结构
  93. // ...
  94. // 输入类型验证
  95. static inputType = ref(InputType.String);
  96. static onInputTypeChanged(el: HTMLInputElement) {
  97. switch (this.inputType.value) {
  98. case InputType.Date:
  99. RegexValidationRules.Instance.bindingRule(el, 'Date');
  100. break;
  101. case InputType.Decimal:
  102. RegexValidationRules.Instance.bindingRule(el, 'Decimal');
  103. break;
  104. // 其他 case 类似
  105. }
  106. }
  107. }
  108. // 定义自定义指令,模拟附加属性
  109. export const vTextBoxHelper = {
  110. mounted(el: HTMLInputElement, binding: DirectiveBinding) {
  111. const { value } = binding;
  112. if (value.placeholder) {
  113. TextBoxHelper.placeholder.value = value.placeholder;
  114. TextBoxHelper.onPlaceholderChanged(el);
  115. }
  116. if (value.isMultiLine !== undefined) {
  117. TextBoxHelper.isMultiLine.value = value.isMultiLine;
  118. TextBoxHelper.onIsMultiLineChanged(el);
  119. }
  120. if (value.inputType !== undefined) {
  121. TextBoxHelper.inputType.value = value.inputType;
  122. TextBoxHelper.onInputTypeChanged(el);
  123. }
  124. },
  125. };