index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. Component({
  2. properties: {
  3. show: {
  4. type: Boolean,
  5. observer(show) {
  6. if (!show) return;
  7. this.updateDivisions();
  8. },
  9. },
  10. title: {
  11. type: String,
  12. value: '',
  13. },
  14. value: {
  15. type: String,
  16. value: '',
  17. observer() {
  18. if (!this.data.show) return;
  19. this.updateDivisions();
  20. },
  21. },
  22. pickerOptions: {
  23. type: Array,
  24. value: [],
  25. observer() {
  26. if (!this.data.show) return;
  27. this.updateDivisions();
  28. },
  29. },
  30. headerVisible: {
  31. type: Boolean,
  32. value: true,
  33. },
  34. },
  35. data: {
  36. pickerValue: [],
  37. },
  38. methods: {
  39. updateDivisions() {
  40. const { pickerOptions, value } = this.data;
  41. const index = (pickerOptions || []).findIndex(
  42. (item) => item.code === value,
  43. );
  44. setTimeout(() => {
  45. this.setData({ pickerValue: index >= 0 ? [index] : [0] });
  46. }, 0);
  47. },
  48. getAreaByIndex(indexes) {
  49. const { pickerOptions } = this.data;
  50. return pickerOptions[indexes.toString()];
  51. },
  52. onChange(e) {
  53. const currentValue = e.detail.value;
  54. const target = this.getAreaByIndex(currentValue);
  55. if (target === null) return;
  56. this.setData({ pickerValue: currentValue });
  57. this.triggerEvent('change', { value: target.code, target: target });
  58. },
  59. onConfirm() {
  60. const target = this.getAreaByIndex(this.data.pickerValue);
  61. this.triggerEvent('confirm', { value: target?.code, target });
  62. },
  63. onClose() {
  64. this.triggerEvent('close');
  65. },
  66. },
  67. });