InputDropdown.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <template lang="pug">
  2. div
  3. .input-group
  4. .input-group-button(v-if='hasPrefix()')
  5. button(type='button') {{ prefix }}
  6. input.input-formatted(
  7. v-model='formattedValue'
  8. type='text'
  9. :disabled='!editable'
  10. :autofocus='focus'
  11. )
  12. .input-group-button(v-if='hasSuffix()')
  13. button(
  14. type='button'
  15. @click='onShowOptions()'
  16. ref='suffixButton'
  17. ) {{ suffix }}
  18. span.caret(:style="{'display': hasOptions() ? 'inline-block' : 'none'}")
  19. ul.dropdown(
  20. :style="{'display': isShowOptions ? 'block' : 'none'}"
  21. ref='suffixDropdown'
  22. )
  23. li(
  24. v-for='(option, index) in options'
  25. :key='index'
  26. )
  27. a(@click='onClickOption(option)') {{ option }}
  28. </template>
  29. <script>
  30. export default {
  31. props: {
  32. value: {
  33. type: Number,
  34. default: 0
  35. },
  36. format: {
  37. type: String,
  38. default: 'text'
  39. },
  40. editable: {
  41. type: Boolean,
  42. default: true
  43. },
  44. focus: {
  45. type: Boolean,
  46. default: false
  47. },
  48. prefix: {
  49. type: String,
  50. default: ''
  51. },
  52. suffix: {
  53. type: String,
  54. default: ''
  55. },
  56. options: {
  57. type: Array,
  58. default: []
  59. }
  60. },
  61. computed: {
  62. formattedValue: {
  63. get() {
  64. let value = this.value
  65. if (this.format === 'number') {
  66. value = this.formatValue(value)
  67. value = !!this.value ? value : value.replace(/\d/, '')
  68. value = value.length === 0 ? 0 : value
  69. }
  70. return value
  71. },
  72. set(value) {
  73. if (this.format === 'number') {
  74. value = this.unformatValue(value)
  75. }
  76. this.value = value
  77. this.onChangeValue(value)
  78. }
  79. }
  80. },
  81. methods: {
  82. formatValue(value, options) {
  83. value = value.toString()
  84. if (!(options instanceof Object)) {
  85. options = {}
  86. }
  87. options.thousandsSeparator = options.thousandsSeparator || '.'
  88. options.decimalPlaces = options.decimalPlaces >= 0 || options.decimalPlaces <= 2 ? options.decimalPlaces : 2
  89. options.decimalSeparator = options.decimalSeparator || ','
  90. options.decimalZeros = !!options.decimalZeros
  91. if (!!(`${options.thousandsSeparator}${options.decimalSeparator}`).replace(/\.,|,\./g, '')) {
  92. throw new Error('Same thousands and decimal separator is not allowed')
  93. }
  94. value = value.split('.')
  95. value[0] = value[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, `$1${options.thousandsSeparator}`)
  96. if (!!value[1]) {
  97. value[1] = Number.parseFloat(`1.${value[1]}e${options.decimalPlaces}`)
  98. value[1] = Math.round(value[1]).toString().replace(/^1/, '')
  99. }
  100. value = value.join(options.decimalSeparator)
  101. if (!options.decimalZeros) {
  102. value = value.replace(/([\.|,]\d)0$/, '$1')
  103. }
  104. return value
  105. },
  106. unformatValue(value) {
  107. value = value.replace(/[\.|,](\d{0,2}$)/, '?$1').split(/\?/)
  108. value[0] = value[0].replace(/[^0-9]/g, '')
  109. value = Number.parseFloat(value.join('.')) || 0
  110. return value
  111. },
  112. hideOptions() {
  113. this.isShowOptions = false
  114. },
  115. onChangeValue(value) {
  116. this.$emit('onChangeValue', value)
  117. },
  118. hasPrefix() {
  119. return !!this.prefix && this.prefix.length !== 0
  120. },
  121. hasSuffix() {
  122. return !!this.suffix && this.suffix.length !== 0
  123. },
  124. hasOptions() {
  125. return this.options.length !== 0
  126. },
  127. onShowOptions() {
  128. if (!this.hasOptions()) {
  129. return
  130. }
  131. this.isShowOptions = !this.isShowOptions
  132. },
  133. onClickOutside(e) {
  134. let suffixButton = this.$refs.suffixButton
  135. if (!suffixButton) {
  136. return
  137. }
  138. let target = e.target
  139. if (target === suffixButton) {
  140. return
  141. }
  142. let el = this.$refs.suffixDropdown
  143. if (el !== target && !el.contains(target)) {
  144. this.hideOptions()
  145. }
  146. },
  147. onClickOption(selectedOption) {
  148. this.hideOptions()
  149. this.$emit('onClickOption', selectedOption)
  150. }
  151. },
  152. created() {
  153. document.addEventListener('click', this.onClickOutside)
  154. },
  155. destroyed() {
  156. document.removeEventListener('click', this.onClickOutside)
  157. },
  158. data() {
  159. return {
  160. value: '',
  161. isShowOptions: false
  162. }
  163. }
  164. }
  165. </script>
  166. <style lang="sass">
  167. @import '../../assets/variables'
  168. .input-group
  169. position: relative
  170. width: 100%
  171. display: table
  172. border-collapse: separate
  173. .input-formatted
  174. position: relative
  175. width: 100%
  176. height: 35px
  177. float: left
  178. z-index: 2
  179. display: table-cell
  180. margin-bottom: 0
  181. border-radius: 0
  182. font-size: 12pt
  183. padding: 6px 12px
  184. &:focus
  185. z-index: 3
  186. .input-group-button
  187. position: relative
  188. font-size: 0
  189. width: 1%
  190. white-space: nowrap
  191. vertical-align: middle
  192. display: table-cell
  193. &:first-child
  194. & > button
  195. margin-right: -1px
  196. &:last-child
  197. & > button
  198. margin-left: -1px
  199. button
  200. height: 35px
  201. display: inline-block
  202. margin-bottom: 0
  203. text-align: center
  204. vertical-align: middle
  205. white-space: nowrap
  206. border: 1px solid #ccc
  207. border-radius: 0
  208. box-shadow: none
  209. background: $app-main-color
  210. color: #fff
  211. z-index: 2
  212. position: relative
  213. user-select: none
  214. &:focus
  215. outline: 0 !important
  216. border: 1px solid #ccc
  217. box-shadow: none
  218. &:caret
  219. display: inline-block
  220. width: 0
  221. height: 0
  222. margin-left: 0
  223. vertical-align: middle
  224. border-top: 4px dashed
  225. border-right: 4px solid transparent
  226. border-left: 4px solid transparent
  227. .dropdown
  228. position: absolute
  229. top: 100%
  230. float: left
  231. left: auto
  232. right: 0
  233. list-style: none
  234. text-align: left
  235. border: 1px solid #ccc
  236. background-color: #fff
  237. background-clip: padding-box
  238. min-width: 160px
  239. padding: 5px 0
  240. margin: 2px 0 0
  241. z-index: 1000
  242. font-size: 10pt
  243. & > li > a
  244. height: 35px
  245. display: block
  246. padding: 3px 20px
  247. clear: both
  248. font-weight: normal
  249. line-height: 2.1
  250. white-space: nowrap
  251. color: #333
  252. border-bottom: 1px solid #e5e5e5
  253. &:hover, &:focus
  254. text-decoration: none
  255. border-bottom: 2px solid #7c7bad
  256. </style>