dk-number-input.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. focus:{
  166. type: Boolean,
  167. value: false
  168. },
  169. dataIndex: {
  170. type: Number,
  171. },
  172. dataIndex2: {
  173. type: Number,
  174. },
  175. left: {
  176. type: String,
  177. value: '1rpx'
  178. },
  179. /**
  180. * 错误提示信息
  181. */
  182. errorMessage: {
  183. type: String,
  184. },
  185. /**
  186. * 提示信息
  187. */
  188. tip: {
  189. type: String,
  190. }
  191. },
  192. data: {
  193. // focus: false,
  194. displayValue: '',
  195. clearFlag: false,
  196. },
  197. /**
  198. * 组件的方法列表
  199. */
  200. methods: {
  201. /**
  202. * @desc : 显示tip
  203. * @author : 周兴
  204. * @date : 2024/4/25 11:46
  205. */
  206. showTip() {
  207. let tip = this.data.tip
  208. if (tip) {
  209. wx.showToast({
  210. title: tip,
  211. icon: 'none',
  212. duration: 2000
  213. })
  214. }
  215. },
  216. /**
  217. * @desc : 光标进入选中
  218. * @author : 周兴
  219. * @date : 2022/6/30 16:16
  220. */
  221. handleInput(e) {
  222. this.triggerEvent('triggerOpen', {
  223. })
  224. if (this.data.readonly) {
  225. return
  226. }
  227. setTimeout(() => {
  228. this.setData({
  229. inputValue: parseFloat(this.data.inputValue),
  230. displayValue: this.data.inputValue,
  231. focus: true
  232. })
  233. }, 30)
  234. },
  235. /**
  236. * @desc : 光标进入输入框
  237. * @author : 周兴
  238. * @date : 2022/6/30 16:16
  239. */
  240. bindFocus(e) {
  241. if (isNaN(this.data.inputValue)) {
  242. this.setData({
  243. inputValue: 0
  244. })
  245. }
  246. },
  247. /**
  248. * @desc : 输入时数据的处理
  249. * @author : 周兴
  250. * @date : 2022/6/30 16:16
  251. */
  252. bindInput(e) {
  253. let inputValue = this.data.inputValue
  254. let minFlag = this.data.minFlag
  255. let min = this.data.min
  256. // 如果输入后让数据变成非数字,那么就要还原成之前的数据
  257. if (isNaN(e.detail)) {
  258. // 如果最小值在0之上 ,就不允许输入负数
  259. if (minFlag && min >= 0 && e.detail == '-' || e.detail != '-') {
  260. this.setData({
  261. inputValue: inputValue
  262. })
  263. return;
  264. }
  265. }
  266. let flag = true;
  267. let values = e.detail.split('.')
  268. // 如果输入的是小数点,那么需要判断是否已经有小数点
  269. if (e.detail.endsWith('.')) {
  270. // 说明有多个小数点
  271. if (values && values.length > 2) {
  272. flag = false;
  273. if ((this.data.inputValue + '').indexOf('.') >= 0) {
  274. return this.data.inputValue;
  275. } else {
  276. // 可能连续输入2个小数点
  277. return this.data.inputValue + '.';
  278. }
  279. }
  280. // 如果inputValue是空,那么当成0处理
  281. if (e.detail == '.') {
  282. return '0.';
  283. }
  284. }
  285. //控制小数点后数字的输入
  286. let digits = this.data.digits
  287. if (values && values.length === 2 && values[1].length > digits) {
  288. flag = false;
  289. this.setData({
  290. inputValue: inputValue
  291. })
  292. return this.data.inputValue;
  293. }
  294. if (flag) {
  295. let value = 0;
  296. let detailValue = e.detail
  297. //影响输入小数点 有问题!!!
  298. // 直接变为负
  299. // if (this.data.negative) {
  300. // detailValue = -1 * Math.abs(detailValue)
  301. // }
  302. // 判断是否超过最大值
  303. if (this.data.maxFlag) {
  304. if (detailValue > this.data.max) {
  305. value = this.data.max
  306. } else {
  307. value = detailValue
  308. }
  309. }
  310. else if (this.data.minFlag) {
  311. if (detailValue <= this.data.min) {
  312. value = this.data.min
  313. } else {
  314. value = detailValue
  315. }
  316. } else {
  317. value = detailValue
  318. }
  319. if (e.detail == '-') {
  320. value = e.detail
  321. }
  322. this.setData({
  323. clearFlag: false,
  324. inputValue: value,
  325. })
  326. if (e.detail != '-') {
  327. this.handleData()
  328. }
  329. }
  330. },
  331. /**
  332. * @desc : 光标离开处理数据的显示
  333. * @author : 周兴
  334. * @date : 2022/6/30 16:16
  335. */
  336. bindBlur() {
  337. if (this.data.clearFlag) {
  338. this.setData({
  339. inputValue: 0,
  340. displayValue: 0,
  341. clearFlag: false
  342. })
  343. }
  344. // 处理数据
  345. if (this.data.inputValue) {
  346. this.setData({
  347. inputValue: Number(this.data.inputValue)
  348. })
  349. }
  350. // 离开的时候进行数据处理
  351. // this.handleDisplayValue();
  352. this.triggerEvent('triggerBindBlur', {
  353. inputValue: this.data.inputValue,
  354. index: this.data.dataIndex,
  355. index2: this.data.dataIndex2,
  356. })
  357. this.setData({
  358. focus: false
  359. })
  360. },
  361. /**
  362. * @desc : 绑定值
  363. * @author : 周兴
  364. * @date : 2022/6/30 16:16
  365. */
  366. handleData() {
  367. let value = this.data.inputValue;
  368. if (value == null || value == '') {
  369. this.setData({
  370. clearFlag: true
  371. })
  372. value = 0
  373. }
  374. if (isNaN(value)) {
  375. value = 0
  376. }
  377. // 如果负数标识开启
  378. if (this.data.negative) {
  379. if (value > 0) {
  380. value = -1 * value;
  381. }
  382. } else {
  383. // if (value < 0) {
  384. // value = -1 * value;
  385. // }
  386. }
  387. this.triggerEvent('triggerBindValue', {
  388. value: value
  389. })
  390. },
  391. /**
  392. * @desc : 处理数据的显示
  393. * @author : 周兴
  394. * @date : 2022/6/30 16:16
  395. */
  396. handleDisplayValue() {
  397. let inputValue = this.data.inputValue;
  398. let displayValue = ''
  399. // if(inputValue != undefined && inputValue != 0){
  400. //添加判断条件,当用户输入空数据问题会出现NaN 于继渤2022/06/30
  401. if (inputValue != undefined && inputValue && inputValue != 0) {
  402. // 判断是否有千分位
  403. if (this.data.formatThousandth) {
  404. displayValue = common.toThousandCents(inputValue);
  405. }
  406. // 如果是负数显示
  407. if (this.data.negative && displayValue.indexOf("-") < 0) {
  408. // displayValue = this.data.sign + "-" + displayValue;
  409. displayValue = "-" + displayValue;
  410. } else {
  411. // 显示金额符号
  412. // displayValue = this.data.sign + displayValue;
  413. }
  414. } else {
  415. displayValue = inputValue;
  416. }
  417. this.setData({
  418. displayValue: displayValue
  419. })
  420. },
  421. },
  422. /**
  423. * 组件生命周期
  424. */
  425. lifetimes: {
  426. attached: function () {
  427. //处理数据的显示
  428. this.handleDisplayValue();
  429. },
  430. detached: function () {
  431. // 在组件实例被从页面节点树移除时执行
  432. },
  433. },
  434. })