Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 767afc1

Browse files
committed
feat(radio): added radio buttons
1 parent 9973895 commit 767afc1

File tree

5 files changed

+92
-5
lines changed

5 files changed

+92
-5
lines changed

dev/typescript/App.vue

+24
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
PasswordInput,
3737
TextAreaInput,
3838
CheckboxInput,
39+
RadioInput,
3940
} from '../../src';
4041
import { email, pattern } from '@/core/utils';
4142
@@ -93,6 +94,29 @@ export default defineComponent({
9394
label: "Check if you're awesome",
9495
name: 'awesomness',
9596
}),
97+
new RadioInput({
98+
label: 'Select one option',
99+
name: 'character',
100+
options: [
101+
{
102+
key: 'mario',
103+
value: 'Mario',
104+
},
105+
{
106+
key: 'crash-bandicoot',
107+
value: 'Crash Bandicoot',
108+
},
109+
{
110+
key: 'sonic',
111+
value: 'Sonic',
112+
},
113+
{
114+
key: 'banjo-kazooie',
115+
value: 'Banjo Kazooie',
116+
disabled: true,
117+
},
118+
],
119+
}),
96120
],
97121
});
98122
function handleSubmit(values) {

src/components/dynamic-input/DynamicInput.vue

+9-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import TextInput from '../text-input/TextInput.vue';
66
import SelectInput from '../select-input/SelectInput.vue';
77
import TextAreaInput from '../text-area-input/TextAreaInput.vue';
88
import CheckboxInput from '../checkbox-input/CheckboxInput.vue';
9+
import RadioInput from '../radio-input/RadioInput.vue';
910
1011
import { FormControl } from '../../core/models';
1112
import { isEmpty, entries, values, keys } from '../../core/utils/helpers';
@@ -15,6 +16,7 @@ const components = {
1516
SelectInput,
1617
TextAreaInput,
1718
CheckboxInput,
19+
RadioInput,
1820
};
1921
2022
const props = {
@@ -109,6 +111,7 @@ export default defineComponent({
109111
});
110112
111113
const hasLabel = computed(() => props?.control?.type !== 'checkbox');
114+
const isFieldSet = computed(() => props?.control?.type === 'radio');
112115
113116
return () => {
114117
switch (props?.control?.type) {
@@ -127,19 +130,22 @@ export default defineComponent({
127130
case 'checkbox':
128131
component = h(CheckboxInput, attributes.value);
129132
break;
133+
case 'radio':
134+
component = h(RadioInput, attributes.value);
135+
break;
130136
default:
131137
break;
132138
}
133139
return h(
134-
'div',
140+
isFieldSet.value ? 'fieldset' : 'div',
135141
{
136142
class: getClasses.value,
137-
role: 'group',
143+
role: isFieldSet.value ? undefined : 'group',
138144
},
139145
[
140146
hasLabel.value
141147
? h(
142-
'label',
148+
isFieldSet.value ? 'legend' : 'label',
143149
{
144150
class: 'form-label',
145151
for: props?.control?.label,
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<script lang="ts">
2+
import { defineComponent, h, PropType } from 'vue';
3+
import { FormControl } from '../../core/models';
4+
import { useInputEvents } from '../../composables/input-events';
5+
6+
const props = {
7+
control: Object as PropType<FormControl<string>>,
8+
};
9+
10+
export default defineComponent({
11+
name: 'asRadioInput',
12+
props,
13+
setup(props, { emit }) {
14+
const { onChange, onFocus, onBlur } = useInputEvents(props?.control, emit);
15+
const renderRadios = props?.control?.options?.map(option => {
16+
return h('div', { class: 'radio-input' }, [
17+
h('input', {
18+
name: props?.control?.name || '',
19+
type: props?.control?.type,
20+
id: option.key,
21+
disabled: option.disabled,
22+
class: ['radio-control'],
23+
value: option.value,
24+
onFocus,
25+
onBlur,
26+
onChange,
27+
}),
28+
h(
29+
'label',
30+
{
31+
class: ['radio-label'],
32+
for: option.key,
33+
},
34+
option.value,
35+
),
36+
]);
37+
});
38+
return () => [
39+
h(
40+
'div',
41+
{
42+
class: 'radio-group',
43+
tabIndex: -1,
44+
role: 'group',
45+
},
46+
renderRadios,
47+
),
48+
];
49+
},
50+
});
51+
</script>

src/core/models.ts

+4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ export class CheckboxInput extends InputBase<boolean> {
8787
type = 'checkbox';
8888
}
8989

90+
export class RadioInput extends InputBase<boolean> {
91+
type = 'radio';
92+
}
93+
9094
export class FormControl<T> extends InputBase<T> {
9195
valid = true;
9296
invalid = false;

src/styles/themes/default.scss

+4-2
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,13 @@ $input-error-color: #dc3545 !default;
127127
}
128128
}
129129

130-
.checkbox-control {
130+
.checkbox-control,
131+
.radio-control {
131132
margin-right: 1rem;
132133
}
133134

134-
.checkbox-label {
135+
.checkbox-label,
136+
.radio-label {
135137
user-select: none;
136138
font-size: 0.85rem;
137139
}

0 commit comments

Comments
 (0)