UNPKG

@progress/kendo-vue-inputs

Version:
286 lines (285 loc) 8.82 kB
/** * @license *------------------------------------------------------------------------------------------- * Copyright © 2025 Progress Software Corporation. All rights reserved. * Licensed under commercial license. See LICENSE.md in the package root for more information *------------------------------------------------------------------------------------------- */ import { defineComponent as x, ref as B, inject as D, h as M, createVNode as l } from "vue"; import { validatePackage as O, guid as P, getRef as f, getDefaultSlots as z, classNames as m, kendoThemeMaps as g, templateRendering as L, getListeners as w, getTemplate as I, Keys as H, setRef as T, getTabIndex as N } from "@progress/kendo-vue-common"; import { packageMetadata as R } from "../package-metadata.mjs"; import { provideLocalizationService as q } from "@progress/kendo-vue-intl"; import { checkboxValidation as K, checkboxOptionalText as j, messages as A } from "../messages/main.mjs"; const U = /* @__PURE__ */ x({ name: "KendoCheckbox", emits: { changemodel: null, "update:modelValue": null, change: null, focus: null, blur: null }, model: { event: "changemodel" }, inject: { kendoLocalizationService: { default: null } }, props: { checked: { type: Boolean, default: void 0 }, defaultChecked: { type: Boolean, default: void 0 }, defaultValue: { type: [String, Boolean], default: void 0 }, modelValue: { type: [String, Boolean], default: void 0 }, dir: String, disabled: Boolean, id: String, ariaLabelledBy: String, ariaDescribedBy: String, label: String, labelRender: [String, Number, Boolean, Object], labelPlacement: String, labelOptional: Boolean, labelClass: String, name: String, size: { type: String, default: "medium", validator: function(e) { return [null, "small", "medium", "large"].includes(e); } }, rounded: { type: String, default: "medium", validator: function(e) { return [null, "small", "medium", "large"].includes(e); } }, tabIndex: Number, value: { type: [String, Boolean], default: void 0 }, validationMessage: String, required: Boolean, valid: { type: Boolean, default: void 0 }, validityStyles: { type: Boolean, default: !0 } }, data() { return { valueDuringOnChange: void 0, currentDir: "ltr", currentChecked: void 0, currentValue: void 0 }; }, created() { O(R), this.calculatedId = P(), this.$props.defaultChecked !== void 0 && (this.currentChecked = this.$props.defaultChecked), this.$props.defaultValue !== void 0 && (this.currentValue = this.$props.defaultValue), this.currentDir = this.$props.dir; }, computed: { valueIsBooleanOrNull() { const e = this.$props.value; return typeof e == "boolean" || e === null; }, isCheckedControlled() { return this.$props.checked !== void 0; }, isValueControlled() { return this.$props.value !== void 0 && this.valueIsBooleanOrNull; }, computedValue() { return this.$data.valueDuringOnChange !== void 0 ? this.$data.valueDuringOnChange : this.$props.value !== void 0 ? this.$props.value : this.$props.modelValue !== void 0 ? this.$props.modelValue : this.$data.currentValue; }, computedChecked() { return this.$data.valueDuringOnChange !== void 0 ? this.$data.valueDuringOnChange : this.$props.checked !== void 0 ? this.$props.checked : this.$props.modelValue !== void 0 ? this.$props.modelValue : this.$data.currentChecked; }, useValueAsChecked() { return this.computedChecked === void 0 && this.computedValue; }, checkedProp() { return this.useValueAsChecked ? this.computedValue : this.computedChecked; }, valueProp() { const e = this.$props.value; return this.useValueAsChecked || this.isValueControlled ? e === null ? e : void 0 : e || this.computedValue; }, indeterminateProp() { return this.checkedProp === null || this.valueProp === null; }, isValid() { const e = this.$props.valid; return e !== void 0 ? e : this.$props.required ? !!this.computedChecked : !0; }, labelComputedClass() { return { "k-checkbox-label": !0, [this.$props.labelClass]: this.$props.labelClass }; } }, mounted() { if (this.input = f(this, "input"), !this.currentDir && window && this.$el) { const e = window.getComputedStyle(this.$el).direction; e && (this.currentDir = e); } this.setValidity(); }, updated() { this.input || (this.input = f(this, "input")), this.setValidity(); }, setup() { const e = B(null), t = D("kendoLocalizationService", {}); return { inputRef: e, kendoLocalizationService: t }; }, render() { const { ariaDescribedBy: e, ariaLabelledBy: t, disabled: i, id: s, label: b, labelRender: n, labelPlacement: k, name: $, labelOptional: C, tabIndex: V, required: d, validityStyles: v, size: r, rounded: u } = this.$props, o = z(this); let a = b; this.localizationService = q(this), this.defaultValidationMessage = this.localizeMessage(K), this.optionalMessage = this.localizeMessage(j); const h = m({ "k-checkbox-wrap": !0, "k-disabled": i }), y = m({ "k-checkbox": !0, [`k-checkbox-${g.sizeMap[r]}`]: r, [`k-rounded-${g.roundedMap[u]}`]: u, "k-indeterminate": this.indeterminateProp, "k-disabled": i, "k-invalid": !this.isValid && v !== !1 }), c = function() { return l("input", { type: "checkbox", class: y, name: $, id: s || this.calculatedId, ref: T(this, "input"), "aria-labelledby": t, "aria-describedby": e, checked: !!this.checkedProp, disabled: i, tabindex: N(V, i), role: "checkbox", required: d !== void 0 ? d : !1, "aria-checked": this.computedChecked || this.checkedProp ? !0 : this.indeterminateProp ? "mixed" : !1, "aria-disabled": i || void 0, onChange: this.onChangeHandler, onKeydown: this.onKeyDownHandler, onFocus: this.onFocusHandler, onBlur: this.onBlurHandler }, null); }; if (n) { const S = n ? L.call(this, n, w.call(this)) : null; a = I.call(this, { h: M, template: S }); } const p = function() { return a !== void 0 ? l("label", { class: this.labelComputedClass, for: s || this.calculatedId, style: { userSelect: "none" } }, [a, C && l("span", { class: "k-label-optional" }, [this.optionalMessage])]) : null; }; return k === "before" ? l("span", { class: h, dir: "rtl" }, [c.call(this), p.call(this), o]) : l("span", { class: h, dir: this.currentDir }, [c.call(this), p.call(this), o]); }, methods: { setValidity() { const e = this.$props.valid !== void 0 ? this.$props.valid : this.$props.required ? !!this.computedChecked : !0; this.input && this.input.setCustomValidity && this.input.setCustomValidity(e ? "" : this.$props.validationMessage || this.defaultValidationMessage); }, localizeMessage(e) { return this.localizationService.toLanguageString(e, A[e]); }, focusElement() { this.input && this.input.focus(); }, setValue(e, t) { this.$data.valueDuringOnChange = t, this.$nextTick(() => { if (!this.isCheckedControlled && !this.isValueControlled && !this.$props.disabled && (this.currentValue = t, this.currentChecked = t), !this.$props.disabled) { const i = { element: this.$el, focus: null // focusElement }; this.$emit("changemodel", t), this.$emit("update:modelValue", t), this.$emit("change", { e, handle: i, value: t }); } this.$data.valueDuringOnChange = void 0; }); }, onChangeHandler(e) { let t = e.target.checked; this.setValue(e, t); }, onKeyDownHandler(e) { if (this.$props.disabled) return; const { keyCode: t } = e; let i = e.currentTarget.checked; t === H.space && (e.preventDefault(), e.stopPropagation(), this.setValue(e, !i)); }, onBlurHandler(e) { this.$props.disabled || this.$emit("blur", e); }, onFocusHandler(e) { this.$props.disabled || this.$emit("focus", e); } } }); export { U as Checkbox };