Skip to content

Commit

Permalink
feat(switch): add foundation as per MDCWeb 0.38
Browse files Browse the repository at this point in the history
also refactor value property for switch and radio
  • Loading branch information
stasson committed Aug 7, 2018
1 parent 4acde2c commit 7251a6c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 24 deletions.
2 changes: 1 addition & 1 deletion components/radio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var vm = new Vue({
|`name`|String || radio group name (__*__) |
|`label`| String|static| radio label |
|`align-end`| Boolean|| align the radio after the label |
|`value`|String| `label`| radio value, defaults to label value if any |
|`value`|String|| input control value |
|`checked`|Boolean|| forces this radio to be selected. follows v-model otherwise |
|`:disabled`| Boolean|| whether the radio is disabled |
|`v-model`| String || tracks selected radio's value |
Expand Down
4 changes: 2 additions & 2 deletions components/radio/mdc-radio.vue
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export default {
this.ripple.init()
this.formField.init()
this.foundation.setValue(this.value ? this.value : this.label)
this.value && this.foundation.setValue(this.value)
this.foundation.setDisabled(this.disabled)
this.foundation.setChecked(
this.checked || this.picked == this.foundation.getValue()
Expand All @@ -124,7 +124,7 @@ export default {
},
methods: {
setChecked(checked) {
this.foundation.setChecked(checked)
this.foundation.setChecked(checked)
},
isChecked() {
return this.foundation.isChecked()
Expand Down
2 changes: 1 addition & 1 deletion components/switch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var vm = new Vue({
|`disabled`| Boolean|| whether the checkbox is disabled |
|`label`| String|| checkbox label |
|`align-end`| Boolean|| align the checkbox after the label |
|`value`|String| `'on'`| checkbox value |
|`value`|String|| input control value |
|`name`|String|| input name |


Expand Down
77 changes: 57 additions & 20 deletions components/switch/mdc-switch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@
class="mdc-switch-wrapper" >

<div
:class="{'mdc-switch--disabled': disabled }"
:class="classes"
:styles="styles"
class="mdc-switch">
<input
ref="control"
:name="name"
:id="vma_uid_"
:checked="checked"
:disabled="disabled"
type="checkbox"
class="mdc-switch__native-control"
@change="onChanged" >
<div class="mdc-switch__track" />
<div class="mdc-switch__thumb-underlay">
<div class="mdc-switch__thumb">
<input
ref="control"
:name="name"
:id="vma_uid_"
:value="value"
type="checkbox"
role="switch"
class="mdc-switch__native-control"
@change="onChanged" >

<div class="mdc-switch__background">
<div class="mdc-switch__knob"/>
</div>
</div>

</div>

<label
Expand All @@ -37,6 +39,8 @@

<script>
import { DispatchFocusMixin, VMAUniqueIdMixin } from '../base'
import { RippleBase } from '../ripple'
import MDCSwitchFoundation from '@material/switch/foundation'
export default {
name: 'mdc-switch',
Expand All @@ -47,24 +51,57 @@ export default {
},
props: {
checked: Boolean,
disabled: Boolean,
value: String,
label: String,
alignEnd: Boolean,
disabled: Boolean,
value: {
type: String,
default() {
return 'on'
}
},
name: String
},
data() {
return {
classes: {},
styles: {}
}
},
computed: {
hasLabel() {
return this.label || this.$slots.default
}
},
watch: {
checked(value) {
this.foundation && this.foundation.setChecked(value)
},
disabled(value) {
this.foundation && this.foundation.setDisabled(value)
}
},
mounted() {
this.foundation = new MDCSwitchFoundation({
addClass: className => this.$set(this.classes, className, true),
removeClass: className => this.$delete(this.classes, className),
setNativeControlChecked: checked =>
(this.$refs.control.checked = checked),
isNativeControlChecked: () => this.$refs.control.checked,
setNativeControlDisabled: disabled =>
(this.$refs.control.disabled = disabled),
isNativeControlDisabled: () => this.nativeControl_.disabled
})
this.foundation.init()
this.foundation.setChecked(this.checked)
this.foundation.setDisabled(this.disabled)
this.ripple = new RippleBase(this)
this.ripple.init()
},
beforeDestroy() {
this.foundation && this.foundation.destroy()
this.ripple && this.ripple.destroy()
},
methods: {
onChanged(event) {
this.foundation && this.foundation.handleChange()
this.$emit('change', event.target.checked)
}
}
Expand Down

0 comments on commit 7251a6c

Please sign in to comment.