myInput.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // myComponents/myInput/myInput.js
  2. Component({
  3. options: {
  4. multipleSlots: true // 在组件定义时的选项中启用多slot支持
  5. },
  6. /**
  7. * 组件的属性列表
  8. */
  9. properties: {
  10. value:{
  11. type:String,
  12. value:''
  13. },
  14. placeholder:{
  15. type:String,
  16. value:'请输入'
  17. },
  18. showEdit:{
  19. type:Boolean,
  20. value:true
  21. }
  22. },
  23. // 监听传入的变量,当传入的值发生变化时,触发方法
  24. observers: {
  25. 'value': function (val) {
  26. // console.log(val);
  27. }
  28. },
  29. /**
  30. * 页面的初始数据
  31. */
  32. data: {
  33. content:'',
  34. show:false,
  35. fileList:[]
  36. },
  37. methods:{
  38. changeInput(e){
  39. const value = e.detail.value;
  40. this.triggerEvent("input", value);
  41. },
  42. inputBlur(e){
  43. const value = e.detail.value;
  44. if(value == this.data.content){
  45. return false
  46. }
  47. this.changeContent(value)
  48. this.triggerEvent("change", value);
  49. },
  50. changeContent(value){
  51. this.setData(
  52. {
  53. content:value
  54. }
  55. )
  56. },
  57. clickInnerIcon(e){
  58. if(this.properties.showEdit){
  59. // this.data.show = !this.data.show
  60. this.setData(
  61. {
  62. show:!this.data.show
  63. }
  64. )
  65. }
  66. console.log(this.properties.showEdit,this.data.show)
  67. this.triggerEvent('clickInnerIcon',1)
  68. },
  69. buttonClick(e){
  70. // console.log(e,this.properties.value)
  71. this.triggerEvent('search',1)
  72. },
  73. //图片上传
  74. beforeUpload(e){
  75. console.log(e.detail)
  76. if(e.detail.errMsg=="chooseMedia:ok"){
  77. var data = [{
  78. status: 'done',
  79. url: e.detail.tempFiles[0].tempFilePath,
  80. }]
  81. this.setData({ fileList:data })
  82. }
  83. },
  84. onChange(e) {
  85. console.log('1', e)
  86. const { file, fileList } = e.detail
  87. if (file.status === 'uploading') {
  88. this.setData({
  89. progress: 0,
  90. })
  91. wx.showLoading()
  92. } else if (file.status === 'done') {
  93. this.setData({
  94. imageUrl: file.url,
  95. })
  96. }
  97. // Controlled state should set fileList
  98. this.setData({ fileList })
  99. },
  100. //预览
  101. onPreview(e) {
  102. console.log('onPreview', e)
  103. const { file, fileList } = e.detail
  104. wx.previewImage({
  105. current: file.url,
  106. urls: fileList.map((n) => n.url),
  107. })
  108. },
  109. },
  110. /**
  111. * 组件的生命周期函数列表
  112. */
  113. lifetimes: {
  114. // 在组件实例进入页面节点树时执行
  115. attached: function () {
  116. // 初始化操作
  117. // console.log('组件初始化');
  118. this.changeContent(this.properties.value)
  119. // ...
  120. },
  121. // 在组件实例被移除出页面节点树时执行
  122. detached: function () {
  123. // 清理工作
  124. console.log('组件销毁');
  125. // ...
  126. },
  127. // ...
  128. },
  129. /**
  130. * 组件所在页面的生命周期函数可以在 pageLifetimes 中定义
  131. *
  132. * 页面生命周期函数:
  133. * show:页面显示
  134. * hide:页面隐藏
  135. * resize:页面尺寸变化
  136. */
  137. pageLifetimes: {
  138. // ...
  139. }
  140. })