TextSelect.ets 7.6 KB

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