UNPKG

bootstrap-vue

Version:

BootstrapVue, with over 40 plugins and more than 80 custom components, custom directives, and over 300 icons, provides one of the most comprehensive implementations of Bootstrap v4 components and grid system for Vue.js. With extensive and automated WAI-AR

130 lines (119 loc) 3.91 kB
import Vue from '../../utils/vue'; import looseEqual from '../../utils/loose-equal'; import looseIndexOf from '../../utils/loose-index-of'; import { isArray } from '../../utils/inspect'; import formMixin from '../../mixins/form'; import formRadioCheckMixin from '../../mixins/form-radio-check'; import formSizeMixin from '../../mixins/form-size'; import formStateMixin from '../../mixins/form-state'; import idMixin from '../../mixins/id'; // @vue/component export var BFormCheckbox = /*#__PURE__*/ Vue.extend({ name: 'BFormCheckbox', mixins: [formRadioCheckMixin, // Includes shared render function idMixin, formMixin, formSizeMixin, formStateMixin], inject: { bvGroup: { from: 'bvCheckGroup', default: false } }, props: { value: { // type: [String, Number, Boolean, Object], default: true }, uncheckedValue: { // type: [String, Number, Boolean, Object], // Not applicable in multi-check mode default: false }, indeterminate: { // Not applicable in multi-check mode type: Boolean, default: false }, switch: { // Custom switch styling type: Boolean, default: false }, checked: { // v-model (Array when multiple checkboxes have same name) // type: [String, Number, Boolean, Object, Array], default: null } }, computed: { isChecked: function isChecked() { var checked = this.computedLocalChecked; var value = this.value; if (isArray(checked)) { return looseIndexOf(checked, value) > -1; } else { return looseEqual(checked, value); } }, isRadio: function isRadio() { return false; }, isCheck: function isCheck() { return true; } }, watch: { computedLocalChecked: function computedLocalChecked(newVal, oldVal) { this.$emit('input', newVal); if (this.$refs && this.$refs.input) { this.$emit('update:indeterminate', this.$refs.input.indeterminate); } }, indeterminate: function indeterminate(newVal, oldVal) { this.setIndeterminate(newVal); } }, mounted: function mounted() { // Set initial indeterminate state this.setIndeterminate(this.indeterminate); }, methods: { handleChange: function handleChange(_ref) { var _ref$target = _ref.target, checked = _ref$target.checked, indeterminate = _ref$target.indeterminate; var localChecked = this.computedLocalChecked; var value = this.value; var isArr = isArray(localChecked); var uncheckedValue = isArr ? null : this.uncheckedValue; // Update computedLocalChecked if (isArr) { var idx = looseIndexOf(localChecked, value); if (checked && idx < 0) { // Add value to array localChecked = localChecked.concat(value); } else if (!checked && idx > -1) { // Remove value from array localChecked = localChecked.slice(0, idx).concat(localChecked.slice(idx + 1)); } } else { localChecked = checked ? value : uncheckedValue; } this.computedLocalChecked = localChecked; // Change is only emitted on user interaction this.$emit('change', checked ? value : uncheckedValue); // If this is a child of form-checkbox-group, we emit a change event on it as well if (this.isGroup) { this.bvGroup.$emit('change', localChecked); } this.$emit('update:indeterminate', indeterminate); }, setIndeterminate: function setIndeterminate(state) { // Indeterminate only supported in single checkbox mode if (isArray(this.computedLocalChecked)) { state = false; } if (this.$refs && this.$refs.input) { this.$refs.input.indeterminate = state; // Emit update event to prop this.$emit('update:indeterminate', state); } } } });