index.js 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { VantComponent } from '../common/component';
  2. VantComponent({
  3. field: true,
  4. classes: ['node-class'],
  5. props: {
  6. checked: null,
  7. loading: Boolean,
  8. disabled: Boolean,
  9. activeColor: String,
  10. inactiveColor: String,
  11. size: {
  12. type: String,
  13. value: '30px'
  14. },
  15. activeValue: {
  16. type: null,
  17. value: true
  18. },
  19. inactiveValue: {
  20. type: null,
  21. value: false
  22. }
  23. },
  24. watch: {
  25. checked(value) {
  26. this.set({ value });
  27. }
  28. },
  29. created() {
  30. this.set({ value: this.data.checked });
  31. },
  32. methods: {
  33. onClick() {
  34. const { activeValue, inactiveValue } = this.data;
  35. if (!this.data.disabled && !this.data.loading) {
  36. const checked = this.data.checked === activeValue;
  37. const value = checked ? inactiveValue : activeValue;
  38. this.$emit('input', value);
  39. this.$emit('change', value);
  40. }
  41. }
  42. }
  43. });