UNPKG

4.52 kBJavaScriptView Raw
1import { createVNode as _createVNode, isVNode as _isVNode } from "vue";
2
3function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4
5import { provide, inject, nextTick, defineComponent } from 'vue';
6import classNames from '../_util/classNames';
7import PropTypes from '../_util/vue-types';
8import Radio from './Radio';
9import { getOptionProps, filterEmpty, hasProp, getSlot } from '../_util/props-util';
10import { defaultConfigProvider } from '../config-provider';
11import { tuple } from '../_util/type';
12
13function _isSlot(s) {
14 return typeof s === 'function' || Object.prototype.toString.call(s) === '[object Object]' && !_isVNode(s);
15}
16
17export default defineComponent({
18 name: 'ARadioGroup',
19 props: {
20 prefixCls: PropTypes.string,
21 defaultValue: PropTypes.any,
22 value: PropTypes.any,
23 size: PropTypes.oneOf(tuple('large', 'default', 'small')).def('default'),
24 options: PropTypes.array,
25 disabled: PropTypes.looseBool,
26 name: PropTypes.string,
27 buttonStyle: PropTypes.string.def('outline'),
28 onChange: PropTypes.func
29 },
30 emits: ['update:value', 'change'],
31 setup: function setup() {
32 return {
33 updatingValue: false,
34 configProvider: inject('configProvider', defaultConfigProvider),
35 radioGroupContext: null
36 };
37 },
38 data: function data() {
39 var value = this.value,
40 defaultValue = this.defaultValue;
41 return {
42 stateValue: value === undefined ? defaultValue : value
43 };
44 },
45 watch: {
46 value: function value(val) {
47 this.updatingValue = false;
48 this.stateValue = val;
49 }
50 },
51 // computed: {
52 // radioOptions() {
53 // const { disabled } = this;
54 // return this.options.map(option => {
55 // return typeof option === 'string'
56 // ? { label: option, value: option }
57 // : { ...option, disabled: option.disabled === undefined ? disabled : option.disabled };
58 // });
59 // },
60 // },
61 created: function created() {
62 this.radioGroupContext = provide('radioGroupContext', this);
63 },
64 methods: {
65 onRadioChange: function onRadioChange(ev) {
66 var _this = this;
67
68 var lastValue = this.stateValue;
69 var value = ev.target.value;
70
71 if (!hasProp(this, 'value')) {
72 this.stateValue = value;
73 } // nextTick for https://github.com/vueComponent/ant-design-vue/issues/1280
74
75
76 if (!this.updatingValue && value !== lastValue) {
77 this.updatingValue = true;
78 this.$emit('update:value', value);
79 this.$emit('change', ev);
80 }
81
82 nextTick(function () {
83 _this.updatingValue = false;
84 });
85 }
86 },
87 render: function render() {
88 var _this2 = this;
89
90 var props = getOptionProps(this);
91 var customizePrefixCls = props.prefixCls,
92 options = props.options,
93 buttonStyle = props.buttonStyle;
94 var getPrefixCls = this.configProvider.getPrefixCls;
95 var prefixCls = getPrefixCls('radio', customizePrefixCls);
96 var groupPrefixCls = "".concat(prefixCls, "-group");
97 var classString = classNames(groupPrefixCls, "".concat(groupPrefixCls, "-").concat(buttonStyle), _defineProperty({}, "".concat(groupPrefixCls, "-").concat(props.size), props.size));
98 var children = filterEmpty(getSlot(this)); // 如果存在 options, 优先使用
99
100 if (options && options.length > 0) {
101 children = options.map(function (option) {
102 if (typeof option === 'string') {
103 return _createVNode(Radio, {
104 "key": option,
105 "prefixCls": prefixCls,
106 "disabled": props.disabled,
107 "value": option,
108 "checked": _this2.stateValue === option
109 }, _isSlot(option) ? option : {
110 default: function _default() {
111 return [option];
112 }
113 });
114 }
115
116 return _createVNode(Radio, {
117 "key": "radio-group-value-options-".concat(option.value),
118 "prefixCls": prefixCls,
119 "disabled": option.disabled || props.disabled,
120 "value": option.value,
121 "checked": _this2.stateValue === option.value
122 }, {
123 default: function _default() {
124 return [option.label];
125 }
126 });
127 });
128 }
129
130 return _createVNode("div", {
131 "class": classString
132 }, _isSlot(children) ? children : {
133 default: function _default() {
134 return [children];
135 }
136 });
137 }
138});
\No newline at end of file