SelectBox.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { addBtnClick } from '@scripts/utils/UIHelper';
  2. import { _decorator, Button, Component, EditBox, Label, Node, instantiate } from 'cc';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('SelectBox')
  5. export class SelectBox extends Component {
  6. @property(Label)
  7. label: Label;
  8. @property(Node)
  9. pull_toggle: Node;
  10. @property(Node)
  11. pull_up: Node;
  12. @property(Node)
  13. pull_down: Node;
  14. @property(Node)
  15. item: Node;
  16. @property(Node)
  17. scroll_view: Node;
  18. @property(Node)
  19. content: Node;
  20. private _current_idx: number = -1;
  21. private _data: string[] = [];
  22. protected onLoad(): void {
  23. addBtnClick(this.pull_toggle, this.onBtnToggleClick, this);
  24. }
  25. protected onEnable(): void {
  26. }
  27. protected onDisable(): void {
  28. }
  29. private init() {
  30. this.pull_down.active = true;
  31. this.pull_up.active = false;
  32. this.content.active = false;
  33. this.scroll_view.active = false;
  34. this.content.removeAllChildren();
  35. this.label.string = '';
  36. }
  37. public initSelectItems(data: string[] = [], params: any = {}) {
  38. const selectData = data;
  39. this._data = selectData;
  40. this.init();
  41. for (let i = 0; i < selectData.length; i++) {
  42. const item = instantiate(this.item);
  43. item.active = true;
  44. item.parent = this.content;
  45. item.getChildByName("selected").active = false;
  46. item.getChildByName("label").getComponent(Label).string = selectData[i];
  47. addBtnClick(item, () => { this.handleClickItem(i, selectData[i]) }, this);
  48. }
  49. const { defaultIndex } = params;
  50. this.setDefaultIndex(defaultIndex);
  51. }
  52. public getSelectedStr() {
  53. return this.label.string;
  54. }
  55. private handleClickItem(i: number, data: string) {
  56. this.label.string = data;
  57. this._current_idx = i;
  58. this.onBtnToggleClick();
  59. }
  60. private onBtnToggleClick() {
  61. this.pull_down.active = !this.pull_down.active;
  62. this.pull_up.active = !this.pull_up.active;
  63. this.content.active = !this.content.active;
  64. this.scroll_view.active = this.content.active;
  65. if (this.content.active) {
  66. // 标记selected
  67. this.content.children.forEach((item, index) => {
  68. if (index === this._current_idx) {
  69. item.getChildByName("selected").active = true;
  70. } else {
  71. item.getChildByName("selected").active = false;
  72. }
  73. })
  74. }
  75. }
  76. private setDefaultIndex(defaultIndex: number) {
  77. if (defaultIndex >= 0) {
  78. this._current_idx = defaultIndex;
  79. const item = this.content.children[defaultIndex];
  80. item.getChildByName("selected").active = true;
  81. this.label.string = this._data[defaultIndex];
  82. }
  83. }
  84. }