dk-number-input.js 9.7 KB

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