TextSelect.ets 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { DropDownImageInfo, DropDownInfo, TextInfo, TextInputInfo } from '../../models/TextSelectModel'
  2. import { BreakPointSize, YTBreakPoint } from '../../utils/YTBreakPoint'
  3. @Component
  4. export struct TextSelect {
  5. public dropDownImageInfo: DropDownImageInfo = {
  6. dropDownImage: $r('app.media.ic_expand_more_black'),
  7. dropDownImageHeight: 30,
  8. dropDownImageWidth: 30,
  9. dropDownImageBackgroundColor: Color.Transparent,
  10. dropDownImageMargin: { right: 4 }
  11. } //右侧下拉图标
  12. public textInfo: TextInfo = {
  13. fontSize: 16,
  14. fontColor: Color.Black
  15. } //显示的文本样式
  16. public dropDownInfo: DropDownInfo = {
  17. backgroundColor: Color.Black,
  18. fontSize: 16,
  19. fontColor: Color.White,
  20. height: 130
  21. } //下拉框样式
  22. @Prop @Require selectOptions: Array<ESObject> = [] //下拉框数据
  23. @Require showName: string = '' //要展示的字段名
  24. defaultOption: ESObject //默认选中项
  25. public moduleHeight?: Length = 40 //组件高度
  26. @State private selectHeight: Length = 0
  27. @State private isShowSelect: boolean = false
  28. @State private selectText: ResourceStr = ''
  29. public onchange = (item: ESObject) => {
  30. }
  31. public textInputSubmit = (text: string) => {
  32. }
  33. textInputInfo: TextInputInfo | null = null //自定义输入框配置,不传则不显示
  34. @StorageProp(YTBreakPoint.BreakPointVp) breakPointSize: BreakPointSize = { width: 0, height: 0 }
  35. @State selectPosition: Position = { x: 0, y: 0 }
  36. aboutToAppear(): void {
  37. if (!this.defaultOption) {
  38. this.defaultOption = this.selectOptions[0]
  39. }
  40. this.selectText = this.defaultOption[this.showName]
  41. }
  42. build() {
  43. Column() {
  44. if (this.isShowSelect) {
  45. Column() {
  46. }
  47. .position(this.selectPosition)
  48. .backgroundColor(Color.Transparent)
  49. .width(this.breakPointSize.width)
  50. .height(this.breakPointSize.height)
  51. .onClick(() => {
  52. this.isShowSelect = false
  53. this.selectHeight = 0
  54. })
  55. }
  56. Column() {
  57. Row() {
  58. Text(this.selectText)
  59. .fontSize(this.textInfo.fontSize)
  60. .padding({ left: 15 })
  61. Image(this.dropDownImageInfo.dropDownImage)
  62. .height(this.dropDownImageInfo.dropDownImageHeight)
  63. .width(this.dropDownImageInfo.dropDownImageWidth)
  64. .margin(this.dropDownImageInfo.dropDownImageMargin)
  65. .backgroundColor(this.dropDownImageInfo.dropDownImageBackgroundColor)
  66. }
  67. .onClick(() => {
  68. this.isShowSelect = !this.isShowSelect
  69. animateTo({ duration: 300 }, () => {
  70. if (this.isShowSelect) {
  71. this.selectHeight = this.dropDownInfo.height
  72. } else {
  73. this.selectHeight = 0
  74. }
  75. })
  76. })
  77. .height('100%')
  78. .width('100%')
  79. .border({ width: 1, color: '#cbcbcb', radius: 5 })
  80. .justifyContent(FlexAlign.SpaceBetween)
  81. if (this.isShowSelect) {
  82. this.selectBuilder()
  83. }
  84. }
  85. .height(this.moduleHeight)
  86. }
  87. .onAreaChange((old, newArea) => {
  88. this.selectPosition = {
  89. x: -(newArea.globalPosition.x as number),
  90. y: -(newArea.globalPosition.y as number)
  91. }
  92. })
  93. .zIndex(200)
  94. }
  95. @Builder
  96. selectBuilder() {
  97. Row() {
  98. List({ space: 10 }) {
  99. ForEach(this.selectOptions, (item: ESObject) => {
  100. ListItem() {
  101. Text(item[this.showName])
  102. .width('100%')
  103. .fontSize(this.dropDownInfo.fontSize)
  104. .fontColor(this.dropDownInfo.fontColor)
  105. .padding({ top: 4, bottom: 4 })
  106. .onClick(() => {
  107. this.selectText = item[this.showName]
  108. this.isShowSelect = false
  109. this.selectHeight = 0
  110. this.onchange(item)
  111. })
  112. .margin({
  113. left: 15,
  114. right: 15,
  115. top: 4,
  116. bottom: 4
  117. })
  118. }
  119. })
  120. if (this.textInputInfo) {
  121. ListItem() {
  122. Row() {
  123. TextInput({ placeholder: this.textInputInfo.placeholder, text: this.textInputInfo.text })
  124. .width('100%')
  125. .height('100%')
  126. .placeholderFont({ size: 14 })
  127. .padding({ left: 10, right: 10 })
  128. .border({ width: 1, color: '#cbcbcb', radius: 5 })
  129. .backgroundColor(Color.Transparent)
  130. .onChange((value: string) => {
  131. this.textInputInfo!.text = value
  132. })
  133. .onSubmit(() => {
  134. this.selectText = this.textInputInfo!.text
  135. this.isShowSelect = false
  136. this.selectHeight = 0
  137. this.textInputSubmit(this.textInputInfo!.text)
  138. this.textInputInfo!.text = ''
  139. })
  140. }
  141. .padding({ left: 10, right: 10 })
  142. .height(this.textInputInfo.height)
  143. .width('100%')
  144. }
  145. }
  146. }
  147. .height('100%')
  148. .scrollBar(BarState.Off)
  149. .listDirection(Axis.Vertical) // 排列方向
  150. // .divider({ strokeWidth: 1, color: 0xCBCBCB }) // 每行之间的分界线
  151. .edgeEffect(EdgeEffect.Spring) // 滑动到边缘无效果
  152. }
  153. .backgroundColor(this.dropDownInfo.backgroundColor)
  154. .border(this.dropDownInfo.border)
  155. .width('100%')
  156. .height(this.selectHeight)
  157. .borderRadius({ bottomLeft: 10, bottomRight: 10 })
  158. }
  159. }