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

Commit 06d572f

Browse files
authored
Merge pull request #93 from alvarosaburido/feature/checkbox--radio
Feature/checkbox radio
2 parents dc2db8b + 767afc1 commit 06d572f

File tree

11 files changed

+183
-116
lines changed

11 files changed

+183
-116
lines changed

dev/typescript/App.vue

+31-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ import {
3535
FormValidation,
3636
PasswordInput,
3737
TextAreaInput,
38-
} from '../../dist/as-dynamic-forms.esm';
38+
CheckboxInput,
39+
RadioInput,
40+
} from '../../src';
3941
import { email, pattern } from '@/core/utils';
4042
4143
export default defineComponent({
@@ -88,6 +90,33 @@ export default defineComponent({
8890
cols: 20,
8991
rows: 5,
9092
}),
93+
new CheckboxInput({
94+
label: "Check if you're awesome",
95+
name: 'awesomness',
96+
}),
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+
}),
91120
],
92121
});
93122
function handleSubmit(values) {
@@ -99,12 +128,7 @@ export default defineComponent({
99128
function handleError(errors) {
100129
console.error(errors);
101130
}
102-
onMounted(() =>
103-
setTimeout(() => {
104-
form.fields[0].label = 'RockNRoll';
105-
form.fields[0].value = 'James Bond';
106-
}, 2000),
107-
);
131+
108132
return {
109133
title,
110134
form,

dev/typescript/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createApp } from 'vue';
33
import App from './App.vue';
44
import './styles/main.scss';
55

6-
import { createDynamicForms } from '../../dist/as-dynamic-forms.esm';
6+
import { createDynamicForms } from '../../src';
77

88
const VueDynamicForms = createDynamicForms({
99
theme: 'material',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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: 'asCheckboxInput',
12+
props,
13+
setup(props, { emit }) {
14+
const { onCheck, onFocus, onBlur } = useInputEvents(props?.control, emit);
15+
16+
return () => [
17+
h('input', {
18+
name: props?.control?.name || '',
19+
type: props?.control?.type,
20+
id: props?.control?.name,
21+
disabled: props?.control?.disabled,
22+
class: ['checkbox-control'],
23+
value: props?.control?.value,
24+
onFocus,
25+
onBlur,
26+
onChange: onCheck,
27+
}),
28+
h(
29+
'label',
30+
{
31+
class: ['checkbox-label'],
32+
for: props?.control?.name,
33+
},
34+
props?.control?.label,
35+
),
36+
];
37+
},
38+
});
39+
</script>

src/components/dynamic-input/DynamicInput.vue

+28-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { defineComponent, PropType, computed, h } from 'vue';
55
import TextInput from '../text-input/TextInput.vue';
66
import SelectInput from '../select-input/SelectInput.vue';
77
import TextAreaInput from '../text-area-input/TextAreaInput.vue';
8+
import CheckboxInput from '../checkbox-input/CheckboxInput.vue';
9+
import RadioInput from '../radio-input/RadioInput.vue';
810
911
import { FormControl } from '../../core/models';
1012
import { isEmpty, entries, values, keys } from '../../core/utils/helpers';
@@ -13,6 +15,8 @@ const components = {
1315
TextInput,
1416
SelectInput,
1517
TextAreaInput,
18+
CheckboxInput,
19+
RadioInput,
1620
};
1721
1822
const props = {
@@ -32,6 +36,9 @@ export default defineComponent({
3236
return [
3337
'dynamic-input',
3438
'form-group',
39+
{
40+
'form-group--inline': props?.control?.type === 'checkbox',
41+
},
3542
{
3643
'form-group--error': showErrors.value,
3744
},
@@ -103,6 +110,9 @@ export default defineComponent({
103110
};
104111
});
105112
113+
const hasLabel = computed(() => props?.control?.type !== 'checkbox');
114+
const isFieldSet = computed(() => props?.control?.type === 'radio');
115+
106116
return () => {
107117
switch (props?.control?.type) {
108118
case 'text':
@@ -117,24 +127,32 @@ export default defineComponent({
117127
case 'textarea':
118128
component = h(TextAreaInput, attributes.value);
119129
break;
120-
130+
case 'checkbox':
131+
component = h(CheckboxInput, attributes.value);
132+
break;
133+
case 'radio':
134+
component = h(RadioInput, attributes.value);
135+
break;
121136
default:
122137
break;
123138
}
124139
return h(
125-
'div',
140+
isFieldSet.value ? 'fieldset' : 'div',
126141
{
127142
class: getClasses.value,
143+
role: isFieldSet.value ? undefined : 'group',
128144
},
129145
[
130-
h(
131-
'label',
132-
{
133-
class: 'form-label',
134-
for: props?.control?.label,
135-
},
136-
props?.control?.label,
137-
),
146+
hasLabel.value
147+
? h(
148+
isFieldSet.value ? 'legend' : 'label',
149+
{
150+
class: 'form-label',
151+
for: props?.control?.label,
152+
},
153+
props?.control?.label,
154+
)
155+
: null,
138156
component,
139157
h(
140158
'div',
+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/components/text-input/TextInput.vue

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { defineComponent, h, PropType } from 'vue';
33
import { FormControl } from '../../core/models';
44
import { useInputEvents } from '../../composables/input-events';
5+
56
const props = {
67
control: Object as PropType<FormControl<any>>,
78
};

src/composables/input-events.ts

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ export function useInputEvents(control: FormControl<any> | undefined, emit) {
88
}
99
emit('changed', $event.target.value);
1010
}
11+
function onCheck($event) {
12+
if (control) {
13+
control.value = $event.target.checked;
14+
control.dirty = true;
15+
}
16+
emit('changed', $event.target.checked);
17+
}
1118
function onFocus() {
1219
emit('focus');
1320
}
@@ -21,5 +28,6 @@ export function useInputEvents(control: FormControl<any> | undefined, emit) {
2128
onFocus,
2229
onChange,
2330
onBlur,
31+
onCheck,
2432
};
2533
}

src/core/models.ts

+8
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ export class TextAreaInput extends InputBase<string> {
8383
type = 'textarea';
8484
}
8585

86+
export class CheckboxInput extends InputBase<boolean> {
87+
type = 'checkbox';
88+
}
89+
90+
export class RadioInput extends InputBase<boolean> {
91+
type = 'radio';
92+
}
93+
8694
export class FormControl<T> extends InputBase<T> {
8795
valid = true;
8896
invalid = false;

src/core/utils/form-control.model.js

-95
This file was deleted.

src/core/utils/index.ts

-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import formModels from './form-control.model';
21
import validators from './validators';
32

4-
export * from './form-control.model';
53
export * from './validators';
64

75
export default {
86
...validators,
9-
...formModels,
107
};

0 commit comments

Comments
 (0)