Skip to content

Commit 3bf6547

Browse files
committed
feat(checkbox): allow custom label markup
also refactor using formfield foundation closes #185
1 parent 014df1f commit 3bf6547

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed

components/checkbox/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ var vm = new Vue({
3030
})
3131
```
3232

33+
### Custom label markup
34+
35+
```html
36+
<mdc-checkbox v-model="agreed">
37+
<span>I agree with </span>
38+
<a @click.stop href="...">Terms of Serivce</a>
39+
</mdc-checkbox>
40+
```
41+
42+
> use `@click.stop` to prevent triggering checkbox ripple
3343
3444
### props
3545

components/checkbox/mdc-checkbox.vue

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616
<div class="mdc-checkbox__mixedmark"></div>
1717
</div>
1818
</div>
19-
<div ref="label">
20-
<label :for="_uid" v-if="label">{{ label }}</label>
21-
</div>
19+
<label ref="label" :for="_uid"
20+
><slot>{{label}}</slot></label>
2221
</div>
2322
</template>
2423

2524
<script>
2625
2726
/* global HTMLElement */
2827
import MDCCheckboxFoundation from '@material/checkbox/foundation'
28+
import MDCFormFieldFoundation from '@material/form-field/foundation'
2929
import {getCorrectEventName} from '@material/animation'
3030
import {DispatchFocusMixin} from '../base'
3131
import {RippleBase} from '../ripple'
@@ -52,10 +52,13 @@ export default {
5252
}
5353
},
5454
computed: {
55+
hasLabel () {
56+
return this.label || this.$slots.default
57+
},
5558
formFieldClasses () {
5659
return {
57-
'mdc-form-field': this.label,
58-
'mdc-form-field--align-end': this.label && this.alignEnd
60+
'mdc-form-field': this.hasLabel,
61+
'mdc-form-field--align-end': this.hasLabel && this.alignEnd
5962
}
6063
}
6164
},
@@ -83,28 +86,21 @@ export default {
8386
registerChangeHandler: (handler) => this.$refs.control.addEventListener('change', handler),
8487
deregisterChangeHandler: (handler) => this.$refs.control.removeEventListener('change', handler),
8588
getNativeControl: () => this.$refs.control,
86-
forceLayout: () => this.$forceUpdate(),
89+
forceLayout: () => this.$refs.root.offsetWidth,
8790
isAttachedToDOM: () => Boolean(this.$el.parentNode)
8891
})
8992
90-
this.foundation.init()
91-
this.foundation.setChecked(this.checked)
92-
this.foundation.setDisabled(this.disabled)
93-
this.foundation.setIndeterminate(this.indeterminate)
94-
9593
this.ripple = new RippleBase(this, {
9694
isUnbounded: () => true,
9795
isSurfaceActive: () => RippleBase.isSurfaceActive(this.$refs.control),
9896
registerInteractionHandler: (evt, handler) => {
99-
this.$refs.root.addEventListener(evt, handler)
100-
this.$refs.label.addEventListener(evt, handler)
97+
this.$refs.control.addEventListener(evt, handler)
10198
},
10299
deregisterInteractionHandler: (evt, handler) => {
103-
this.$refs.root.removeEventListener(evt, handler)
104-
this.$refs.label.removeEventListener(evt, handler)
100+
this.$refs.control.addEventListener(evt, handler)
105101
},
106102
computeBoundingRect: () => {
107-
const {left, top} = this.$refs.control.getBoundingClientRect()
103+
const {left, top} = this.$refs.root.getBoundingClientRect()
108104
const DIM = 40
109105
return {
110106
top,
@@ -116,9 +112,32 @@ export default {
116112
}
117113
}
118114
})
115+
116+
this.formField = new MDCFormFieldFoundation({
117+
registerInteractionHandler: (type, handler) => {
118+
this.$refs.label.addEventListener(type, handler)
119+
},
120+
deregisterInteractionHandler: (type, handler) => {
121+
this.$refs.label.removeEventListener(type, handler)
122+
},
123+
activateInputRipple: () => {
124+
this.ripple && this.ripple.activate()
125+
},
126+
deactivateInputRipple: () => {
127+
this.ripple && this.ripple.deactivate()
128+
},
129+
});
130+
131+
this.foundation.init()
119132
this.ripple.init()
133+
this.formField.init()
134+
this.foundation.setChecked(this.checked)
135+
this.foundation.setDisabled(this.disabled)
136+
this.foundation.setIndeterminate(this.indeterminate)
137+
120138
},
121139
beforeDestroy () {
140+
this.formField.destroy()
122141
this.ripple.destroy()
123142
this.foundation.destroy()
124143
},

components/unit-test/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export function checkValidMdcAdapter(vm) {
4646
vm.ripple && checkValidFoundation(vm.ripple)
4747

4848
vm.foundation && checkValidFoundation(vm.foundation)
49+
50+
vm.formField && checkValidFoundation(vm.formField)
4951
}
5052

5153
export function checkValidOptions(options) {

0 commit comments

Comments
 (0)