@volverjs/ui-vue
Version:
@volverjs/ui-vue is a lightweight Vue 3 component library to accompany @volverjs/style.
2 lines (1 loc) • 11.8 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("vue"),require("@vueuse/core"),require("dot-prop")):"function"==typeof define&&define.amd?define(["vue","@vueuse/core","dot-prop"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).VvCheckboxGroup=t(e.vue,e.core,e.dotProp)}(this,function(e,t,l){"use strict";var a=(e=>(e.local="local",e.session="session",e))(a||{}),o=(e=>(e.left="left",e.right="right",e.top="top",e.bottom="bottom",e))(o||{}),n=(e=>(e.before="before",e.after="after",e))(n||{}),i=(e=>(e.button="button",e.submit="submit",e.reset="reset",e))(i||{}),r=(e=>(e.nuxtLink="nuxt-link",e.routerLink="router-link",e.a="a",e.button="button",e))(r||{});const u=Symbol.for("volver"),d=Symbol.for("checkGroup"),s={valid:{type:Boolean,default:!1},validLabel:{type:[String,Array],default:void 0}},v={invalid:{type:Boolean,default:!1},invalidLabel:{type:[String,Array],default:void 0}},c={loading:{type:Boolean,default:!1},loadingLabel:{type:String,default:"Loading..."}},f={disabled:{type:Boolean,default:!1}},p={required:{type:Boolean,default:!1}},b={label:{type:[String,Number],default:void 0}},m={readonly:{type:Boolean,default:!1}},y={modifiers:{type:[String,Array],default:void 0}},h={hintLabel:{type:String,default:""}},g={options:{type:Array,default:()=>[]},labelKey:{type:[String,Function],default:"label"},valueKey:{type:[String,Function],default:"value"},disabledKey:{type:[String,Function],default:"disabled"}};n.before;const S={tabindex:{type:[String,Number],default:0}},k={id:[String,Number]};o.bottom;const L={...{...k,name:{type:String,required:!0}},...S,...s,...v,...h,...f,...m,...y,...b,...c,value:{type:[String,Number,Boolean],default:void 0},modelValue:{type:[Object,Number,Boolean,String],default:void 0}},V={...s,...v,...g,...h,...f,...m,...y,...b,...c,...p,modelValue:{type:[String,Array,Boolean,Number,Symbol],default:void 0},name:{type:String,required:!0},vertical:{type:Boolean,default:!1}};i.button,r.button,a.local;const x=V;function O(e,t,l){return $(e,t)}function $(e,t){if(e===t)return!0;if(e&&t&&"object"==typeof e&&"object"==typeof t){const l=Array.isArray(e),a=Array.isArray(t);let o,n,i;if(l&&a){if(n=e.length,n!==t.length)return!1;for(o=n;0!==o--;)if(!$(e[o],t[o]))return!1;return!0}if(l!==a)return!1;const r=e instanceof Date,u=t instanceof Date;if(r!==u)return!1;if(r&&u)return e.getTime()===t.getTime();const d=e instanceof RegExp,s=t instanceof RegExp;if(d!==s)return!1;if(d&&s)return e.toString()===t.toString();const v=Object.keys(e);if(n=v.length,n!==Object.keys(t).length)return!1;for(o=n;0!==o--;)if(!Object.prototype.hasOwnProperty.call(t,v[o]))return!1;for(o=n;0!==o--;)if(i=v[o],!$(e[i],t[i]))return!1;return!0}return Number.isNaN(e)&&Number.isNaN(t)}function B(e){return Array.isArray(e)?e.filter(e=>"string"==typeof e).join(" "):e}function A(t,l){const a=e.computed(()=>e.isRef(t)?t.value:t),o=e.computed(()=>B(a.value.invalidLabel)),n=e.computed(()=>B(a.value.validLabel)),i=e.computed(()=>a.value.loadingLabel),r=e.computed(()=>a.value.hintLabel),u=e.computed(()=>Boolean(a.value.loading&&(l.loading||i.value))),d=e.computed(()=>!u.value&&Boolean(a.value.invalid&&(l.invalid||o.value))),s=e.computed(()=>!u.value&&!d.value&&Boolean(a.value.valid&&(l.valid||n.value))),v=e.computed(()=>!u.value&&!d.value&&!s.value&&Boolean(l.hint||r.value)),c=e.computed(()=>d.value||s.value||u.value||v.value),f=e.computed(()=>({modelValue:a.value.modelValue,valid:a.value.valid,invalid:a.value.invalid,loading:a.value.loading})),p=e.defineComponent({name:"HintSlot",props:{tag:{type:String,default:"small"}},setup:()=>({isVisible:c,invalidLabel:o,validLabel:n,loadingLabel:i,hintLabel:r,hasInvalidLabelOrSlot:d,hasValidLabelOrSlot:s,hasLoadingLabelOrSlot:u,hasHintLabelOrSlot:v}),render(){if(this.isVisible){let t;return this.hasInvalidLabelOrSlot&&(t="alert"),this.hasValidLabelOrSlot&&(t="status"),this.hasLoadingLabelOrSlot?e.h(this.tag,{role:t},this.$slots.loading?.()??this.loadingLabel):this.hasInvalidLabelOrSlot?e.h(this.tag,{role:t},this.$slots.invalid?.()??this.$slots.invalid??this.invalidLabel):this.hasValidLabelOrSlot?e.h(this.tag,{role:t},this.$slots.valid?.()??this.validLabel):e.h(this.tag,{role:t},this.$slots.hint?.()??this.$slots.hint??this.hintLabel)}return null}});return{hasInvalidLabelOrSlot:d,hasHintLabelOrSlot:v,hasValidLabelOrSlot:s,hasLoadingLabelOrSlot:u,hintSlotScope:f,HintSlot:p}}const C={...L,...y,indeterminate:Boolean,uncheckedValue:[String,Number,Boolean],switch:Boolean};function P(t,l){const{group:a,isInGroup:o,getGroupOrLocalRef:n}=function(t){const l=e.inject(t,void 0),a=e.computed(()=>void 0!==l);return{group:l,isInGroup:a,getGroupOrLocalRef:function(t,a,o){const n=l?.[t];if(n)return e.computed({get:()=>n.value,set(e){n.value=e}});const i=e.toRef(a,t);return e.computed({get:()=>i.value,set(e){o&&o(`update:${t}`,e)}})}}}(d),{id:i,switch:r,indeterminate:u}=e.toRefs(t),s=n("modelValue",t,l),v=n("valid",t),c=n("invalid",t),f=e.computed(()=>Boolean(t.readonly||a?.readonly.value)),p=e.computed(()=>Boolean(t.disabled||a?.disabled.value));return{id:i,propsSwitch:r,indeterminate:u,group:a,isInGroup:o,modelValue:s,valid:v,invalid:c,readonly:f,disabled:p}}function N(t,l,a){const o=e.inject(u,void 0),n=e.computed(()=>{if(o&&o.defaults.value?.[t])return o.defaults.value[t]});return e.computed(()=>{if(void 0===n.value)return a;const e=n.value,t=l,o=a;return Object.keys(t).reduce((l,a)=>{const n=o[a];if(l[a]=n,a in e){if(Array.isArray(t[a])){const o=t[a];if(o.length){o[0]===n&&(l[a]=e[a])}}if("function"==typeof t[a]){(0,t[a])()===n&&(l[a]=e[a])}if("object"==typeof t[a]){let o=t[a].default;"function"==typeof o&&(o=o()),"object"==typeof o?JSON.stringify(o)===JSON.stringify(n)&&(l[a]=e[a]):o===n&&(l[a]=e[a])}}return l},{})})}function w(t,l,a){return e.computed(()=>{const o={[t]:!0},n="string"==typeof l?.value?l.value.split(" "):l?.value;return n&&Array.isArray(n)&&n.forEach(e=>{e&&(o[`${t}--${e}`]=!0)}),a&&Object.keys(a.value).forEach(l=>{o[`${t}--${l}`]=e.unref(a.value[l])}),o})}const R=["for"],j=["id","name","disabled","value","tabindex","aria-invalid","aria-describedby","aria-errormessage"],_=e.defineComponent({name:"VvCheckbox",props:C,emits:["click","update:modelValue","change","blur"],setup(t,{emit:l}){const a=t,o=l,n=e.useSlots(),i=N("VvCheckbox",C,a),{id:r,disabled:u,readonly:d,valid:s,invalid:v,propsSwitch:c,modelValue:f,indeterminate:p,isInGroup:b}=P(a,o),m=function(t){return e.computed(()=>String(t?.value||e.useId()))}(r),y=e.computed(()=>`${m.value}-hint`),h=e.computed(()=>k.value?-1:a.tabindex),g=e.ref(),S=e.computed(()=>void 0!==a.uncheckedValue&&!b.value),k=e.computed(()=>u.value||d.value),L=e.computed(()=>!0===v.value||!0!==s.value&&void 0),V=e.computed(()=>S.value?f.value===a.value:Array.isArray(f.value)?function(e,t){if(null!=e&&t&&t.length)for(const l of t)if(O(e,l))return!0;return!1}(a.value,f.value):O(a.value,f.value)),x=e.computed(()=>!!p.value||!(V.value||!S.value||a.uncheckedValue===f.value)),$=e.computed(()=>{if(!S.value)return!["string","number","boolean"].includes(typeof a.value)||a.value}),B=e.computed({get:()=>V.value,set(e){if(S.value)f.value=e?a.value:a.uncheckedValue;else if(Array.isArray(f.value)||b.value){const t=f.value??[],l=new Set(Array.isArray(t)?t:[t]);e?l.add(a.value):l.delete(a.value),f.value=[...l]}else f.value=e?a.value:void 0;o("change",e)}}),{modifiers:_}=e.toRefs(a),z=w("vv-checkbox",_,e.computed(()=>({switch:c.value,valid:s.value,invalid:v.value,disabled:u.value,readonly:d.value,indeterminate:p.value})));e.watchEffect(()=>{S.value&&Array.isArray(f.value)&&console.warn("[VvCheckbox] The model value is an array but the component is in binary mode.")}),e.watch(()=>x.value,e=>{g.value.indeterminate=!!e}),e.onMounted(()=>{x.value&&(g.value.indeterminate=!0)});const{HintSlot:E,hasHintLabelOrSlot:G,hasInvalidLabelOrSlot:q,hintSlotScope:I}=A(i,n);return(t,l)=>(e.openBlock(),e.createElementBlock("label",{class:e.normalizeClass(e.unref(z)),for:e.unref(m)},[e.withDirectives(e.createElementVNode("input",{id:e.unref(m),ref_key:"input",ref:g,"onUpdate:modelValue":l[0]||(l[0]=t=>e.isRef(B)?B.value=t:null),type:"checkbox",class:"vv-checkbox__input",name:t.name,disabled:e.unref(k),value:e.unref($),tabindex:e.unref(h),"aria-invalid":e.unref(L),"aria-describedby":e.unref(G)?e.unref(y):void 0,"aria-errormessage":e.unref(q)?e.unref(y):void 0},null,8,j),[[e.vModelCheckbox,e.unref(B)]]),e.renderSlot(t.$slots,"default",{value:e.unref(f)},()=>[e.createTextVNode(e.toDisplayString(t.label),1)]),e.createVNode(e.unref(E),{id:e.unref(y),class:"vv-checkbox__hint"},e.createSlots({_:2},[t.$slots.hint?{name:"hint",fn:e.withCtx(()=>[e.renderSlot(t.$slots,"hint",e.normalizeProps(e.guardReactiveProps(e.unref(I))))]),key:"0"}:void 0,t.$slots.loading?{name:"loading",fn:e.withCtx(()=>[e.renderSlot(t.$slots,"loading",e.normalizeProps(e.guardReactiveProps(e.unref(I))))]),key:"1"}:void 0,t.$slots.valid?{name:"valid",fn:e.withCtx(()=>[e.renderSlot(t.$slots,"valid",e.normalizeProps(e.guardReactiveProps(e.unref(I))))]),key:"2"}:void 0,t.$slots.invalid?{name:"invalid",fn:e.withCtx(()=>[e.renderSlot(t.$slots,"invalid",e.normalizeProps(e.guardReactiveProps(e.unref(I))))]),key:"3"}:void 0]),1032,["id"])],10,R))}});const z=["textContent"],E={class:"vv-checkbox-group__wrapper"};return e.defineComponent({name:"VvCheckboxGroup",props:x,emits:["update:modelValue","change"],setup(a,{emit:o}){const n=a,i=o,r=e.useSlots(),u=N("VvCheckboxGroup",x,n),s=t.useVModel(n,"modelValue",i),{disabled:v,readonly:c,vertical:f,valid:p,invalid:b,modifiers:m}=e.toRefs(n);var y,h;y=d,h={modelValue:s,disabled:v,readonly:c,valid:p,invalid:b},e.provide(y,h);const{getOptionLabel:g,getOptionValue:S,isOptionDisabled:k}=function(t){const{options:a,labelKey:o,valueKey:n,disabledKey:i}=e.toRefs(t);return{options:a,getOptionLabel:e=>"string"==typeof e?e:"function"==typeof o.value?o.value(e):String(o.value?l.getProperty(e,o.value):e),getOptionValue:e=>"string"==typeof e?e:"function"==typeof n.value?n.value(e):n.value?l.getProperty(e,n.value):e,isOptionDisabled:e=>"string"!=typeof e&&("function"==typeof i.value?i.value(e):!!i.value&&Boolean(l.getProperty(e,i.value))),getOptionGrouped:e=>"string"==typeof e?[]:"object"==typeof e&&e&&"options"in e?e.options:[]}}(n),L=w("vv-checkbox-group",m,e.computed(()=>({disabled:v.value,readonly:c.value,required:n.required,horizontal:!f.value,valid:p.value,invalid:b.value})));const{HintSlot:V,hintSlotScope:O}=A(u,r);return(t,l)=>(e.openBlock(),e.createElementBlock("fieldset",{class:e.normalizeClass(e.unref(L))},[t.label?(e.openBlock(),e.createElementBlock("legend",{key:0,textContent:e.toDisplayString(t.label)},null,8,z)):e.createCommentVNode("v-if",!0),e.createElementVNode("div",E,[t.options.length>0?(e.openBlock(!0),e.createElementBlock(e.Fragment,{key:0},e.renderList(t.options,(t,l)=>(e.openBlock(),e.createBlock(_,e.mergeProps({key:l},{ref_for:!0},function(e,t){return{id:`${n.name}_opt${t}`,name:n.name,label:g(e),value:S(e),required:n.required,disabled:k(e)}}(t,l)),null,16))),128)):e.renderSlot(t.$slots,"default",{key:1})]),e.createVNode(e.unref(V),{class:"vv-checkbox-group__hint"},e.createSlots({_:2},[t.$slots.hint?{name:"hint",fn:e.withCtx(()=>[e.renderSlot(t.$slots,"hint",e.normalizeProps(e.guardReactiveProps(e.unref(O))))]),key:"0"}:void 0,t.$slots.loading?{name:"loading",fn:e.withCtx(()=>[e.renderSlot(t.$slots,"loading",e.normalizeProps(e.guardReactiveProps(e.unref(O))))]),key:"1"}:void 0,t.$slots.valid?{name:"valid",fn:e.withCtx(()=>[e.renderSlot(t.$slots,"valid",e.normalizeProps(e.guardReactiveProps(e.unref(O))))]),key:"2"}:void 0,t.$slots.invalid?{name:"invalid",fn:e.withCtx(()=>[e.renderSlot(t.$slots,"invalid",e.normalizeProps(e.guardReactiveProps(e.unref(O))))]),key:"3"}:void 0]),1024)],2))}})});