index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import _extends from 'babel-runtime/helpers/extends';
  2. import _defineProperty from 'babel-runtime/helpers/defineProperty';
  3. import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
  4. import PropTypes from '../_util/vue-types';
  5. import hasProp, { getOptionProps, getComponentFromProp, getListeners } from '../_util/props-util';
  6. import VcSwitch from '../vc-switch';
  7. import Wave from '../_util/wave';
  8. import Icon from '../icon';
  9. import { ConfigConsumerProps } from '../config-provider/configConsumerProps';
  10. import Base from '../base';
  11. import warning from '../_util/warning';
  12. var Switch = {
  13. name: 'ASwitch',
  14. __ANT_SWITCH: true,
  15. model: {
  16. prop: 'checked',
  17. event: 'change'
  18. },
  19. props: {
  20. prefixCls: PropTypes.string,
  21. // size=default and size=large are the same
  22. size: PropTypes.oneOf(['small', 'default', 'large']),
  23. disabled: PropTypes.bool,
  24. checkedChildren: PropTypes.any,
  25. unCheckedChildren: PropTypes.any,
  26. tabIndex: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  27. checked: PropTypes.bool,
  28. defaultChecked: PropTypes.bool,
  29. autoFocus: PropTypes.bool,
  30. loading: PropTypes.bool
  31. },
  32. inject: {
  33. configProvider: { 'default': function _default() {
  34. return ConfigConsumerProps;
  35. } }
  36. },
  37. methods: {
  38. focus: function focus() {
  39. this.$refs.refSwitchNode.focus();
  40. },
  41. blur: function blur() {
  42. this.$refs.refSwitchNode.blur();
  43. }
  44. },
  45. created: function created() {
  46. warning(hasProp(this, 'checked') || !hasProp(this, 'value'), 'Switch', '`value` is not validate prop, do you mean `checked`?');
  47. },
  48. render: function render() {
  49. var _classes;
  50. var h = arguments[0];
  51. var _getOptionProps = getOptionProps(this),
  52. customizePrefixCls = _getOptionProps.prefixCls,
  53. size = _getOptionProps.size,
  54. loading = _getOptionProps.loading,
  55. disabled = _getOptionProps.disabled,
  56. restProps = _objectWithoutProperties(_getOptionProps, ['prefixCls', 'size', 'loading', 'disabled']);
  57. var getPrefixCls = this.configProvider.getPrefixCls;
  58. var prefixCls = getPrefixCls('switch', customizePrefixCls);
  59. var classes = (_classes = {}, _defineProperty(_classes, prefixCls + '-small', size === 'small'), _defineProperty(_classes, prefixCls + '-loading', loading), _classes);
  60. var loadingIcon = loading ? h(Icon, {
  61. attrs: { type: 'loading' },
  62. 'class': prefixCls + '-loading-icon' }) : null;
  63. var switchProps = {
  64. props: _extends({}, restProps, {
  65. prefixCls: prefixCls,
  66. loadingIcon: loadingIcon,
  67. checkedChildren: getComponentFromProp(this, 'checkedChildren'),
  68. unCheckedChildren: getComponentFromProp(this, 'unCheckedChildren'),
  69. disabled: disabled || loading
  70. }),
  71. on: getListeners(this),
  72. 'class': classes,
  73. ref: 'refSwitchNode'
  74. };
  75. return h(
  76. Wave,
  77. {
  78. attrs: { insertExtraNode: true }
  79. },
  80. [h(VcSwitch, switchProps)]
  81. );
  82. }
  83. };
  84. /* istanbul ignore next */
  85. Switch.install = function (Vue) {
  86. Vue.use(Base);
  87. Vue.component(Switch.name, Switch);
  88. };
  89. export default Switch;