dk-number-input.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*******************************************************************************
  2. * Copyright(c) 2022 dongke All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:数字组件(支持千分位,金额符)
  5. * 2.功能描述:dk-number-input组件
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 周兴 2022-6-30 1.00 新建
  9. *******************************************************************************/
  10. const common = require('../../../utils/common.js')
  11. Component({
  12. options: {
  13. addGlobalClass: true,
  14. },
  15. properties: {
  16. titleValue: {
  17. type: String,
  18. value: ''
  19. },
  20. required: Boolean,//是否显示表单必填星号
  21. requiredLeft: {
  22. type: String,
  23. value: '50rpx'
  24. },
  25. titleWidth: {
  26. type: String,
  27. value: '164rpx'
  28. },
  29. height:{
  30. type:String,
  31. value:'46rpx'
  32. },
  33. titleColor: {
  34. type: String,
  35. value: '#1B365D'
  36. },
  37. titleFontWeight: {
  38. type: String,
  39. value: 'normal'
  40. },
  41. inputColor: {
  42. type: String,
  43. value: '#1B365D'
  44. },
  45. readonly: {
  46. type: Boolean,
  47. value: false
  48. },
  49. // 内容是否居右,默认是false
  50. contentRight: {
  51. type: Boolean,
  52. value: false
  53. },
  54. /**
  55. * 输入的数据
  56. */
  57. inputValue: {
  58. type: Float32Array,
  59. value: 0,
  60. },
  61. /**
  62. * 是否支持千分位
  63. */
  64. formatThousandth: {
  65. type: Boolean,
  66. value: true
  67. },
  68. /**
  69. * 是否可用
  70. */
  71. disabled: {
  72. type: Boolean,
  73. value: false
  74. },
  75. /**
  76. * 金额符
  77. */
  78. sign: {
  79. type: String,
  80. value: '¥'
  81. },
  82. percent: {
  83. type: String,
  84. value: '%'
  85. },
  86. percentSignFlag: {
  87. type: Boolean,
  88. value: false
  89. },
  90. /**
  91. * 标识的大小
  92. */
  93. signSize: {
  94. type: String,
  95. value: ''
  96. },
  97. /**
  98. * 是否居中
  99. */
  100. center: {
  101. type: String,
  102. value: 'center'
  103. },
  104. /**
  105. * 小数位数
  106. */
  107. digits: {
  108. type: Number,
  109. value: 2
  110. },
  111. /**
  112. * 最大值
  113. */
  114. max: {
  115. type: Number,
  116. value: 10000000
  117. },
  118. /**
  119. * 最小值
  120. */
  121. min: {
  122. type: Number,
  123. value: 0
  124. },
  125. /**
  126. * 字体大小
  127. */
  128. fontSize: {
  129. type: Number,
  130. value: 13
  131. },
  132. inputWidthFlag: {
  133. type: Boolean,
  134. value: false
  135. },
  136. /**
  137. * 字体大小
  138. */
  139. fontWeight: {
  140. type: String,
  141. value: 'nomal'
  142. },
  143. /**
  144. * 是否是负数处理(输入整数,自动变为负数)
  145. */
  146. negative: {
  147. type: Boolean,
  148. value: false
  149. },
  150. /**
  151. * 提示信息
  152. */
  153. placeholder: {
  154. type: String,
  155. value: ''
  156. },
  157. maxFlag: {
  158. type: Boolean,
  159. value: true
  160. },
  161. minFlag: {
  162. type: Boolean,
  163. value: true
  164. },
  165. dataIndex: {
  166. type: Number,
  167. },
  168. dataIndex2: {
  169. type: Number,
  170. },
  171. left: {
  172. type: String,
  173. value: '1rpx'
  174. },
  175. /**
  176. * 错误提示信息
  177. */
  178. errorMessage: {
  179. type: String,
  180. },
  181. /**
  182. * 提示信息
  183. */
  184. tip: {
  185. type: String,
  186. }
  187. },
  188. data: {
  189. focus: false,
  190. displayValue: '',
  191. clearFlag: false,
  192. },
  193. /**
  194. * 组件的方法列表
  195. */
  196. methods: {
  197. /**
  198. * @desc : 显示tip
  199. * @author : 周兴
  200. * @date : 2024/4/25 11:46
  201. */
  202. showTip() {
  203. let tip = this.data.tip
  204. if (tip) {
  205. wx.showToast({
  206. title: tip,
  207. icon: 'none',
  208. duration: 2000
  209. })
  210. }
  211. },
  212. /**
  213. * @desc : 光标进入选中
  214. * @author : 周兴
  215. * @date : 2022/6/30 16:16
  216. */
  217. handleInput(e) {
  218. this.triggerEvent('triggerOpen', {
  219. })
  220. if (this.data.readonly) {
  221. return
  222. }
  223. setTimeout(() => {
  224. this.setData({
  225. inputValue: parseFloat(this.data.inputValue),
  226. displayValue: this.data.inputValue,
  227. focus: true
  228. })
  229. }, 30)
  230. },
  231. /**
  232. * @desc : 光标进入输入框
  233. * @author : 周兴
  234. * @date : 2022/6/30 16:16
  235. */
  236. bindFocus(e) {
  237. if (isNaN(this.data.inputValue)) {
  238. this.setData({
  239. inputValue: 0
  240. })
  241. }
  242. },
  243. /**
  244. * @desc : 输入时数据的处理
  245. * @author : 周兴
  246. * @date : 2022/6/30 16:16
  247. */
  248. bindInput(e) {
  249. let inputValue = this.data.inputValue
  250. let minFlag = this.data.minFlag
  251. let min = this.data.min
  252. // 如果输入后让数据变成非数字,那么就要还原成之前的数据
  253. if (isNaN(e.detail)) {
  254. // 如果最小值在0之上 ,就不允许输入负数
  255. if (minFlag && min >= 0 && e.detail == '-' || e.detail != '-') {
  256. this.setData({
  257. inputValue: inputValue
  258. })
  259. return;
  260. }
  261. }
  262. let flag = true;
  263. let values = e.detail.split('.')
  264. // 如果输入的是小数点,那么需要判断是否已经有小数点
  265. if (e.detail.endsWith('.')) {
  266. // 说明有多个小数点
  267. if (values && values.length > 2) {
  268. flag = false;
  269. if ((this.data.inputValue + '').indexOf('.') >= 0) {
  270. return this.data.inputValue;
  271. } else {
  272. // 可能连续输入2个小数点
  273. return this.data.inputValue + '.';
  274. }
  275. }
  276. // 如果inputValue是空,那么当成0处理
  277. if (e.detail == '.') {
  278. return '0.';
  279. }
  280. }
  281. //控制小数点后数字的输入
  282. let digits = this.data.digits
  283. if (values && values.length === 2 && values[1].length > digits) {
  284. flag = false;
  285. this.setData({
  286. inputValue: inputValue
  287. })
  288. return this.data.inputValue;
  289. }
  290. if (flag) {
  291. let value = 0;
  292. let detailValue = e.detail
  293. //影响输入小数点 有问题!!!
  294. // 直接变为负
  295. // if (this.data.negative) {
  296. // detailValue = -1 * Math.abs(detailValue)
  297. // }
  298. // 判断是否超过最大值
  299. if (this.data.maxFlag) {
  300. if (detailValue > this.data.max) {
  301. value = this.data.max
  302. } else {
  303. value = detailValue
  304. }
  305. }
  306. else if (this.data.minFlag) {
  307. if (detailValue <= this.data.min) {
  308. value = this.data.min
  309. } else {
  310. value = detailValue
  311. }
  312. } else {
  313. value = detailValue
  314. }
  315. if (e.detail == '-') {
  316. value = e.detail
  317. }
  318. this.setData({
  319. clearFlag: false,
  320. inputValue: value,
  321. })
  322. if (e.detail != '-') {
  323. this.handleData()
  324. }
  325. }
  326. },
  327. /**
  328. * @desc : 光标离开处理数据的显示
  329. * @author : 周兴
  330. * @date : 2022/6/30 16:16
  331. */
  332. bindBlur() {
  333. if (this.data.clearFlag) {
  334. this.setData({
  335. inputValue: 0,
  336. displayValue: 0,
  337. clearFlag: false
  338. })
  339. }
  340. // 处理数据
  341. if (this.data.inputValue) {
  342. this.setData({
  343. inputValue: Number(this.data.inputValue)
  344. })
  345. }
  346. // 离开的时候进行数据处理
  347. // this.handleDisplayValue();
  348. this.triggerEvent('triggerBindBlur', {
  349. inputValue: this.data.inputValue,
  350. index: this.data.dataIndex,
  351. index2: this.data.dataIndex2,
  352. })
  353. this.setData({
  354. focus: false
  355. })
  356. },
  357. /**
  358. * @desc : 绑定值
  359. * @author : 周兴
  360. * @date : 2022/6/30 16:16
  361. */
  362. handleData() {
  363. let value = this.data.inputValue;
  364. if (value == null || value == '') {
  365. this.setData({
  366. clearFlag: true
  367. })
  368. value = 0
  369. }
  370. if (isNaN(value)) {
  371. value = 0
  372. }
  373. // 如果负数标识开启
  374. if (this.data.negative) {
  375. if (value > 0) {
  376. value = -1 * value;
  377. }
  378. } else {
  379. // if (value < 0) {
  380. // value = -1 * value;
  381. // }
  382. }
  383. this.triggerEvent('triggerBindValue', {
  384. value: value
  385. })
  386. },
  387. /**
  388. * @desc : 处理数据的显示
  389. * @author : 周兴
  390. * @date : 2022/6/30 16:16
  391. */
  392. handleDisplayValue() {
  393. let inputValue = this.data.inputValue;
  394. let displayValue = ''
  395. // if(inputValue != undefined && inputValue != 0){
  396. //添加判断条件,当用户输入空数据问题会出现NaN 于继渤2022/06/30
  397. if (inputValue != undefined && inputValue && inputValue != 0) {
  398. // 判断是否有千分位
  399. if (this.data.formatThousandth) {
  400. displayValue = common.toThousandCents(inputValue);
  401. }
  402. // 如果是负数显示
  403. if (this.data.negative && displayValue.indexOf("-") < 0) {
  404. // displayValue = this.data.sign + "-" + displayValue;
  405. displayValue = "-" + displayValue;
  406. } else {
  407. // 显示金额符号
  408. // displayValue = this.data.sign + displayValue;
  409. }
  410. } else {
  411. displayValue = inputValue;
  412. }
  413. this.setData({
  414. displayValue: displayValue
  415. })
  416. },
  417. },
  418. /**
  419. * 组件生命周期
  420. */
  421. lifetimes: {
  422. attached: function () {
  423. //处理数据的显示
  424. this.handleDisplayValue();
  425. },
  426. detached: function () {
  427. // 在组件实例被从页面节点树移除时执行
  428. },
  429. },
  430. })