VmInputBase.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using Abp.Localization;
  2. using VberZero;
  3. using VberZero.Tools.StringModel;
  4. namespace VberAdmin.Web.Models.Input;
  5. public abstract class VmInputBase
  6. {
  7. protected static string L(string name)
  8. {
  9. var str = LocalizationHelper.GetSource(VzConsts.LocalizationSourceName).GetString(name);
  10. return str;
  11. }
  12. #region 字段属性
  13. protected VmInputBase(string name, string displayName, VmInputType inputType)
  14. {
  15. Name = name;
  16. DisplayName = displayName;
  17. InputType = inputType;
  18. IsHidden = false;
  19. IsReadOnly = false;
  20. IsDisabled = false;
  21. IsRequired = false;
  22. Other = "";
  23. DefaultClass = "form-control form-control-sm form-control-solid";
  24. DefaultValue = "";
  25. LabelClass = "fs-6 fw-bolder mb-1";
  26. InputClass = "";
  27. }
  28. public string ModalId { get; set; }
  29. public bool UseId { get; set; }
  30. public string Name { get; set; }
  31. public string DisplayName { get; set; }
  32. public VmInputType InputType { get; set; }
  33. public string Style { get; set; }
  34. public string Class { get; set; }
  35. public virtual string DefaultClass { get; set; }
  36. protected string _other;
  37. public virtual string Other
  38. {
  39. get
  40. {
  41. if (MinLength != null) _other += $" minlength={MinLength}";
  42. if (MaxLength != null) _other += $" maxlength={MaxLength}";
  43. if (Min != null) _other += $" min={Min}";
  44. if (Max != null) _other += $" max={Max}";
  45. return _other;
  46. }
  47. set => _other = value;
  48. }
  49. public int? MinLength { get; set; }
  50. public int? MaxLength { get; set; }
  51. public int? Min { get; set; }
  52. public int? Max { get; set; }
  53. public string DefaultValue { get; set; }
  54. private string PlaceholderPrefix =>
  55. InputType == VmInputType.Select ? L("PlaceholderSelectHeader") : L("PlaceholderHeader");
  56. public string PlaceholderStr => Placeholder.Empty() ? $"{PlaceholderPrefix}{DisplayName}..." : Placeholder;
  57. public string Placeholder { get; set; }
  58. public bool IsHidden { get; set; }
  59. public bool IsRequired { get; set; }
  60. public bool IsDisabled { get; set; }
  61. public bool IsReadOnly { get; set; }
  62. public string Help { get; set; }
  63. protected virtual string ClassStr =>
  64. $"{(IsHidden ? "" : DefaultClass)} {Class} {(IsRequired ? "required" : "")} {(DefaultValue.Empty() ? "" : "default-value")} {(IsDisabled ? "disabled" : "")} {(IsReadOnly ? "readonly" : "")}";
  65. public string LabelClass { get; set; }
  66. public string InputClass { get; set; }
  67. public string LayoutClass { get; set; }
  68. public string Special { get; set; }
  69. public virtual string InputBaseString =>
  70. $" class=\"{ClassStr}\" {(UseId ? $"id =\"{Name}\"" : "")} name=\"{Name}\" {(DefaultValue.Empty() ? "" : $"value=\"{DefaultValue}\"")} placeholder=\"{PlaceholderStr}\" {(IsDisabled ? "disabled" : "")} {(IsReadOnly ? "readonly" : "")} {Other} {(Style.Empty() ? "" : $"style=\"{Style}\"")}";
  71. #endregion 字段属性
  72. #region 方法
  73. public virtual VmInputBase WithHide()
  74. {
  75. IsHidden = true;
  76. return this;
  77. }
  78. public virtual VmInputBase WithReadonly()
  79. {
  80. IsReadOnly = true;
  81. return this;
  82. }
  83. public virtual VmInputBase WithDisabled()
  84. {
  85. IsDisabled = true;
  86. return this;
  87. }
  88. public virtual VmInputBase WithRequired()
  89. {
  90. IsRequired = true;
  91. return this;
  92. }
  93. public virtual VmInputBase WithDefaultValue(string value)
  94. {
  95. DefaultValue = value;
  96. return this;
  97. }
  98. public virtual VmInputBase WithPlaceholder(string placeholder)
  99. {
  100. Placeholder = placeholder;
  101. return this;
  102. }
  103. public virtual VmInputBase WithStyle(string style)
  104. {
  105. Style = style;
  106. return this;
  107. }
  108. public virtual VmInputBase WithClass(string @class)
  109. {
  110. Class = @class;
  111. return this;
  112. }
  113. public virtual VmInputBase WithOther(string other)
  114. {
  115. Other += other;
  116. return this;
  117. }
  118. public virtual VmInputBase WithHelp(string help)
  119. {
  120. Help = help;
  121. return this;
  122. }
  123. public virtual VmInputBase WithSpecial(string special)
  124. {
  125. Special = special;
  126. return this;
  127. }
  128. public virtual VmInputBase WithMaxLength(int maxLength)
  129. {
  130. MaxLength = maxLength;
  131. return this;
  132. }
  133. public virtual VmInputBase WithMinLength(int minLength)
  134. {
  135. MinLength = minLength;
  136. return this;
  137. }
  138. public virtual VmInputBase WithHidden()
  139. {
  140. IsHidden = true;
  141. return this;
  142. }
  143. public virtual VmInputBase WithLayoutClass(string layout)
  144. {
  145. LayoutClass = layout;
  146. return this;
  147. }
  148. #endregion 方法
  149. #region 配合查询模态框使用
  150. /// <summary>
  151. /// 查询模态框ID
  152. /// </summary>
  153. public string QueryModalId { get; set; }
  154. public string DivSearchClass { get; set; }
  155. protected string _target;
  156. public string Target =>
  157. string.IsNullOrEmpty(_target) ? DefaultTarget ?? "" :
  158. _target.StartsWith(".") ? _target :
  159. _target.StartsWith("#") ? _target : $"#{_target}";
  160. public string DefaultTarget { get; set; }
  161. public string Clear { get; set; }
  162. /// <summary>
  163. /// 添加搜索按钮
  164. /// </summary>
  165. /// <param name="modalId"></param>
  166. /// <returns></returns>
  167. public virtual VmInputBase WithQueryIcon(string modalId)
  168. {
  169. QueryModalId = modalId;
  170. return this;
  171. }
  172. /// <summary>
  173. /// 添加搜索按钮
  174. /// </summary>
  175. /// <param name="modalId"></param>
  176. /// <param name="target"></param>
  177. /// <param name="clear"></param>
  178. /// <returns></returns>
  179. public virtual VmInputBase WithQueryIcon(string modalId, string target, string clear = "")
  180. {
  181. QueryModalId = modalId;
  182. _target = target;
  183. if (clear.NotEmpty()) WithQueryClear(clear, true);
  184. return this;
  185. }
  186. /// <summary>
  187. /// 添加搜索范围Class
  188. /// </summary>
  189. /// <param name="class"></param>
  190. /// <returns></returns>
  191. public virtual VmInputBase WithQueryClass(string @class)
  192. {
  193. DivSearchClass = @class;
  194. return this;
  195. }
  196. /// <summary>
  197. /// 添加搜索按钮
  198. /// </summary>
  199. /// <param name="modalId"></param>
  200. /// <param name="class"></param>
  201. /// <param name="clear"></param>
  202. /// <returns></returns>
  203. public virtual VmInputBase WithQueryIconWithClass(string modalId, string @class, string clear = "")
  204. {
  205. QueryModalId = modalId;
  206. _target = "." + @class;
  207. DivSearchClass = @class;
  208. if (clear.NotEmpty()) WithQueryClear(clear, true);
  209. return this;
  210. }
  211. /// <summary>
  212. /// 配置需要清空的INPUT name
  213. /// </summary>
  214. /// <param name="clear"></param>
  215. /// <param name="isDefault"></param>
  216. /// <returns></returns>
  217. public virtual VmInputBase WithQueryClear(string clear, bool isDefault = false)
  218. {
  219. Clear = isDefault ? $"{clear}No,{clear}Name" : clear;
  220. return this;
  221. }
  222. #endregion 配合查询模态框使用
  223. }