Group.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _defineProperty2 = require('babel-runtime/helpers/defineProperty');
  6. var _defineProperty3 = _interopRequireDefault(_defineProperty2);
  7. var _extends2 = require('babel-runtime/helpers/extends');
  8. var _extends3 = _interopRequireDefault(_extends2);
  9. var _classnames = require('classnames');
  10. var _classnames2 = _interopRequireDefault(_classnames);
  11. var _vueTypes = require('../_util/vue-types');
  12. var _vueTypes2 = _interopRequireDefault(_vueTypes);
  13. var _Radio = require('./Radio');
  14. var _Radio2 = _interopRequireDefault(_Radio);
  15. var _propsUtil = require('../_util/props-util');
  16. var _configConsumerProps = require('../config-provider/configConsumerProps');
  17. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  18. function noop() {}
  19. exports['default'] = {
  20. name: 'ARadioGroup',
  21. model: {
  22. prop: 'value'
  23. },
  24. props: {
  25. prefixCls: _vueTypes2['default'].string,
  26. defaultValue: _vueTypes2['default'].any,
  27. value: _vueTypes2['default'].any,
  28. size: {
  29. 'default': 'default',
  30. validator: function validator(value) {
  31. return ['large', 'default', 'small'].includes(value);
  32. }
  33. },
  34. options: {
  35. 'default': function _default() {
  36. return [];
  37. },
  38. type: Array
  39. },
  40. disabled: Boolean,
  41. name: String,
  42. buttonStyle: _vueTypes2['default'].string.def('outline')
  43. },
  44. data: function data() {
  45. var value = this.value,
  46. defaultValue = this.defaultValue;
  47. this.updatingValue = false;
  48. return {
  49. stateValue: value === undefined ? defaultValue : value
  50. };
  51. },
  52. provide: function provide() {
  53. return {
  54. radioGroupContext: this
  55. };
  56. },
  57. inject: {
  58. configProvider: { 'default': function _default() {
  59. return _configConsumerProps.ConfigConsumerProps;
  60. } }
  61. },
  62. computed: {
  63. radioOptions: function radioOptions() {
  64. var disabled = this.disabled;
  65. return this.options.map(function (option) {
  66. return typeof option === 'string' ? { label: option, value: option } : (0, _extends3['default'])({}, option, { disabled: option.disabled === undefined ? disabled : option.disabled });
  67. });
  68. },
  69. classes: function classes() {
  70. var _ref;
  71. var prefixCls = this.prefixCls,
  72. size = this.size;
  73. return _ref = {}, (0, _defineProperty3['default'])(_ref, '' + prefixCls, true), (0, _defineProperty3['default'])(_ref, prefixCls + '-' + size, size), _ref;
  74. }
  75. },
  76. watch: {
  77. value: function value(val) {
  78. this.updatingValue = false;
  79. this.stateValue = val;
  80. }
  81. },
  82. methods: {
  83. onRadioChange: function onRadioChange(ev) {
  84. var _this = this;
  85. var lastValue = this.stateValue;
  86. var value = ev.target.value;
  87. if (!(0, _propsUtil.hasProp)(this, 'value')) {
  88. this.stateValue = value;
  89. }
  90. // nextTick for https://github.com/vueComponent/ant-design-vue/issues/1280
  91. if (!this.updatingValue && value !== lastValue) {
  92. this.updatingValue = true;
  93. this.$emit('input', value);
  94. this.$emit('change', ev);
  95. }
  96. this.$nextTick(function () {
  97. _this.updatingValue = false;
  98. });
  99. }
  100. },
  101. render: function render() {
  102. var _this2 = this;
  103. var h = arguments[0];
  104. var _getListeners = (0, _propsUtil.getListeners)(this),
  105. _getListeners$mouseen = _getListeners.mouseenter,
  106. mouseenter = _getListeners$mouseen === undefined ? noop : _getListeners$mouseen,
  107. _getListeners$mousele = _getListeners.mouseleave,
  108. mouseleave = _getListeners$mousele === undefined ? noop : _getListeners$mousele;
  109. var props = (0, _propsUtil.getOptionProps)(this);
  110. var customizePrefixCls = props.prefixCls,
  111. options = props.options,
  112. buttonStyle = props.buttonStyle;
  113. var getPrefixCls = this.configProvider.getPrefixCls;
  114. var prefixCls = getPrefixCls('radio', customizePrefixCls);
  115. var groupPrefixCls = prefixCls + '-group';
  116. var classString = (0, _classnames2['default'])(groupPrefixCls, groupPrefixCls + '-' + buttonStyle, (0, _defineProperty3['default'])({}, groupPrefixCls + '-' + props.size, props.size));
  117. var children = (0, _propsUtil.filterEmpty)(this.$slots['default']);
  118. // 如果存在 options, 优先使用
  119. if (options && options.length > 0) {
  120. children = options.map(function (option) {
  121. if (typeof option === 'string') {
  122. return h(
  123. _Radio2['default'],
  124. {
  125. key: option,
  126. attrs: { prefixCls: prefixCls,
  127. disabled: props.disabled,
  128. value: option,
  129. checked: _this2.stateValue === option
  130. }
  131. },
  132. [option]
  133. );
  134. } else {
  135. return h(
  136. _Radio2['default'],
  137. {
  138. key: 'radio-group-value-options-' + option.value,
  139. attrs: { prefixCls: prefixCls,
  140. disabled: option.disabled || props.disabled,
  141. value: option.value,
  142. checked: _this2.stateValue === option.value
  143. }
  144. },
  145. [option.label]
  146. );
  147. }
  148. });
  149. }
  150. return h(
  151. 'div',
  152. { 'class': classString, on: {
  153. 'mouseenter': mouseenter,
  154. 'mouseleave': mouseleave
  155. }
  156. },
  157. [children]
  158. );
  159. }
  160. };