From 3bf65470fba0ef6c3ba0e136b7deefb19507d3da Mon Sep 17 00:00:00 2001 From: stasson Date: Sun, 7 Jan 2018 11:40:21 +0100 Subject: [PATCH] feat(checkbox): allow custom label markup also refactor using formfield foundation closes #185 --- components/checkbox/README.md | 10 ++++++ components/checkbox/mdc-checkbox.vue | 51 +++++++++++++++++++--------- components/unit-test/index.js | 2 ++ 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/components/checkbox/README.md b/components/checkbox/README.md index e233099e..fecaf4d0 100644 --- a/components/checkbox/README.md +++ b/components/checkbox/README.md @@ -30,6 +30,16 @@ var vm = new Vue({ }) ``` +### Custom label markup + +```html + + I agree with + Terms of Serivce + +``` + +> use `@click.stop` to prevent triggering checkbox ripple ### props diff --git a/components/checkbox/mdc-checkbox.vue b/components/checkbox/mdc-checkbox.vue index 570972e3..83800291 100644 --- a/components/checkbox/mdc-checkbox.vue +++ b/components/checkbox/mdc-checkbox.vue @@ -16,9 +16,8 @@
-
- -
+ @@ -26,6 +25,7 @@ /* global HTMLElement */ import MDCCheckboxFoundation from '@material/checkbox/foundation' +import MDCFormFieldFoundation from '@material/form-field/foundation' import {getCorrectEventName} from '@material/animation' import {DispatchFocusMixin} from '../base' import {RippleBase} from '../ripple' @@ -52,10 +52,13 @@ export default { } }, computed: { + hasLabel () { + return this.label || this.$slots.default + }, formFieldClasses () { return { - 'mdc-form-field': this.label, - 'mdc-form-field--align-end': this.label && this.alignEnd + 'mdc-form-field': this.hasLabel, + 'mdc-form-field--align-end': this.hasLabel && this.alignEnd } } }, @@ -83,28 +86,21 @@ export default { registerChangeHandler: (handler) => this.$refs.control.addEventListener('change', handler), deregisterChangeHandler: (handler) => this.$refs.control.removeEventListener('change', handler), getNativeControl: () => this.$refs.control, - forceLayout: () => this.$forceUpdate(), + forceLayout: () => this.$refs.root.offsetWidth, isAttachedToDOM: () => Boolean(this.$el.parentNode) }) - this.foundation.init() - this.foundation.setChecked(this.checked) - this.foundation.setDisabled(this.disabled) - this.foundation.setIndeterminate(this.indeterminate) - this.ripple = new RippleBase(this, { isUnbounded: () => true, isSurfaceActive: () => RippleBase.isSurfaceActive(this.$refs.control), registerInteractionHandler: (evt, handler) => { - this.$refs.root.addEventListener(evt, handler) - this.$refs.label.addEventListener(evt, handler) + this.$refs.control.addEventListener(evt, handler) }, deregisterInteractionHandler: (evt, handler) => { - this.$refs.root.removeEventListener(evt, handler) - this.$refs.label.removeEventListener(evt, handler) + this.$refs.control.addEventListener(evt, handler) }, computeBoundingRect: () => { - const {left, top} = this.$refs.control.getBoundingClientRect() + const {left, top} = this.$refs.root.getBoundingClientRect() const DIM = 40 return { top, @@ -116,9 +112,32 @@ export default { } } }) + + this.formField = new MDCFormFieldFoundation({ + registerInteractionHandler: (type, handler) => { + this.$refs.label.addEventListener(type, handler) + }, + deregisterInteractionHandler: (type, handler) => { + this.$refs.label.removeEventListener(type, handler) + }, + activateInputRipple: () => { + this.ripple && this.ripple.activate() + }, + deactivateInputRipple: () => { + this.ripple && this.ripple.deactivate() + }, + }); + + this.foundation.init() this.ripple.init() + this.formField.init() + this.foundation.setChecked(this.checked) + this.foundation.setDisabled(this.disabled) + this.foundation.setIndeterminate(this.indeterminate) + }, beforeDestroy () { + this.formField.destroy() this.ripple.destroy() this.foundation.destroy() }, diff --git a/components/unit-test/index.js b/components/unit-test/index.js index 854217de..ea344aa5 100644 --- a/components/unit-test/index.js +++ b/components/unit-test/index.js @@ -46,6 +46,8 @@ export function checkValidMdcAdapter(vm) { vm.ripple && checkValidFoundation(vm.ripple) vm.foundation && checkValidFoundation(vm.foundation) + + vm.formField && checkValidFoundation(vm.formField) } export function checkValidOptions(options) {