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

Commit 9973895

Browse files
committed
feat(checkbox): added simple checkbox
1 parent 4c492ba commit 9973895

File tree

10 files changed

+95
-115
lines changed

10 files changed

+95
-115
lines changed

dev/typescript/App.vue

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ import {
3535
FormValidation,
3636
PasswordInput,
3737
TextAreaInput,
38-
} from '../../dist/as-dynamic-forms.esm';
38+
CheckboxInput,
39+
} from '../../src';
3940
import { email, pattern } from '@/core/utils';
4041
4142
export default defineComponent({
@@ -88,6 +89,10 @@ export default defineComponent({
8889
cols: 20,
8990
rows: 5,
9091
}),
92+
new CheckboxInput({
93+
label: "Check if you're awesome",
94+
name: 'awesomness',
95+
}),
9196
],
9297
});
9398
function handleSubmit(values) {
@@ -99,12 +104,7 @@ export default defineComponent({
99104
function handleError(errors) {
100105
console.error(errors);
101106
}
102-
onMounted(() =>
103-
setTimeout(() => {
104-
form.fields[0].label = 'RockNRoll';
105-
form.fields[0].value = 'James Bond';
106-
}, 2000),
107-
);
107+
108108
return {
109109
title,
110110
form,

dev/typescript/main.ts

Lines changed: 1 addition & 1 deletion
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',
Lines changed: 39 additions & 0 deletions
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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ 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';
89
910
import { FormControl } from '../../core/models';
1011
import { isEmpty, entries, values, keys } from '../../core/utils/helpers';
@@ -13,6 +14,7 @@ const components = {
1314
TextInput,
1415
SelectInput,
1516
TextAreaInput,
17+
CheckboxInput,
1618
};
1719
1820
const props = {
@@ -32,6 +34,9 @@ export default defineComponent({
3234
return [
3335
'dynamic-input',
3436
'form-group',
37+
{
38+
'form-group--inline': props?.control?.type === 'checkbox',
39+
},
3540
{
3641
'form-group--error': showErrors.value,
3742
},
@@ -103,6 +108,8 @@ export default defineComponent({
103108
};
104109
});
105110
111+
const hasLabel = computed(() => props?.control?.type !== 'checkbox');
112+
106113
return () => {
107114
switch (props?.control?.type) {
108115
case 'text':
@@ -117,24 +124,29 @@ export default defineComponent({
117124
case 'textarea':
118125
component = h(TextAreaInput, attributes.value);
119126
break;
120-
127+
case 'checkbox':
128+
component = h(CheckboxInput, attributes.value);
129+
break;
121130
default:
122131
break;
123132
}
124133
return h(
125134
'div',
126135
{
127136
class: getClasses.value,
137+
role: 'group',
128138
},
129139
[
130-
h(
131-
'label',
132-
{
133-
class: 'form-label',
134-
for: props?.control?.label,
135-
},
136-
props?.control?.label,
137-
),
140+
hasLabel.value
141+
? h(
142+
'label',
143+
{
144+
class: 'form-label',
145+
for: props?.control?.label,
146+
},
147+
props?.control?.label,
148+
)
149+
: null,
138150
component,
139151
h(
140152
'div',

src/components/text-input/TextInput.vue

Lines changed: 1 addition & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ export class TextAreaInput extends InputBase<string> {
8383
type = 'textarea';
8484
}
8585

86+
export class CheckboxInput extends InputBase<boolean> {
87+
type = 'checkbox';
88+
}
89+
8690
export class FormControl<T> extends InputBase<T> {
8791
valid = true;
8892
invalid = false;

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

Lines changed: 0 additions & 95 deletions
This file was deleted.

src/core/utils/index.ts

Lines changed: 0 additions & 3 deletions
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
};

src/styles/themes/default.scss

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ $input-error-color: #dc3545 !default;
5858
order: 2;
5959
}
6060

61+
&--inline {
62+
flex-direction: row;
63+
align-items: center;
64+
}
65+
6166
&--error {
6267
.form-label {
6368
color: $input-error-color;
@@ -121,3 +126,12 @@ $input-error-color: #dc3545 !default;
121126
}
122127
}
123128
}
129+
130+
.checkbox-control {
131+
margin-right: 1rem;
132+
}
133+
134+
.checkbox-label {
135+
user-select: none;
136+
font-size: 0.85rem;
137+
}

0 commit comments

Comments
 (0)