Group.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.radioGroupProps = exports.default = void 0;
  7. var _vue = require("vue");
  8. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  9. var _classNames2 = _interopRequireDefault(require("../_util/classNames"));
  10. var _vueTypes = _interopRequireDefault(require("../_util/vue-types"));
  11. var _Radio = _interopRequireDefault(require("./Radio"));
  12. var _useConfigInject2 = _interopRequireDefault(require("../_util/hooks/useConfigInject"));
  13. var _type = require("../_util/type");
  14. var _FormItemContext = require("../form/FormItemContext");
  15. var RadioGroupSizeTypes = (0, _type.tuple)('large', 'default', 'small');
  16. var radioGroupProps = function radioGroupProps() {
  17. return {
  18. prefixCls: String,
  19. value: _vueTypes.default.any,
  20. size: _vueTypes.default.oneOf(RadioGroupSizeTypes),
  21. options: {
  22. type: Array
  23. },
  24. disabled: {
  25. type: Boolean,
  26. default: undefined
  27. },
  28. name: String,
  29. buttonStyle: {
  30. type: String,
  31. default: 'outline'
  32. },
  33. id: String,
  34. optionType: {
  35. type: String,
  36. default: 'default'
  37. },
  38. onChange: Function,
  39. 'onUpdate:value': Function
  40. };
  41. };
  42. exports.radioGroupProps = radioGroupProps;
  43. var _default2 = (0, _vue.defineComponent)({
  44. compatConfig: {
  45. MODE: 3
  46. },
  47. name: 'ARadioGroup',
  48. props: radioGroupProps(),
  49. // emits: ['update:value', 'change'],
  50. setup: function setup(props, _ref) {
  51. var slots = _ref.slots,
  52. emit = _ref.emit;
  53. var formItemContext = (0, _FormItemContext.useInjectFormItemContext)();
  54. var _useConfigInject = (0, _useConfigInject2.default)('radio', props),
  55. prefixCls = _useConfigInject.prefixCls,
  56. direction = _useConfigInject.direction,
  57. size = _useConfigInject.size;
  58. var stateValue = (0, _vue.ref)(props.value);
  59. var updatingValue = (0, _vue.ref)(false);
  60. (0, _vue.watch)(function () {
  61. return props.value;
  62. }, function (val) {
  63. stateValue.value = val;
  64. updatingValue.value = false;
  65. });
  66. var onRadioChange = function onRadioChange(ev) {
  67. var lastValue = stateValue.value;
  68. var value = ev.target.value;
  69. if (!('value' in props)) {
  70. stateValue.value = value;
  71. }
  72. // nextTick for https://github.com/vueComponent/ant-design-vue/issues/1280
  73. if (!updatingValue.value && value !== lastValue) {
  74. updatingValue.value = true;
  75. emit('update:value', value);
  76. emit('change', ev);
  77. formItemContext.onFieldChange();
  78. }
  79. (0, _vue.nextTick)(function () {
  80. updatingValue.value = false;
  81. });
  82. };
  83. (0, _vue.provide)('radioGroupContext', {
  84. onRadioChange: onRadioChange,
  85. stateValue: stateValue,
  86. props: props
  87. });
  88. return function () {
  89. var _classNames;
  90. var options = props.options,
  91. optionType = props.optionType,
  92. buttonStyle = props.buttonStyle,
  93. _props$id = props.id,
  94. id = _props$id === void 0 ? formItemContext.id.value : _props$id;
  95. var groupPrefixCls = "".concat(prefixCls.value, "-group");
  96. var classString = (0, _classNames2.default)(groupPrefixCls, "".concat(groupPrefixCls, "-").concat(buttonStyle), (_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(groupPrefixCls, "-").concat(size.value), size.value), (0, _defineProperty2.default)(_classNames, "".concat(groupPrefixCls, "-rtl"), direction.value === 'rtl'), _classNames));
  97. var children = null;
  98. if (options && options.length > 0) {
  99. var optionsPrefixCls = optionType === 'button' ? "".concat(prefixCls.value, "-button") : prefixCls.value;
  100. children = options.map(function (option) {
  101. if (typeof option === 'string' || typeof option === 'number') {
  102. return (0, _vue.createVNode)(_Radio.default, {
  103. "key": option,
  104. "prefixCls": optionsPrefixCls,
  105. "disabled": props.disabled,
  106. "value": option,
  107. "checked": stateValue.value === option
  108. }, {
  109. default: function _default() {
  110. return [option];
  111. }
  112. });
  113. }
  114. var value = option.value,
  115. disabled = option.disabled,
  116. label = option.label;
  117. return (0, _vue.createVNode)(_Radio.default, {
  118. "key": "radio-group-value-options-".concat(value),
  119. "prefixCls": optionsPrefixCls,
  120. "disabled": disabled || props.disabled,
  121. "value": value,
  122. "checked": stateValue.value === value
  123. }, {
  124. default: function _default() {
  125. return [label];
  126. }
  127. });
  128. });
  129. } else {
  130. var _slots$default;
  131. children = (_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots);
  132. }
  133. return (0, _vue.createVNode)("div", {
  134. "class": classString,
  135. "id": id
  136. }, [children]);
  137. };
  138. }
  139. });
  140. exports.default = _default2;