Skip to content

Commit 1b08f45

Browse files
committed
Add Connected <RadioGroup />
1 parent f7335f8 commit 1b08f45

File tree

4 files changed

+157
-4
lines changed

4 files changed

+157
-4
lines changed

VueForm/ConnectedRadioGroup.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import { Radio, RadioGroup } from 'element-ui'
2+
import noop from 'lodash/noop'
3+
import isNil from 'lodash/isNil'
4+
import resolveRegisterFormComponent from './utils/resolveRegisterFormComponent'
5+
import FormItem from './ConnectedFormItem'
6+
7+
export default {
8+
props: {
9+
name: {
10+
type: String,
11+
required: true,
12+
},
13+
14+
options: {
15+
type: Array,
16+
required: true,
17+
},
18+
19+
value: [String, Number],
20+
21+
valueKey: {
22+
type: String,
23+
default: () => 'id',
24+
},
25+
26+
labelKey: {
27+
type: String,
28+
default: () => 'name',
29+
},
30+
31+
disabledKey: {
32+
type: String,
33+
default: () => 'disabled',
34+
},
35+
36+
size: String,
37+
disabled: Boolean,
38+
border: Boolean,
39+
40+
validate: {
41+
type: Function,
42+
default: noop,
43+
},
44+
45+
handleFocus: {
46+
type: Function,
47+
default: noop,
48+
},
49+
50+
handleBlur: {
51+
type: Function,
52+
default: noop,
53+
},
54+
55+
handleChange: {
56+
type: Function,
57+
default: noop,
58+
},
59+
60+
/* FormItem Props */
61+
label: String,
62+
formItem: Boolean,
63+
labelWidth: String,
64+
},
65+
66+
data() {
67+
const $registerFormComponent = resolveRegisterFormComponent(this)
68+
69+
return $registerFormComponent(this.name, this.value, this.validate)
70+
},
71+
72+
destroyed() {
73+
this.cleanFormValue()
74+
},
75+
76+
methods: {
77+
generateOptions(option) {
78+
let { [this.valueKey]: optionValue, [this.labelKey]: optionLabel } = option
79+
const { [this.disabledKey]: optionDisabled } = option
80+
81+
if (isNil(optionLabel)) {
82+
optionValue = option
83+
optionLabel = option
84+
}
85+
86+
return (
87+
<Radio key={optionValue} label={optionValue} disabled={optionDisabled} border={this.border}>
88+
{optionLabel || optionValue}
89+
</Radio>
90+
)
91+
},
92+
93+
handleFieldBlur(...args) {
94+
this.touched = true
95+
96+
this.handleBlur(...args)
97+
},
98+
99+
renderRadioGroup(value, setValue) {
100+
return (
101+
<RadioGroup
102+
class={this.class}
103+
name={this.name}
104+
value={value}
105+
size={this.size}
106+
disabled={this.disabled}
107+
on-input={setValue}
108+
on-focus={this.handleFocus}
109+
on-blur={this.handleFieldBlur}
110+
on-change={setValue}>
111+
{this.options.map(this.generateOptions)}
112+
</RadioGroup>
113+
)
114+
},
115+
},
116+
117+
render() {
118+
const [value, setValue, error] = this.useState()
119+
const fieldError = this.touched ? error : undefined
120+
121+
if (this.formItem) {
122+
return (
123+
<FormItem label={this.label || this.name} label-width={this.labelWidth} error={fieldError}>
124+
{this.renderRadioGroup(value, setValue)}
125+
</FormItem>
126+
)
127+
}
128+
129+
return this.renderRadioGroup(value, setValue)
130+
},
131+
}

VueForm/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Form from './Form'
22
import Radio from './ConnectedRadio'
3+
import RadioGroup from './ConnectedRadioGroup'
34
import Checkbox from './ConnectedCheckbox'
45
import CheckboxGroup from './ConnectedCheckboxGroup'
56
import Input from './ConnectedInput'
@@ -14,6 +15,7 @@ import validations from './validations'
1415

1516
export {
1617
Radio,
18+
RadioGroup,
1719
Checkbox,
1820
CheckboxGroup,
1921
Input,

example/src/App.vue

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@ import BasicForm from './BasicForm'
33
import InlineValidationsForm from './InlineValidationsForm'
44
import AsyncValidationForm from './AsyncValidationForm'
55
6+
const Divider = {
7+
render() {
8+
return (
9+
<div class="divider" />
10+
)
11+
},
12+
}
13+
614
export default {
715
render() {
816
return (
917
<div class="forms">
1018
<BasicForm />
11-
<br />
12-
<hr />
19+
<Divider />
1320
<InlineValidationsForm />
14-
<br />
15-
<hr />
21+
<Divider />
1622
<AsyncValidationForm />
1723
</div>
1824
)
@@ -25,4 +31,10 @@ export default {
2531
max-width: 1000px;
2632
padding: 0 20px;
2733
}
34+
35+
.divider {
36+
height: 2px;
37+
background-color: #757575;
38+
margin: 50px 0;
39+
}
2840
</style>

example/src/BasicForm/BasicForm.vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Form, {
55
CheckboxGroup,
66
InputNumber,
77
Radio,
8+
RadioGroup,
89
Select,
910
Slider,
1011
Switch,
@@ -27,6 +28,7 @@ export default {
2728
'Ebony Maw',
2829
],
2930
companiesOptions: ['Apple', 'Google', 'Amazon', 'Microsoft'],
31+
browsersOptions: ['Chrome', 'Safari', 'Firefox', 'Edge', 'Opera'],
3032
}
3133
},
3234
@@ -58,6 +60,12 @@ export default {
5860
<Radio name="word" value="B">B</Radio>
5961
</div>
6062
<br />
63+
<RadioGroup
64+
formItem
65+
name="browser"
66+
label="Which browser do you use?"
67+
options={this.browsersOptions}
68+
/>
6169
<Select
6270
formItem
6371
name="superhero"

0 commit comments

Comments
 (0)