index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. Component({
  2. externalClasses: ['l-class', 'l-error-text', 'l-error-text-class'],
  3. behaviors: ['wx://form-field'],
  4. relations: {
  5. '../radio/index': {
  6. type: 'child',
  7. linked() {
  8. // const currentLength = this.add();
  9. // if (currentLength === this.properties.length) {
  10. this.init();
  11. // }
  12. },
  13. linkChanged() {
  14. },
  15. unlinked() {
  16. this.init();
  17. }
  18. }
  19. },
  20. properties: {
  21. current: {
  22. type: String
  23. },
  24. noneChecked: {
  25. type: Boolean,
  26. value: true
  27. },
  28. placement: {
  29. type: String,
  30. value: 'column', //column row
  31. },
  32. },
  33. data: {
  34. currentLength: 0
  35. },
  36. methods: {
  37. // add() {
  38. // if (this.properties.length <= 0) {
  39. // throw new Error('为提高性能,请主动设置radio-group的length属性');
  40. // }
  41. // return this.data.currentLength += 1;
  42. // },
  43. // reduce() {
  44. // this.length -= 1;
  45. // return this.data.currentLength -= 1;
  46. // },
  47. checkedKeyRepeat(items) {
  48. let keys = items.map(item => {
  49. return item.data.key;
  50. });
  51. const repeat = this.isRepeat(keys);
  52. if (repeat !== false) {
  53. throw new Error(`keys有重复元素, radio的key属性不能重复:${repeat}`);
  54. }
  55. },
  56. isRepeat(arr) {
  57. let hash = {};
  58. for (let i in arr) {
  59. if (hash[arr[i]]) //hash 哈希
  60. return arr[i];
  61. hash[arr[i]] = true;
  62. }
  63. return false;
  64. },
  65. init() {
  66. const items = this.getRelationNodes('../radio/index');
  67. this.checkedKeyRepeat(items);
  68. this.onChangeHandle(items);
  69. },
  70. onChangeHandle(items) {
  71. items.forEach((item) => {
  72. let checked = this.properties.current == item.data.key;
  73. item.setChecked(checked, item.data.key);
  74. });
  75. },
  76. onEmitEventHandle(currentItem, select) {
  77. this.properties.current = select?currentItem.key:null;
  78. const items = this.getRelationNodes('../radio/index');
  79. this.onChangeHandle(items);
  80. // currentItem.currentKey = this.properties.current
  81. Object.assign(currentItem, {
  82. currentKey: this.properties.current
  83. });
  84. this.triggerEvent('linchange', currentItem, {
  85. bubbles: true,
  86. composed: true
  87. });
  88. }
  89. },
  90. observers: {
  91. 'current': function() {
  92. this.init();
  93. }
  94. }
  95. });