index.js 1.11 KB
Newer Older
fengzhaoyu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
"use strict";

Component({
  mixins: [],
  data: {
    $uiName: 'switch',
    _checked: false
  },
  props: {
    className: '',
    checked: false,
    disabled: false,
    dmData: {},
    isControlled: false,
    onDmChange: function onDmChange() {}
  },
  didMount: function didMount() {
    this.setData({
      _checked: this.props.checked
    });
  },
  didUpdate: function didUpdate(prevProps, prevData) {
    if (prevProps.checked != this.props.checked && this.props.checked != this.data._checked) {
      this.setData({
        _checked: this.props.checked
      });
    }
  },
  didUnmount: function didUnmount() {},
  methods: {
    onDmTap: function onDmTap(e) {
      var _this$props = this.props,
          disabled = _this$props.disabled,
          isControlled = _this$props.isControlled;

      if (disabled) {
        return;
      }

      var checked = this.data._checked;

      if (!isControlled) {
        checked = !checked;
        this.setData({
          _checked: checked
        });
      }

      this.props.onDmChange({
        checked: checked,
        dmData: this.props.dmData
      });
    }
  }
});