XDialogList.ets 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { XDialogCommon } from './XDialogCommon'
  2. import { XDialogController } from './XDialogController'
  3. @Preview
  4. @Component
  5. export struct XDialogList {
  6. controller: XDialogController | null = null
  7. onSelected: (index: number, value: string) => void = (index: number, value: string) => {
  8. }
  9. onCancel?: () => void = undefined
  10. title?: string = undefined
  11. values?: Array<string> | Array<object> = undefined
  12. autoCancel?: boolean = false
  13. private dialogController: CustomDialogController | null = null
  14. build() {
  15. // this.buildContent()
  16. }
  17. aboutToAppear(): void {
  18. this.dialogController = new CustomDialogController({
  19. builder: XDialogCommon({
  20. dialogContent: () => {
  21. this.buildContent()
  22. },
  23. }),
  24. alignment: DialogAlignment.Center,
  25. autoCancel: this.autoCancel ?? false,
  26. cancel: () => {
  27. this.onCancel && this.onCancel()
  28. }
  29. })
  30. if (this.controller) {
  31. this.controller.open = () => {
  32. this.dialogController?.open()
  33. }
  34. this.controller.close = () => {
  35. this.dialogController?.close()
  36. }
  37. }
  38. }
  39. @Builder
  40. buildContent() {
  41. Column() {
  42. Text(this.title)
  43. .fontSize(13)
  44. .textAlign(TextAlign.Center)
  45. .width('60%')
  46. .maxLines(2)
  47. .ellipsisMode(EllipsisMode.END)
  48. .textOverflow({
  49. overflow: TextOverflow.Ellipsis
  50. })
  51. .visibility(this.title ? Visibility.Visible : Visibility.None)
  52. List({ space: 20, initialIndex: 0 }) {
  53. ForEach(this.values ?? [], (item: string, index: number) => {
  54. ListItem() {
  55. Text(item)
  56. .width('100%')
  57. .fontSize(16)
  58. .textAlign(TextAlign.Center)
  59. .onClick(() => {
  60. this.onSelected(index, item)
  61. this.dialogController?.close()
  62. })
  63. }
  64. }, (item: string) => item)
  65. }
  66. .listDirection(Axis.Vertical) // 排列方向
  67. .scrollBar(BarState.Off)
  68. .friction(0.6)
  69. .divider({ strokeWidth: 1, color: 0xEEEEEE, startMargin: 20, endMargin: 20 }) // 每行之间的分界线
  70. .edgeEffect(EdgeEffect.Spring) // 边缘效果设置为Spring
  71. .width('100%')
  72. .height(this.values === undefined ? '20%' : this.values?.length < 8 ? `${this.values?.length / 16 * 100}%` : '50%')
  73. .margin({ top: 20 })
  74. }.padding({ top: 20, bottom: 20, left: 20, right: 20 })
  75. }
  76. }