index.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. Component({
  2. externalClasses: ['l-class', 'l-disabled-class'],
  3. behaviors: ['wx://form-field'],
  4. relations: {
  5. '../radio-group/index': {
  6. type: 'parent'
  7. }
  8. },
  9. properties: {
  10. key: String,
  11. cell: Object,
  12. // radio的大小
  13. size: {
  14. type: String,
  15. value: '38rpx'
  16. },
  17. disabled: {
  18. type: Boolean
  19. },
  20. custom: Boolean,
  21. color: {
  22. type: String,
  23. value: '#ccc'
  24. },
  25. // 选中后的颜色
  26. selectColor: {
  27. type: String,
  28. value: '#3963BC'
  29. },
  30. disabledColor: {
  31. type: String,
  32. value: '#ccc'
  33. },
  34. placement: {
  35. type: String,
  36. value: 'left'
  37. },
  38. transition: {
  39. type: Boolean,
  40. value: true
  41. }
  42. },
  43. data: {
  44. checked: false
  45. },
  46. methods: {
  47. setChecked(checked) {
  48. this.setData({
  49. checked,
  50. });
  51. },
  52. // 点击radio
  53. onRadioChangeTap() {
  54. if (this.properties.disabled) {
  55. return;
  56. }
  57. const parent = this.getRelationNodes('../radio-group/index')[0];
  58. const noneChecked = parent.properties.noneChecked;
  59. const isCurrent = this.isCurrentSelectedKey(parent);
  60. let select = true;
  61. if (isCurrent) {
  62. select = false;
  63. if (!noneChecked) {
  64. return;
  65. }
  66. }
  67. const checked = !this.data.checked;
  68. this.data.checked = checked;
  69. // 子组件不能修改父组件属性
  70. // parent.properties.current = null
  71. const item = {
  72. checked,
  73. key: this.properties.key,
  74. cell: this.properties.cell
  75. };
  76. if (parent) {
  77. parent.onEmitEventHandle(item, select);
  78. }
  79. },
  80. isCurrentSelectedKey(parent) {
  81. const currentKey = parent.properties.current;
  82. if (currentKey == this.properties.key) {
  83. return true;
  84. }
  85. return false;
  86. }
  87. }
  88. });