Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

Commit 795c94e

Browse files
committed
add FormCheckbox component
1 parent 61d9867 commit 795c94e

File tree

8 files changed

+116
-27
lines changed

8 files changed

+116
-27
lines changed

example/src/components/FormsPage.vue

+22-4
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,26 @@
6464
<form-group>
6565
<form-label>Inline Radios</form-label>
6666
<div class="custom-controls-stacked">
67-
<form-radio checked name="example-radios" :value="1" label="Option 1" inline/>
68-
<form-radio name="example-radios" :value="2" label="Option 2" inline/>
69-
<form-radio name="example-radios" :value="3" label="Option 3" inline/>
67+
<form-radio checked name="example-inline-radios" :value="1" label="Option 1" inline/>
68+
<form-radio name="example-inline-radios" :value="2" label="Option 2" inline/>
69+
<form-radio name="example-inline-radios" :value="3" label="Option 3" inline/>
70+
</div>
71+
</form-group>
72+
<form-group>
73+
<form-label>Checkboxes</form-label>
74+
<div class="custom-controls-stacked">
75+
<form-checkbox checked name="example-radios" value="option1" label="Option 1"/>
76+
<form-checkbox name="example-checkboxes" value="option2" label="Option 2"/>
77+
<form-checkbox name="example-checkboxes" value="option3" disabled label="Option Disabled"/>
78+
<form-checkbox name="example-checkboxes" checked value="option4" disabled label="Option Disabled Checked"/>
79+
</div>
80+
</form-group>
81+
<form-group>
82+
<form-label>Inline Checkboxes</form-label>
83+
<div class="custom-controls-stacked">
84+
<form-checkbox checked name="example-inline-checkboxes" value="option1" label="Option 1" inline/>
85+
<form-checkbox name="example-inline-checkboxes" value="option2" label="Option 2" inline/>
86+
<form-checkbox name="example-inline-checkboxes" value="option3" label="Option 3" inline/>
7087
</div>
7188
</form-group>
7289
</grid-col>
@@ -80,7 +97,8 @@
8097

8198
<script>
8299
export default {
83-
name: "forms-page"
100+
name: "forms-page",
101+
data: () => ({check: []})
84102
}
85103
</script>
86104

src/components/Form/FormCheckbox.vue

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<template>
2+
<label class="custom-control custom-checkbox" :class="{'custom-control-inline': inline}">
3+
<input class="custom-control-input" v-bind="{name, checked: isChecked, disabled, value: inputValue}" @change="onChange" type="checkbox">
4+
<div class="custom-control-label">{{ label }}</div>
5+
</label>
6+
</template>
7+
8+
<script>
9+
import radioCheckMixin from '../../mixins/radioCheck'
10+
import {objectCompare} from '../../utils'
11+
12+
export default {
13+
name: "form-checkbox",
14+
props: {
15+
model: {default: () => ([]), type: Array}
16+
},
17+
methods: {
18+
onChange ({ target: { checked } }) {
19+
let value = this.model
20+
if(checked) {
21+
value.push(this.inputValue)
22+
} else {
23+
// remove current value from selection
24+
value = value.filter(v => {
25+
if (typeof this.inputValue === 'object') {
26+
return !objectCompare(this.inputValue, v)
27+
}
28+
return v !== this.inputValue
29+
})
30+
}
31+
this.$emit('input', value)
32+
}
33+
},
34+
mixins: [radioCheckMixin]
35+
}
36+
</script>
37+
38+
<style scoped>
39+
40+
</style>

src/components/Form/FormLabel.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
export default {
77
name: "form-label",
88
props: {
9-
labelFor: {default: '', type: String},
9+
labelFor: {default: false, type: [Boolean, String]},
1010
label: {default: '', type: String},
1111
labelSmall: {default: '', type: String},
1212
}

src/components/Form/FormRadio.vue

+4-22
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,22 @@
11
<template>
22
<label class="custom-control custom-radio" :class="{'custom-control-inline': inline}">
3-
<input class="custom-control-input" v-bind="{name, checked: isChecked, disabled, value: radioValue}" @change="onChange" type="radio">
3+
<input class="custom-control-input" v-bind="{name, checked: isChecked, disabled, inputValue}" @change="onChange" type="radio">
44
<div class="custom-control-label">{{ label }}</div>
55
</label>
66
</template>
77

88
<script>
9+
import checkboxMixin from '../../mixins/radioCheck'
910
import FormInput from './FormInput'
1011
1112
export default {
1213
name: "form-radio",
13-
props: {
14-
inline: {default: false, type: Boolean},
15-
value: {type: [Boolean, String, Number]},
16-
label: {type: String},
17-
checked: {default: false, type: Boolean},
18-
disabled: {default: false, type: Boolean},
19-
name: {default: '', type: String}
20-
},
21-
22-
computed: {
23-
radioValue () {
24-
return this.$attrs.value
25-
},
26-
27-
isChecked () {
28-
return this.checked || this.radioValue === this.value
29-
}
30-
},
31-
3214
methods: {
3315
onChange () {
34-
this.$emit('input', this.radioValue)
16+
this.$emit('input', this.value)
3517
}
3618
},
37-
19+
mixins: [checkboxMixin],
3820
components: {FormInput}
3921
}
4022
</script>

src/components/Form/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import FormCheckbox from './FormCheckbox'
12
import FormGroup from './FormGroup'
23
import FormInput from './FormInput'
34
import FormInvalidFeedback from './FormInvalidFeedback'
@@ -7,6 +8,7 @@ import FormStaticText from './FormStaticText'
78
import FormTextarea from './FormTextarea'
89

910
export {
11+
FormCheckbox,
1012
FormGroup,
1113
FormInput,
1214
FormInvalidFeedback,

src/mixins/radioCheck.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {objectCompare} from '../utils'
2+
3+
export default {
4+
props: {
5+
inline: {default: false, type: Boolean},
6+
value: {},
7+
label: {type: String},
8+
checked: {default: false, type: Boolean},
9+
disabled: {default: false, type: Boolean},
10+
name: {default: '', type: String},
11+
model: {type: [Array, String, Number]}
12+
},
13+
14+
model: {
15+
prop: 'model',
16+
event: 'input'
17+
},
18+
19+
computed: {
20+
inputValue () {
21+
return this.value || this.label
22+
},
23+
24+
isChecked () {
25+
let checked = this.inputValue === this.model
26+
if(Array.isArray(this.model)) {
27+
checked = !!this.model.find(v => {
28+
if(typeof this.inputValue === 'object') {
29+
return objectCompare(this.inputValue, v)
30+
}
31+
return v === this.inputValue
32+
})
33+
}
34+
return this.checked || checked
35+
},
36+
}
37+
}

src/utils/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Compares if two objects are the same
3+
* todo improve comparing object and add to a specific mixin
4+
* @param object1
5+
* @param object2
6+
* @returns {boolean}
7+
*/
8+
export function objectCompare (object1, object2) {
9+
return JSON.stringify(object1) === JSON.stringify(object2)
10+
}

src/utils/plugins.js

Whitespace-only changes.

0 commit comments

Comments
 (0)