@public-ui/components
Version:
Contains all web components that belong to KoliBri - The accessible HTML-Standard.
4 lines • 16.5 kB
JavaScript
/*!
* KoliBri - The accessible HTML-Standard
*/
"use strict";var common=require("./common-1pTy2kta.js"),accessAndShortKey=require("./access-and-short-key-DwC1Fz3j.js"),disabled=require("./disabled-CaeFOFNv.js"),hideLabel=require("./hide-label-DJdjz8C9.js"),label=require("./label-6ELpV3zN.js"),tooltipAlign=require("./tooltip-align-Bmn7e65O.js"),events=require("./events-Cd8febUV.js"),associated_controller=require("./associated.controller-bswFXTsy.js"),isObject=require("./isObject-DMA85Fq4.js"),tslib_es6=require("./tslib.es6-7DbU_BSw.js"),index=require("./index-BH-BeuHg.js"),clsx=require("./clsx-CkZlkaek.js"),reuse=require("./reuse-Cs36Ddzu.js"),i18n=require("./i18n-D4Klw_bZ.js"),InternalUnderlinedBadgeText=require("./InternalUnderlinedBadgeText-CUCD88bO.js"),Alert=require("./Alert-DPcQYoEw.js"),componentNames=require("./component-names-Bu9hV7OT.js");const validateAdjustHeight=(e,t)=>{common.watchBoolean(e,"_adjustHeight",t)},validateHideMsg=(e,t,n)=>{common.watchBoolean(e,"_hideMsg",t,n)},validateHint=(e,t)=>{common.watchString(e,"_hint",t)},validateMsg=(e,t)=>{common.objectObjectHandler(t,()=>{try{t=common.parseJson(t)}catch(e){}common.watchValidator(e,"_msg",e=>{if(void 0===e)return!0;if("string"==typeof e&&e.length>0)return!0;if(common.isObject(e)&&null!==e){const t=e._description;return common.isString(t,1)}return!1},new Set(["MsgPropType","string"]),t)})};function isMsgDefinedAndInputTouched(e,t){return Boolean(e)&&!0===t}function normalizeMsg(e){if("string"==typeof e)try{return common.parseJson(e)}catch(t){return{_description:e,_type:"error"}}return e&&"object"==typeof e&&!("_type"in e)?Object.assign(Object.assign({},e),{_type:"error"}):e}function getMsgType(e){var t;return"string"==typeof e?"error":null!==(t=null==e?void 0:e._type)&&void 0!==t?t:"error"}const validateTouched=(e,t)=>{common.watchBoolean(e,"_touched",t)},KolFormFieldCharacterLimitHintFc=({id:e,maxLength:t})=>index.h("span",{id:`${e}-character-limit-hint`,class:"visually-hidden"},i18n.translate("kol-character-limit-hint",{placeholders:{limit:String(t)}})),KolFormFieldCounterFc=({currentLength:e,currentLengthDebounced:t,maxLength:n,maxLengthBehavior:i,id:o})=>{let r,s;const a=["kol-form-field__counter"];if("hard"===i)r="number"==typeof n?i18n.translate("kol-character-counter-current-of-max",{placeholders:{current:String(e),max:String(n)}}):i18n.translate("kol-character-counter-current",{placeholders:{current:String(e)}}),s="number"==typeof n?i18n.translate("kol-character-counter-current-of-max-aria",{placeholders:{current:String(t),max:String(n)}}):i18n.translate("kol-character-counter-current",{placeholders:{current:String(t)}});else{if("number"!=typeof n)return null;{const i=n-e,o=i<0,l=n-t,d=l<0;r=o?i18n.translate("kol-character-limit-exceeded",{placeholders:{over:String(Math.abs(i))}}):i18n.translate("kol-character-limit-remaining",{placeholders:{remaining:String(i)}}),s=d?i18n.translate("kol-character-limit-exceeded",{placeholders:{over:String(Math.abs(l))}}):i18n.translate("kol-character-limit-remaining",{placeholders:{remaining:String(l)}}),o&&a.push("kol-form-field__counter--exceeded")}}return index.h(index.Fragment,null,index.h("span",{class:clsx.clsx(a),"aria-hidden":"true","data-testid":"input-counter"},r),index.h("span",{id:`${o}-counter`,"aria-live":"polite",class:"visually-hidden","data-testid":"input-counter-aria"},s))},KolFormFieldHintFc=e=>{var{id:t,class:n,hint:i,baseClassName:o="kol-form-field"}=e,r=tslib_es6.__rest(e,["id","class","hint","baseClassName"]);return i?index.h("span",Object.assign({class:clsx.clsx(`${o}__hint`,n),id:`${t}-hint`},r),i):null};var symbolTag="[object Symbol]";function isSymbol(e){return"symbol"==typeof e||isObject.isObjectLike(e)&&isObject.baseGetTag(e)==symbolTag}var reWhitespace=/\s/;function trimmedEndIndex(e){for(var t=e.length;t--&&reWhitespace.test(e.charAt(t)););return t}var reTrimStart=/^\s+/;function baseTrim(e){return e?e.slice(0,trimmedEndIndex(e)+1).replace(reTrimStart,""):e}var NAN=NaN,reIsBadHex=/^[-+]0x[0-9a-f]+$/i,reIsBinary=/^0b[01]+$/i,reIsOctal=/^0o[0-7]+$/i,freeParseInt=parseInt;function toNumber(e){if("number"==typeof e)return e;if(isSymbol(e))return NAN;if(isObject.isObject(e)){var t="function"==typeof e.valueOf?e.valueOf():e;e=isObject.isObject(t)?t+"":t}if("string"!=typeof e)return 0===e?e:+e;e=baseTrim(e);var n=reIsBinary.test(e);return n||reIsOctal.test(e)?freeParseInt(e.slice(2),n?2:8):reIsBadHex.test(e)?NAN:+e}var now=function(){return isObject.root.Date.now()},FUNC_ERROR_TEXT="Expected a function",nativeMax=Math.max,nativeMin=Math.min;function debounce(e,t,n){var i,o,r,s,a,l,d=0,c=!1,h=!1,u=!0;if("function"!=typeof e)throw new TypeError(FUNC_ERROR_TEXT);function m(t){var n=i,r=o;return i=o=void 0,d=t,s=e.apply(r,n)}function p(e){var n=e-l;return void 0===l||n>=t||n<0||h&&e-d>=r}function g(){var e=now();if(p(e))return b(e);a=setTimeout(g,function(e){var n=t-(e-l);return h?nativeMin(n,r-(e-d)):n}(e))}function b(e){return a=void 0,u&&i?m(e):(i=o=void 0,s)}function v(){var e=now(),n=p(e);if(i=arguments,o=this,l=e,n){if(void 0===a)return function(e){return d=e,a=setTimeout(g,t),c?m(e):s}(l);if(h)return clearTimeout(a),a=setTimeout(g,t),m(l)}return void 0===a&&(a=setTimeout(g,t)),s}return t=toNumber(t)||0,isObject.isObject(n)&&(c=!!n.leading,r=(h="maxWait"in n)?nativeMax(toNumber(n.maxWait)||0,t):r,u="trailing"in n?!!n.trailing:u),v.cancel=function(){void 0!==a&&clearTimeout(a),d=0,i=l=o=a=void 0},v.flush=function(){return void 0===a?s:b(now())},v}const LabelFc=({hasExpertSlot:e,accessKey:t,shortKey:n,label:i,showBadge:o=!0})=>{if(e)return index.h("slot",{name:"expert"});if(!i)return null;const r=InternalUnderlinedBadgeText.isString(t)||InternalUnderlinedBadgeText.isString(n);if(!o||!r)return index.h("span",null,i);const s=reuse.buildBadgeTextString(t,n);return index.h(index.Fragment,null,index.h(InternalUnderlinedBadgeText.KolInternalUnderlinedBadgeTextFc,{badgeText:s,label:i})," ",index.h("kbd",{class:"badge-text-hint","aria-hidden":"true"},s))},KolFormFieldLabelFc=e=>{var{component:t="label",id:n,baseClassName:i="kol-form-field",class:o,accessKey:r,shortKey:s,label:a,hideLabel:l,hasExpertSlot:d,showBadge:c,readOnly:h}=e,u=tslib_es6.__rest(e,["component","id","baseClassName","class","accessKey","shortKey","label","hideLabel","hasExpertSlot","showBadge","readOnly"]);const m=!d&&l,p=i18n.translate("kol-readonly");return index.h(t,Object.assign({},u,{class:clsx.clsx(`${i}__label`,o),id:m?void 0:`${n}-label`,hidden:m,htmlFor:n}),index.h("span",{class:clsx.clsx(`${i}__label-text`)},index.h(LabelFc,{hasExpertSlot:d,accessKey:r,shortKey:s,label:a,showBadge:c}),h?index.h("span",{class:clsx.clsx(`${i}__label__read-only`),"aria-hidden":"true"},"(",p,")"):null))},FormFieldMsgFc=e=>{var t,n,{alert:i,msg:o,id:r,class:s}=e,a=tslib_es6.__rest(e,["alert","msg","id","class"]);const l=normalizeMsg(o);return index.h(Alert.KolAlertFc,Object.assign({id:`${r}-msg`,alert:null!==(t=null==l?void 0:l._alert)&&void 0!==t?t:i,hasCloser:!1,level:0,type:null!==(n=null==l?void 0:l._type)&&void 0!==n?n:"error",variant:"msg",class:clsx.clsx("kol-form-field__msg",s)},a),(null==l?void 0:l._description)||void 0)},FormFieldTooltipFc=({id:e,align:t,badgeText:n,label:i,class:o})=>index.h(componentNames.KolTooltipWcTag,{"aria-hidden":"true",class:clsx.clsx("kol-form-field__tooltip",o),_badgeText:n,_align:t,_id:`${e}-label`,_label:i});function getModifierClassNameByMsgType(e){return(null==e?void 0:e.type)&&{default:"msg-type-default",info:"msg-type-info",success:"msg-type-success",warning:"msg-type-warning",error:"msg-type-error"}[null==e?void 0:e.type]||""}const InputContainer=(e,t)=>{var{class:n}=e,i=tslib_es6.__rest(e,["class"]);return index.h("div",Object.assign({class:clsx.clsx("kol-form-field__input",n)},i),t)},KolFormFieldFc=(e,t)=>{const{component:n="div",renderNoLabel:i,renderNoTooltip:o,renderNoHint:r,anotherChildren:s,id:a,required:l,alert:d,disabled:c,class:h,msg:u,hideMsg:m,hideLabel:p,label:g,hint:b,accessKey:v,shortKey:f,tooltipAlign:_,counter:y,readOnly:x,touched:F,maxLength:K,ariaDescribedBy:L,formFieldLabelProps:T,formFieldHintProps:j,formFieldTooltipProps:S,formFieldMsgProps:C,formFieldInputProps:B}=e,O=tslib_es6.__rest(e,["component","renderNoLabel","renderNoTooltip","renderNoHint","anotherChildren","id","required","alert","disabled","class","msg","hideMsg","hideLabel","label","hint","accessKey","shortKey","tooltipAlign","counter","readOnly","touched","maxLength","ariaDescribedBy","formFieldLabelProps","formFieldHintProps","formFieldTooltipProps","formFieldMsgProps","formFieldInputProps"]),M=!i,k=!r,I=!o,A=reuse.showExpertSlot(g),D=isMsgDefinedAndInputTouched(u,F),H=reuse.buildBadgeTextString(v,f),E=I&&!A&&p;let w={"kol-form-field--disabled":Boolean(c),"kol-form-field--required":Boolean(l),"kol-form-field--touched":Boolean(F),"kol-form-field--hide-label":Boolean(p),"kol-form-field--read-only":Boolean(x),"kol-form-field--hidden-msg":Boolean(m)};if(D){const e=getMsgType(u);w=Object.assign(Object.assign({},w),{[`kol-form-field--${e}`]:!0,[`kol-form-field--${getModifierClassNameByMsgType({type:e})}`]:!0})}return index.h(n,Object.assign({class:clsx.clsx("kol-form-field",w,h),"aria-describedby":L},O),M&&index.h(KolFormFieldLabelFc,Object.assign({},T||{},{id:a,hasExpertSlot:A,hideLabel:p,label:g,accessKey:v,shortKey:f,readOnly:x})),index.h(InputContainer,Object.assign({},B),t,E&&!0===p&&index.h(FormFieldTooltipFc,Object.assign({},S||{},{id:a,label:g,align:_,badgeText:H}))),y?index.h(KolFormFieldCounterFc,Object.assign({id:a},y)):null,K?index.h(KolFormFieldCharacterLimitHintFc,{id:a,maxLength:K}):null,D&&!m&&index.h(FormFieldMsgFc,Object.assign({},C||{},{id:a,alert:d,msg:u})),k&&index.h(KolFormFieldHintFc,Object.assign({},j||{},{id:a,hint:b})),s)};function getDefaultProps({ariaDescribedBy:e,hideLabel:t,label:n}){return{title:"",autoCapitalize:"off",autoCorrect:"off","aria-describedby":(null==e?void 0:e.length)?e.join(" "):void 0,"aria-label":t&&n?n:void 0}}class ControlledInputController extends associated_controller.AssociatedInputController{constructor(e,t,n){super(e,t,n),this.component=e}validateTouched(e){validateTouched(this.component,e)}componentWillLoad(){super.componentWillLoad(),this.validateTouched(this.component._touched)}}class InputController extends ControlledInputController{constructor(e,t,n){super(e,t,n),this.valueChangeListeners=[],this.onFacade={onBlur:this.onBlur.bind(this),onChange:this.onChange.bind(this),onClick:this.onClick.bind(this),onFocus:this.onFocus.bind(this),onInput:this.onInput.bind(this),onKeyDown:this.onKeyDown.bind(this)},this.updateCurrentLengthDebounced=debounce(e=>{common.setState(this.component,"_currentLengthDebounced",e)},500),this.component=e}validateAccessKey(e){accessAndShortKey.validateAccessKey(this.component,e),accessAndShortKey.validateAccessAndShortKey(e,this.component._shortKey)}validateAdjustHeight(e){validateAdjustHeight(this.component,e)}validateDisabled(e){disabled.validateDisabled(this.component,e)}validateTooltipAlign(e){tooltipAlign.validateTooltipAlign(this.component,e)}validateHideMsg(e){validateHideMsg(this.component,e,{hooks:{afterPatch:()=>{this.component.state._hideMsg&&common.a11yHint("Property _hideMsg for inputs: Only use when the error message is shown outside of the input component.")}}})}validateHideLabel(e){hideLabel.validateHideLabel(this.component,e,{hooks:{afterPatch:()=>{this.component.state._hideLabel&&common.a11yHint("Property hide-label for inputs: Only use for exceptions like search inputs that are clearly identifiable by their context.")}}})}validateHint(e){validateHint(this.component,e)}validateId(e){common.watchString(this.component,"_id",e,{minLength:1}),""!==e&&void 0!==e||common.devHint("A unique ID on the input fields is not strictly required, but it might be relevant for E2E tests.")}validateLabel(e){label.validateLabelWithExpertSlot(this.component,e,{required:!0})}validateMsg(e){validateMsg(this.component,e)}validateOn(e){"object"==typeof e&&common.setState(this.component,"_on",e)}validateShortKey(e){accessAndShortKey.validateShortKey(this.component,e),accessAndShortKey.validateAccessAndShortKey(this.component._accessKey,e)}validateSmartButton(e){common.objectObjectHandler(e,()=>{try{e=common.parseJson(e)}catch(e){}common.setState(this.component,"_smartButton",e)})}validateTabIndex(e){accessAndShortKey.validateTabIndex(this.component,e)}componentWillLoad(){super.componentWillLoad(),this.validateAccessKey(this.component._accessKey),this.validateAdjustHeight(this.component._adjustHeight),this.validateMsg(this.component._msg),this.validateDisabled(this.component._disabled),this.validateHideMsg(this.component._hideMsg),this.validateHideLabel(this.component._hideLabel),this.validateHint(this.component._hint),this.validateId(this.component._id),this.validateLabel(this.component._label),this.validateShortKey(this.component._shortKey),this.validateSmartButton(this.component._smartButton),this.validateOn(this.component._on),this.validateTabIndex(this.component._tabIndex),accessAndShortKey.validateAccessAndShortKey(this.component._accessKey,this.component._shortKey)}emitEvent(e,t){this.host&&events.dispatchDomEvent(this.host,e,t)}onBlur(e){var t;this.component._touched=!0,this.emitEvent(events.KolEvent.blur),"function"==typeof(null===(t=this.component._on)||void 0===t?void 0:t.onBlur)&&this.component._on.onBlur(e)}onChange(e,t){var n;void 0===t&&(t=e.target.value),this.emitEvent(events.KolEvent.change,t),"function"==typeof(null===(n=this.component._on)||void 0===n?void 0:n.onChange)&&this.component._on.onChange(e,t),this.valueChangeListeners.forEach(e=>e(t))}onInput(e,t=!0,n){var i;void 0===n&&(n=e.target.value),this.emitEvent(events.KolEvent.input,n),t&&this.setFormAssociatedValue(n),"function"==typeof(null===(i=this.component._on)||void 0===i?void 0:i.onInput)&&this.component._on.onInput(e,n)}onClick(e){var t;this.emitEvent(events.KolEvent.click),"function"==typeof(null===(t=this.component._on)||void 0===t?void 0:t.onClick)&&this.component._on.onClick(e)}onFocus(e){var t;this.emitEvent(events.KolEvent.focus),"function"==typeof(null===(t=this.component._on)||void 0===t?void 0:t.onFocus)&&this.component._on.onFocus(e)}onKeyDown(e){var t;this.emitEvent(events.KolEvent.keydown),"function"==typeof(null===(t=this.component._on)||void 0===t?void 0:t.onKeyDown)&&this.component._on.onKeyDown(e)}addValueChangeListener(e){this.valueChangeListeners.push(e)}hasSoftCharacterLimit(){return"number"==typeof this.component.state._maxLength&&"soft"===this.component.state._maxLengthBehavior}hasCounter(){return!0===this.component.state._hasCounter}}const getRenderStates=e=>{const t=e._msg,n="string"==typeof t?t:null==t?void 0:t._description,i=getMsgType(t),o=Boolean(n&&n.length>0),r="error"===i&&o&&!0===e._touched,s="string"==typeof e._hint&&e._hint.length>0,a=[];return o&&!e._hideMsg&&a.push(`${e._id}-msg`),!0===s&&a.push(`${e._id}-hint`),e._hasCounter&&a.push(`${e._id}-counter`),!0===r&&a.push(`${e._id}-error`),{hasError:r,hasHint:s,ariaDescribedBy:a}};function getFormFieldProps(e){const t={id:e._id,disabled:e._disabled,msg:e._msg,hint:e._hint,label:e._label,hideLabel:e._hideLabel,hideMsg:e._hideMsg,touched:e._touched};return"_required"in e&&(t.required=e._required),"_readOnly"in e&&(t.readOnly=e._readOnly),"_accessKey"in e&&(t.accessKey=e._accessKey),"_shortKey"in e&&(t.shortKey=e._shortKey),"_maxLength"in e&&(t.maxLength=e._maxLength),"_currentLength"in e&&"number"==typeof e._currentLength&&"_currentLengthDebounced"in e&&"number"==typeof e._currentLengthDebounced&&"_hasCounter"in e&&!0===e._hasCounter&&(t.counter={currentLength:e._currentLength,currentLengthDebounced:e._currentLengthDebounced,maxLength:e._maxLength,maxLengthBehavior:e._maxLengthBehavior||"hard"}),t}const FormFieldStateWrapper=(e,t)=>{var{state:n}=e,i=tslib_es6.__rest(e,["state"]);const{ariaDescribedBy:o}=getRenderStates(n),r=("fieldset"===i.component||"_options"in n&&"_orientation"in n)&&o.length>0?o.join(" "):void 0,s=getFormFieldProps(n);return index.h(KolFormFieldFc,Object.assign({},s,i,{ariaDescribedBy:r}),t)};exports.FormFieldStateWrapper=FormFieldStateWrapper,exports.FormFieldTooltipFc=FormFieldTooltipFc,exports.InputController=InputController,exports.KolFormFieldHintFc=KolFormFieldHintFc,exports.KolFormFieldLabelFc=KolFormFieldLabelFc,exports.getDefaultProps=getDefaultProps,exports.getMsgType=getMsgType,exports.getRenderStates=getRenderStates,exports.isMsgDefinedAndInputTouched=isMsgDefinedAndInputTouched;