华西海圻ELN前端工程
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.

257 lines
5.3 KiB

  1. <template>
  2. <el-input
  3. v-model="inputValue"
  4. :maxlength="maxLength"
  5. :disabled="disabled"
  6. :readonly="readonly"
  7. :clearable="clearable"
  8. :show-password="showPassword"
  9. :placeholder="placeholder || ('请输入' + label)"
  10. :class="borderClass"
  11. @blur="onBlur"
  12. @focus="onFocus"
  13. @change="onChange"
  14. @clear="onClear"
  15. @keyup.enter="onEnter"
  16. />
  17. </template>
  18. <script>
  19. export default {
  20. name: 'CustomInput',
  21. props: {
  22. // v-model 值
  23. value: {
  24. type: [String, Number],
  25. default: ''
  26. },
  27. // 标签名称
  28. label: {
  29. type: String,
  30. default: ''
  31. },
  32. // 最大长度
  33. maxLength: {
  34. type: Number,
  35. default: null
  36. },
  37. // 是否禁用
  38. disabled: {
  39. type: Boolean,
  40. default: false
  41. },
  42. // 是否只读
  43. readonly: {
  44. type: Boolean,
  45. default: false
  46. },
  47. // 是否显示清除按钮
  48. clearable: {
  49. type: Boolean,
  50. default: true
  51. },
  52. // 是否显示密码
  53. showPassword: {
  54. type: Boolean,
  55. default: false
  56. },
  57. // 占位符
  58. placeholder: {
  59. type: String,
  60. default: ''
  61. },
  62. // 边框类型
  63. fillType: {
  64. type: String,
  65. default: '',
  66. validator: value => {
  67. return ['', 'actFill', 'green', 'preFill'].includes(value)
  68. }
  69. },
  70. // 输入框尺寸
  71. size: {
  72. type: String,
  73. default: 'default',
  74. validator: value => {
  75. return ['large', 'default', 'small'].includes(value)
  76. }
  77. }
  78. },
  79. data() {
  80. return {
  81. inputValue: this.value
  82. }
  83. },
  84. computed: {
  85. borderClass() {
  86. const typeMap = {
  87. actFill: 'orange-border',
  88. green: 'green-border',
  89. preFill: 'blue-border'
  90. }
  91. return typeMap[this.fillType] || ''
  92. }
  93. },
  94. watch: {
  95. value(newVal) {
  96. this.inputValue = newVal
  97. },
  98. inputValue(newVal) {
  99. this.$emit('input', newVal)
  100. }
  101. },
  102. methods: {
  103. onBlur(event) {
  104. this.$emit('blur', event)
  105. },
  106. onFocus(event) {
  107. this.$emit('focus', event)
  108. },
  109. onChange(value) {
  110. this.$emit('change', value)
  111. },
  112. onClear() {
  113. this.$emit('clear')
  114. },
  115. onEnter(event) {
  116. this.$emit('enter', event)
  117. },
  118. // 公开方法:清除输入
  119. clear() {
  120. this.inputValue = ''
  121. this.$emit('input', '')
  122. this.$emit('clear')
  123. },
  124. // 公开方法:聚焦
  125. focus() {
  126. this.$el.querySelector('input').focus()
  127. },
  128. // 公开方法:失焦
  129. blur() {
  130. this.$el.querySelector('input').blur()
  131. }
  132. },
  133. filters: {
  134. getFillType(type) {
  135. const typeMap = {
  136. actFill: 'orange-border',
  137. green: 'green-border',
  138. preFill: 'blue-border'
  139. }
  140. return typeMap[type] || ''
  141. }
  142. }
  143. }
  144. </script>
  145. <style lang="scss">
  146. // 基础样式增强
  147. :deep(.el-input) {
  148. .el-input__inner {
  149. transition: border-color 0.2s ease, box-shadow 0.2s ease;
  150. &:focus {
  151. box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
  152. }
  153. }
  154. }
  155. // 橙色边框样式
  156. .orange-border {
  157. :deep(.el-input__inner) {
  158. border-color: #f9c588;
  159. &:focus {
  160. border-color: #f9c588;
  161. box-shadow: 0 0 0 2px rgba(249, 197, 136, 0.2);
  162. }
  163. &:hover {
  164. border-color: #f9c588;
  165. }
  166. }
  167. }
  168. // 绿色边框样式
  169. .green-border {
  170. :deep(.el-input__inner) {
  171. border-color: #67c23a;
  172. &:focus {
  173. border-color: #67c23a;
  174. box-shadow: 0 0 0 2px rgba(103, 194, 58, 0.2);
  175. }
  176. &:hover {
  177. border-color: #67c23a;
  178. }
  179. }
  180. }
  181. // 蓝色边框样式
  182. .blue-border {
  183. :deep(.el-input__inner) {
  184. border-color: #409eff;
  185. &:focus {
  186. border-color: #409eff;
  187. box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
  188. }
  189. &:hover {
  190. border-color: #409eff;
  191. }
  192. }
  193. }
  194. // 禁用状态样式
  195. :deep(.el-input.is-disabled) {
  196. .el-input__inner {
  197. background-color: #f5f7fa;
  198. border-color: #e4e7ed;
  199. color: #c0c4cc;
  200. }
  201. }
  202. // 只读状态样式
  203. :deep(.el-input.is-readonly) {
  204. .el-input__inner {
  205. background-color: #fafafa;
  206. cursor: not-allowed;
  207. }
  208. }
  209. // 清除按钮样式
  210. :deep(.el-input__clear) {
  211. color: #909399;
  212. transition: color 0.2s ease;
  213. &:hover {
  214. color: #409eff;
  215. }
  216. }
  217. // 密码显示按钮样式
  218. :deep(.el-input__password) {
  219. color: #909399;
  220. transition: color 0.2s ease;
  221. &:hover {
  222. color: #409eff;
  223. }
  224. }
  225. // 输入框尺寸样式
  226. :deep(.el-input--large) {
  227. .el-input__inner {
  228. height: 40px;
  229. font-size: 16px;
  230. }
  231. }
  232. :deep(.el-input--small) {
  233. .el-input__inner {
  234. height: 32px;
  235. font-size: 13px;
  236. }
  237. }
  238. </style>