fs-field.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <view
  3. class="fs-field"
  4. :class="{
  5. 'fs-field-tighten': tighten
  6. }"
  7. :style="{ 'background-color': bgColor, borderRadius: round ? '80rpx' : '' }"
  8. >
  9. <view
  10. v-if="type === 'textarea'"
  11. class="fs-field-textarea"
  12. :class="{ 'fs-field-padding': !formItemPosition }"
  13. :style="{ height: height }"
  14. >
  15. <slot name="before"></slot>
  16. <textarea
  17. class="fs-textarea"
  18. placeholder-class="fs-ph-class"
  19. :class="{ clearable, 'fs-field-border': border }"
  20. :name="name"
  21. :placeholder="placeholder"
  22. :maxlength="maxlength"
  23. :disabled="disabled"
  24. :value="modelValue"
  25. :auto-height="autoHeight"
  26. :fixed="fixed"
  27. @input="handleInput"
  28. @focus="handleFocus"
  29. @blur="handleBlur"
  30. @confirm="handleConfirm"
  31. />
  32. <fs-icon
  33. class="fs-field-icon fs-field-icon-close"
  34. type="icon-guanbi2fill"
  35. size="20px"
  36. @touchstart="handleClear"
  37. v-if="clearable"
  38. ></fs-icon>
  39. <slot name="after"></slot>
  40. </view>
  41. <view class="fs-field-input" :class="{ 'fs-field-padding': !formItemPosition }" v-else>
  42. <view v-if="slots.before" style="padding-right: 10rpx;"><slot name="before"></slot></view>
  43. <input
  44. class="fs-input"
  45. placeholder-class="fs-ph-class"
  46. :class="{ clearable, 'fs-field-border': border }"
  47. :value="modelValue"
  48. :type="type"
  49. :password="type === 'password'"
  50. :placeholder="placeholder"
  51. :name="name"
  52. :maxlength="maxlength"
  53. :disabled="disabled"
  54. @input="handleInput"
  55. @focus="handleFocus"
  56. @blur="handleBlur"
  57. @confirm="handleConfirm"
  58. />
  59. <fs-icon
  60. class="fs-field-icon fs-field-icon-close"
  61. type="icon-close-circle"
  62. size="20px"
  63. @touchstart="handleClear"
  64. v-if="clearable && modelValue"
  65. ></fs-icon>
  66. <view v-if="slots.after" style="padding-left: 10rpx;"><slot name="after"></slot></view>
  67. </view>
  68. </view>
  69. </template>
  70. <script setup>
  71. import { inject, computed, useSlots } from 'vue'
  72. const props = defineProps({
  73. modelValue: String,
  74. placeholder: String,
  75. name: String,
  76. type: {
  77. type: String,
  78. default: 'text'
  79. },
  80. maxlength: {
  81. type: [Number, String],
  82. default: 140
  83. },
  84. disabled: Boolean,
  85. border: Boolean,
  86. clearable: Boolean,
  87. autoHeight: Boolean,
  88. required: Boolean,
  89. tighten: Boolean,
  90. fixed: Boolean,
  91. round: Boolean,
  92. height: {
  93. type: String,
  94. default: '70rpx'
  95. },
  96. bgColor: {
  97. type: String,
  98. default: '#fff'
  99. }
  100. })
  101. const emits = defineEmits(['update:modelValue', 'focus', 'blur', 'confirm'])
  102. const slots = useSlots()
  103. const formItemPosition = inject('form-item-position', '')
  104. const position = computed(() => props.labelPosition || formItemPosition)
  105. const handleInput = e => {
  106. emits('update:modelValue', e.detail.value)
  107. }
  108. const handleFocus = () => {
  109. emits('focus')
  110. }
  111. const handleBlur = e => {
  112. emits('blur', e.detail.value)
  113. }
  114. const handleConfirm = () => {
  115. emits('confirm')
  116. }
  117. const handleClear = () => {
  118. emits('update:modelValue', '')
  119. }
  120. </script>
  121. <style lang="scss" scoped>
  122. .fs-field {
  123. box-sizing: border-box;
  124. font-size: var(--content-size);
  125. overflow: hidden;
  126. &-input {
  127. display: flex;
  128. height: 70rpx;
  129. align-items: center;
  130. position: relative;
  131. background-color: inherit;
  132. }
  133. &-textarea {
  134. display: flex;
  135. width: 100%;
  136. align-items: center;
  137. position: relative;
  138. }
  139. &-padding {
  140. padding: 20rpx 30rpx;
  141. height: 90rpx;
  142. }
  143. &-border {
  144. border: 2rpx solid var(--border-color) !important;
  145. border-radius: 4rpx !important;
  146. &.fs-input,
  147. &.fs-textarea {
  148. padding: 20rpx;
  149. }
  150. }
  151. &-icon {
  152. position: absolute;
  153. top: 50%;
  154. transform: translateY(-50%);
  155. color: var(--sub);
  156. z-index: 10;
  157. }
  158. &-icon-close {
  159. right: var(--gutter);
  160. }
  161. &-opacity {
  162. background-color: rgba(255, 255, 255, 0.5);
  163. color: #fff;
  164. .fs-ph-class {
  165. color: #fff;
  166. }
  167. }
  168. &-tighten {
  169. padding: 0 var(--tighten-gutter);
  170. }
  171. &-round {
  172. border-radius: 35px;
  173. }
  174. }
  175. .fs-textarea,
  176. .fs-input {
  177. width: 100%;
  178. height: 100%;
  179. flex: 1;
  180. box-sizing: border-box !important;
  181. border-radius: 6rpx;
  182. border: none;
  183. outline: none;
  184. }
  185. .fs-input {
  186. background: transparent;
  187. &.fs-clearable {
  188. padding-right: 70rpx;
  189. }
  190. }
  191. .fs-ph-class {
  192. color: #c0c5ce;
  193. font-size: var(--content-size);
  194. }
  195. </style>