index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. Component({
  2. behaviors: ['wx://form-field'],
  3. externalClasses: ['l-class', 'l-error-text', 'l-error-text-class'],
  4. relations: {
  5. '../checkbox/index': {
  6. type: 'child',
  7. linked(target) {
  8. this.init(target);
  9. },
  10. linkChanged() {
  11. },
  12. unlinked() {
  13. // this.init(target);
  14. }
  15. }
  16. },
  17. properties: {
  18. // 选项的排列方式 一行显示 or 多行显示
  19. placement: {
  20. type: String,
  21. value: 'column', //column row
  22. },
  23. // 最多选中值
  24. maxSelected: {
  25. type: [Number,null],
  26. value: null
  27. },
  28. minSelected: {
  29. type: [Number,null],
  30. value: null
  31. }
  32. },
  33. data: {
  34. },
  35. attached() {
  36. let { minSelected, maxSelected} = this.properties;
  37. this.checkMax(minSelected, maxSelected);
  38. },
  39. methods: {
  40. init(target) {
  41. if(this._keys === undefined) this._keys = {};
  42. // if(this._selected === undefined) this._selected = {};
  43. // this.checkDefaultItem(target);
  44. this.checkedKeyRepeat(target);
  45. },
  46. checkedKeyRepeat(target) {
  47. let { key } = target.properties;
  48. if(this._keys[key]) {
  49. throw new Error(`keys有重复元素, chekbox的key属性不能重复:${key}`);
  50. } else {
  51. this._keys[key] = true;
  52. }
  53. },
  54. checkDefaultItem(target) {
  55. const { key, checked } = target.properties;
  56. if(checked) {
  57. this._selected[key] = checked;
  58. }
  59. },
  60. checkMax(min, max) {
  61. if(min !== null && min < 0) {
  62. throw new Error('最小选择个数必须大于等于0');
  63. }
  64. if(max !== null && max < 0) {
  65. throw new Error('最多选择个数必须大于0');
  66. }
  67. if(max !== null && min !== null && min >= max) {
  68. throw new Error('最多选择个数必须大于最小选择个数');
  69. }
  70. },
  71. onEmitEventHandle(currentItem) {
  72. // currentItem.checked ? this.addSelect(currentItem.key):this.removeSelect(currentItem.key);
  73. this.triggerEvent('linchange', currentItem, {
  74. bubbles: true,
  75. composed: true
  76. });
  77. },
  78. onEmitOverflowHandle(data){
  79. this.triggerEvent('linout', data, {
  80. bubbles: true,
  81. composed: true
  82. });
  83. },
  84. removeSelect(key) {
  85. delete this._selected[key];
  86. },
  87. addSelect(key) {
  88. this._selected[key] = key;
  89. },
  90. _selected(){
  91. const items = this.getRelationNodes('../checkbox/index');
  92. let num = 0;
  93. items.map(item=> {
  94. item.properties.checked ? num++ : '';
  95. });
  96. return num;
  97. }
  98. }
  99. });