ActionSheetDialogView.ets 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2024 桃花镇童长老 @pura/harmony-dialog
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. *
  15. * https://ohpm.openharmony.cn/#/cn/detail/@pura%2Fharmony-dialog
  16. */
  17. import { DialogHelper } from '../dialog/DialogHelper';
  18. import { BottomSheetOptions } from '../model/BottomSheetOptions';
  19. import { SheetOptions } from '../model/SheetOptions';
  20. import { Utils } from '../utils/Utils';
  21. /**
  22. * TODO ActionSheetDialog组件
  23. * author: 桃花镇童长老ᥫ᭡
  24. * since: 2024/08/01
  25. * 仓库主页:https://ohpm.openharmony.cn/#/cn/detail/@pura%2Fharmony-dialog
  26. * github: https://github.com/787107497
  27. * gitee: https://gitee.com/tongyuyan/harmony-utils
  28. * QQ交流群: 569512366
  29. */
  30. @Component
  31. export default struct ActionSheetDialogView {
  32. @Prop options: BottomSheetOptions;
  33. @State title: string = ""; //标题
  34. @State fontSize: number | string | Resource = 16; //按钮的文字大小,字体大小16。
  35. aboutToAppear(): void {
  36. if (this.options.title) {
  37. this.title = Utils.getResourceStr(this.options.title) ?? "";
  38. }
  39. if (this.options.sheets && this.options.sheets.length >= 1) {
  40. let fontSize = (this.options.sheets[0] as SheetOptions).fontSize;
  41. if (fontSize) {
  42. this.fontSize = fontSize;
  43. }
  44. }
  45. }
  46. build() {
  47. Column({ space: 0 }) {
  48. Column() {
  49. Column() {
  50. Text(this.options.title)
  51. .fontSize(14)
  52. .fontColor(this.options.titleFontColor ?? $r('app.color.color_99'))
  53. .align(Alignment.Center)
  54. .textAlign(TextAlign.Center)
  55. .width('100%')
  56. .padding({ left: 10, right: 10, bottom: 15, top: 15 })
  57. Divider()
  58. .width('100%')
  59. .strokeWidth(1)
  60. .color($r('app.color.color_line'))
  61. }
  62. .width('100%')
  63. .visibility(this.title.length > 0 ? Visibility.Visible : Visibility.None)
  64. List() {
  65. ForEach(this.options.sheets, (sheet: SheetOptions, index: number) => {
  66. this.TextBuilder(sheet.value, sheet.fontColor ?? $r('app.color.color_33'), sheet.fontSize ?? 16, index)
  67. }, (sheet: SheetOptions, index: number) => `${index}-${sheet.value}`)
  68. }
  69. .divider({ strokeWidth: 1, color: $r('app.color.color_line') })
  70. .width('100%')
  71. }
  72. .justifyContent(FlexAlign.Center)
  73. .alignItems(HorizontalAlign.Center)
  74. .backgroundColor(this.options.backgroundColor)
  75. .borderRadius(this.options.cornerRadius)
  76. .shadow(ShadowStyle.OUTER_DEFAULT_SM)
  77. .width('100%')
  78. .margin({ bottom: 10 })
  79. Button({ type: ButtonType.Capsule, stateEffect: true }) {
  80. Text(this.options.cancelValue)
  81. .fontSize(this.fontSize)
  82. .fontColor($r('app.color.color_cancel'))
  83. .fontWeight(FontWeight.Bold)
  84. .align(Alignment.Center)
  85. .textAlign(TextAlign.Center)
  86. .width('100%')
  87. .padding({ top: 20, bottom: 20, left: 10, right: 10 })
  88. .backgroundColor(this.options.backgroundColor)
  89. .borderRadius(this.options.cornerRadius)
  90. .shadow(ShadowStyle.OUTER_DEFAULT_SM)
  91. }
  92. .backgroundColor(Color.Transparent)
  93. .clickEffect({level:ClickEffectLevel.HEAVY})
  94. .width('100%')
  95. .onClick(()=>{
  96. DialogHelper.closeDialog(this.options.dialogId ?? "");
  97. })
  98. }
  99. .justifyContent(FlexAlign.Center)
  100. .alignItems(HorizontalAlign.Center)
  101. .width(this.options.width)
  102. .height(this.options.height)
  103. .constraintSize({ maxWidth: this.options.maxWidth })
  104. .margin({ bottom: 30 })
  105. }
  106. @Builder
  107. TextBuilder(value: ResourceStr, fontColor: ResourceColor, fontSize: number | string | Resource, index: number) {
  108. Button({ type: ButtonType.Capsule, stateEffect: true }) {
  109. Text(value)
  110. .fontSize(fontSize)
  111. .fontColor(fontColor)
  112. .align(Alignment.Center)
  113. .textAlign(TextAlign.Center)
  114. .width('100%')
  115. .padding({ top: 20, bottom: 20, left: 10, right: 10 })
  116. }
  117. .backgroundColor(Color.Transparent)
  118. .clickEffect({level:ClickEffectLevel.HEAVY})
  119. .onClick(() => {
  120. if (this.options.actionCancel){
  121. DialogHelper.closeDialog(this.options.dialogId ?? "");
  122. }
  123. if (this.options.onAction) {
  124. this.options.onAction(index, this.options.dialogId ?? "", "");
  125. }
  126. })
  127. }
  128. }