@public-ui/components
Version:
Contains all web components that belong to KoliBri - The accessible HTML-Standard.
4 lines • 11.8 kB
JavaScript
/*!
* KoliBri - The accessible HTML-Standard
*/
import{h,Fragment,proxyCustomElement,HTMLElement}from"@stencil/core/internal/client";import{c as clsx}from"./clsx.js";import{c as checkHasMsg,d as getDefaultProps,g as getRenderStates,F as FormFieldStateWrapper}from"./controller2.js";import{I as InputIconController,a as InputContainerStateWrapperFc}from"./controller-icon.js";import{_ as __rest}from"./tslib.es6.js";import{q as watchJsonArrayString}from"./common.js";import{n as nonce}from"./dev.utils.js";import{p as propagateSubmitEventToForm}from"./controller.js";import{v as validateMultiple}from"./multiple.js";import{f as fillKeyOptionMap,v as validateOptionsWithOptgroup}from"./controller3.js";import{v as validateRequired}from"./required.js";import{v as validateRows}from"./rows.js";const NativeOptionFc=e=>{var{baseClassName:t="kol-select",class:a,index:i,selectedValue:l,selected:s,value:o,label:r,disabled:n}=e,d=__rest(e,["baseClassName","class","index","selectedValue","selected","value","label","disabled"]);void 0===l?l=[]:Array.isArray(l)||(l=[l]);const c=s||l.includes(o);return h("option",Object.assign({class:clsx(`${t}__option`,{[`${t}__option--selected`]:c,[`${t}__option--disabled`]:n},a),selected:c,disabled:n,value:i},d),r)},NativeOptionListFc=({baseClassName:e,preKey:t,options:a,disabled:i,value:l,OptionProps:s={},OptionGroupProps:o={}})=>(null==a?void 0:a.length)?h(Fragment,null,a.map((r,n)=>{const d=[t,`-${n}`].join("");if("options"in r){if(!a.length)return null;const{label:t}=r,n=__rest(r,["label"]);return h("optgroup",Object.assign({class:clsx(`${e}__optgroup`,{[`${e}__optgroup--disabled`]:i}),key:d},o,{label:null==t?void 0:t.toString(),disabled:i}),h(NativeOptionListFc,Object.assign({baseClassName:e,OptionGroupProps:o,OptionProps:s,value:l,preKey:d},n)))}return"value"in r?h(NativeOptionFc,Object.assign({key:d,baseClassName:e},s,{index:d,selectedValue:l},r)):null})):null,NativeSelectFc=e=>{const{class:t,msg:a,touched:i,disabled:l,required:s,options:o,value:r,OptionProps:n,OptionGroupProps:d,ariaDescribedBy:c,hideLabel:u,label:p}=e,v=__rest(e,["class","msg","touched","disabled","required","options","value","OptionProps","OptionGroupProps","ariaDescribedBy","hideLabel","label"]),_=checkHasMsg(a,i),m="string"==typeof a?"error":null==a?void 0:a._type,y={"kol-select--disabled":Boolean(l),"kol-select--required":Boolean(s),"kol-select--touched":Boolean(i),[`kol-select--${m||"error"}`]:_},b=Object.assign(Object.assign({class:clsx("kol-select",y,t),required:s,disabled:l},getDefaultProps({ariaDescribedBy:c,hideLabel:u,label:p})),v);return h("select",Object.assign({},b),h(NativeOptionListFc,{baseClassName:"kol-select",options:o,value:r,OptionGroupProps:d,OptionProps:n}))};function getSelectProps(e){const{ariaDescribedBy:t}=getRenderStates(e),a={id:e._id,hideLabel:e._hideLabel,label:e._label,value:e._value,options:e._options,accessKey:e._accessKey,disabled:e._disabled,name:e._name,ariaDescribedBy:t,size:e._rows,multiple:e._multiple,required:e._required,touched:e._touched,msg:e._msg};return"_shortKey"in e&&(a["aria-keyshortcuts"]=e._shortKey),a}const SelectStateWrapper=e=>{var{state:t}=e,a=__rest(e,["state"]);return h(NativeSelectFc,Object.assign({},getSelectProps(t),a))};class SelectController extends InputIconController{constructor(e,t,a){super(e,t,a),this.keyOptionMap=new Map,this.getOptionByKey=e=>this.keyOptionMap.get(e),this.isValueInOptions=(e,t)=>void 0!==t.find(t=>"string"==typeof t.value?t.value===e:!!Array.isArray(t.options)&&this.isValueInOptions(e,t.options)),this.filterValuesInOptions=(e,t)=>e.filter(e=>void 0!==this.isValueInOptions(e,t)),this.afterPatchOptions=(e,t,a,i)=>{"_value"===i&&this.setFormAssociatedValue(e)},this.beforePatchOptions=(e,t)=>{const a=t.get("_value");void 0===a||Array.isArray(a)||t.set("_value",[a]);const i=t.has("_options")?t.get("_options"):this.component.state._options;if(Array.isArray(i)&&i.length>0){this.keyOptionMap.clear(),fillKeyOptionMap(this.keyOptionMap,i);const e=t.has("_value")?t.get("_value"):this.component.state._value,a=this.filterValuesInOptions(Array.isArray(e)&&e.length>0?e:[],i);!1===this.component._multiple&&0===a.length?t.set("_value",[i[0].value]):Array.isArray(e)&&a.length<e.length&&t.set("_value",a)}},this.component=e}validateOptions(e){validateOptionsWithOptgroup(this.component,e,{hooks:{afterPatch:this.afterPatchOptions,beforePatch:this.beforePatchOptions}})}validateMultiple(e){this.assertComponentValueMatchesMultiplicity(!0===e),validateMultiple(this.component,e,{hooks:{afterPatch:this.afterPatchOptions,beforePatch:this.beforePatchOptions}})}validateRequired(e){validateRequired(this.component,e)}validateRows(e){validateRows(this.component,e)}validateValue(e){this.assertValueMatchesMultiplicity(e),watchJsonArrayString(this.component,"_value",()=>!0,void 0===e?[]:Array.isArray(e)?e:[e],void 0,{hooks:{afterPatch:this.afterPatchOptions,beforePatch:this.beforePatchOptions}})}componentWillLoad(){super.componentWillLoad(),this.validateOptions(this.component._options),this.validateMultiple(this.component._multiple),this.validateRequired(this.component._required),this.validateRows(this.component._rows),this.validateValue(this.component._value)}assertValueMatchesMultiplicity(e){const t=Array.isArray(e);if(!0===this.component._multiple){if(void 0!==e&&!t)throw new Error(`↑ The schema for the property (_value) is not valid for multiple mode. Expected an array. The value will not be changed. (received = ${JSON.stringify(e)})`)}else if(t)throw new Error(`↑ The schema for the property (_value) is not valid for single mode. Expected a single value. The value will not be changed. (received = ${JSON.stringify(e)})`)}assertComponentValueMatchesMultiplicity(e){const t=this.component._value;if(e){if(void 0!==t&&!Array.isArray(t))throw new Error(`↑ The schema for the property (_value) is not valid for multiple mode. Expected an array. The value will not be changed. (current = ${JSON.stringify(t)})`)}else if(Array.isArray(t))throw new Error(`↑ The schema for the property (_value) is not valid for single mode. Expected a single value. The value will not be changed. (current = ${JSON.stringify(t)})`)}}const KolSelectWc$1=proxyCustomElement(class extends HTMLElement{async getValue(){return this._multiple?this.state._value:Array.isArray(this.state._value)&&this.state._value.length>0?this.state._value[0]:this.state._value}async kolFocus(){var e;null===(e=this.selectRef)||void 0===e||e.focus()}getFormFieldProps(){return{state:this.state,class:clsx("kol-form-field-select",{"kol-form-field--has-value":this.state._hasValue}),tooltipAlign:this._tooltipAlign,onClick:()=>{var e;return null===(e=this.selectRef)||void 0===e?void 0:e.focus()},alert:this.showAsAlert()}}getSelectProps(){return Object.assign(Object.assign({ref:this.catchRef,state:this.state},this.controller.onFacade),{onInput:this.onInput.bind(this),onChange:this.onChange.bind(this),onFocus:e=>{this.controller.onFacade.onFocus(e),this.inputHasFocus=!0},onBlur:e=>{this.controller.onFacade.onBlur(e),this.inputHasFocus=!1}})}render(){return h(FormFieldStateWrapper,Object.assign({key:"149de6c2a2a155d9869e4d09118d7b511058b606"},this.getFormFieldProps()),h(InputContainerStateWrapperFc,{key:"84abd564f808296ad32ea68165780862e7808fd0",state:this.state},h("form",{key:"3d143d2d857793ef6e744def1eada9346983f4d5",onSubmit:e=>{e.preventDefault(),propagateSubmitEventToForm({form:this.host})}},h("input",{key:"4f654dceadd96f6074e22a56aefec34944ab2faa",type:"submit",hidden:!0}),h(SelectStateWrapper,Object.assign({key:"e576920b9756ed1f8fce70c13c070749d463e839"},this.getSelectProps())))))}constructor(e){super(),!1!==e&&this.__registerHost(),this.catchRef=e=>{this.selectRef=e},this._disabled=!1,this._hideMsg=!1,this._hideLabel=!1,this._hint="",this._multiple=!1,this._required=!1,this._tooltipAlign="top",this._touched=!1,this.state={_hasValue:!1,_hideMsg:!1,_id:`id-${nonce()}`,_label:"",_multiple:!1,_options:[],_value:[]},this.inputHasFocus=!1,this.controller=new SelectController(this,"select",this.host)}showAsAlert(){return Boolean(this.state._touched)&&!this.inputHasFocus}validateAccessKey(e){this.controller.validateAccessKey(e)}validateDisabled(e){this.controller.validateDisabled(e)}validateHideMsg(e){this.controller.validateHideMsg(e)}validateHideLabel(e){this.controller.validateHideLabel(e)}validateHint(e){this.controller.validateHint(e)}validateIcons(e){this.controller.validateIcons(e)}validateId(e){this.controller.validateId(e)}validateLabel(e){this.controller.validateLabel(e)}validateMsg(e){this.controller.validateMsg(e)}validateMultiple(e){this.controller.validateMultiple(e)}validateName(e){this.controller.validateName(e)}validateOn(e){this.controller.validateOn(e)}validateOptions(e){this.controller.validateOptions(e)}validateRequired(e){this.controller.validateRequired(e)}validateRows(e){this.controller.validateRows(e)}validateShortKey(e){this.controller.validateShortKey(e)}validateSyncValueBySelector(e){this.controller.validateSyncValueBySelector(e)}validateTabIndex(e){this.controller.validateTabIndex(e)}validateTouched(e){this.controller.validateTouched(e)}validateValue(e){this.controller.validateValue(e)}componentWillLoad(){this._touched=!0===this._touched,this.controller.componentWillLoad(),this.state._hasValue=!!this.state._value,this.controller.addValueChangeListener(e=>this.state._hasValue=!!e)}onInput(e){var t;const a=Array.from((null===(t=this.selectRef)||void 0===t?void 0:t.options)||[]).filter(e=>e.selected).map(e=>{var t;return null===(t=this.controller.getOptionByKey(e.value))||void 0===t?void 0:t.value});if(this._multiple)this._value=a,this.controller.onFacade.onInput(e,!0,a);else{const t=a.length>0?a[0]:void 0;this._value=t,this.controller.onFacade.onInput(e,!0,t)}}onChange(e){this._multiple,this.controller.onFacade.onChange(e,this._value)}get host(){return this}static get watchers(){return{_accessKey:["validateAccessKey"],_disabled:["validateDisabled"],_hideMsg:["validateHideMsg"],_hideLabel:["validateHideLabel"],_hint:["validateHint"],_icons:["validateIcons"],_id:["validateId"],_label:["validateLabel"],_msg:["validateMsg"],_multiple:["validateMultiple"],_name:["validateName"],_on:["validateOn"],_options:["validateOptions"],_required:["validateRequired"],_rows:["validateRows"],_shortKey:["validateShortKey"],_syncValueBySelector:["validateSyncValueBySelector"],_tabIndex:["validateTabIndex"],_touched:["validateTouched"],_value:["validateValue"]}}},[256,"kol-select-wc",{_accessKey:[1,"_access-key"],_disabled:[4],_hideMsg:[4,"_hide-msg"],_hideLabel:[4,"_hide-label"],_hint:[1],_icons:[1],_id:[1],_label:[1],_msg:[1],_multiple:[4],_name:[1],_on:[16],_options:[1],_required:[4],_shortKey:[1,"_short-key"],_rows:[2],_syncValueBySelector:[1,"_sync-value-by-selector"],_tabIndex:[2,"_tab-index"],_tooltipAlign:[1,"_tooltip-align"],_touched:[1540],_value:[1544],state:[32],inputHasFocus:[32],getValue:[64],kolFocus:[64]},void 0,{_accessKey:["validateAccessKey"],_disabled:["validateDisabled"],_hideMsg:["validateHideMsg"],_hideLabel:["validateHideLabel"],_hint:["validateHint"],_icons:["validateIcons"],_id:["validateId"],_label:["validateLabel"],_msg:["validateMsg"],_multiple:["validateMultiple"],_name:["validateName"],_on:["validateOn"],_options:["validateOptions"],_required:["validateRequired"],_rows:["validateRows"],_shortKey:["validateShortKey"],_syncValueBySelector:["validateSyncValueBySelector"],_tabIndex:["validateTabIndex"],_touched:["validateTouched"],_value:["validateValue"]}]);function defineCustomElement$1(){if("undefined"==typeof customElements)return;["kol-select-wc"].forEach(e=>{if("kol-select-wc"===e)customElements.get(e)||customElements.define(e,KolSelectWc$1)})}const KolSelectWc=KolSelectWc$1,defineCustomElement=defineCustomElement$1;export{KolSelectWc,defineCustomElement};