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

Commit de7d7b0

Browse files
committed
add FormCheckbox component
1 parent 61d9867 commit de7d7b0

File tree

8 files changed

+134
-27
lines changed

8 files changed

+134
-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

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

+8-22
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,26 @@
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"
4+
v-bind="{name, checked: isChecked, disabled, inputValue}" @change="onChange"
5+
@focus="onFocus"
6+
@blur="onBlur"
7+
type="radio">
48
<div class="custom-control-label">{{ label }}</div>
59
</label>
610
</template>
711

812
<script>
13+
import checkboxMixin from '../../mixins/radioCheck'
914
import FormInput from './FormInput'
1015
1116
export default {
1217
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-
3218
methods: {
3319
onChange () {
34-
this.$emit('input', this.radioValue)
20+
this.$emit('input', this.value)
3521
}
3622
},
37-
23+
mixins: [checkboxMixin],
3824
components: {FormInput}
3925
}
4026
</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

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
38+
methods: {
39+
onFocus () {
40+
this.$emit('focus')
41+
},
42+
onBlur () {
43+
this.$emit('blur')
44+
}
45+
}
46+
}

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)