UNPKG

@public-ui/components

Version:

Contains all web components that belong to KoliBri - The accessible HTML-Standard.

4 lines 15.7 kB
/*! * KoliBri - The accessible HTML-Standard */ "use strict";var common=require("./common-BHv-S8J5.js"),accessAndShortKey=require("./access-and-short-key-CggfLrQB.js"),disabled=require("./disabled-wCErt5QV.js"),hideLabel=require("./hide-label-OPx-sdW8.js"),label=require("./label-DLgz6vof.js"),tooltipAlign=require("./tooltip-align-DAf31VLo.js"),events=require("./events-DDhO-Ns8.js"),associated_controller=require("./associated.controller-Bjo-6GWY.js"),isObject=require("./isObject-DMA85Fq4.js"),Heading=require("./Heading-DNsjTrzP.js"),index=require("./index-DxCCX-ts.js"),clsx=require("./clsx-CkZlkaek.js"),Alert=require("./Alert-BCV1CX_M.js"),InternalUnderlinedBadgeText=require("./InternalUnderlinedBadgeText-BNtVBme6.js"),i18n=require("./i18n-CR9McMIU.js"),componentNames=require("./component-names-D7MwtuB7.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)};function transformObjectProperties(e){return Object.entries(e).reduce(((e,[t,n])=>{const i=t.startsWith("_")?t.slice(1):t;return Object.assign(Object.assign({},e),{[i]:n})}),{})}const 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;return common.isString(t._description,1)}return!1}),new Set(["MsgPropType","string"]),t)}))};function convertMsgToInternMsg(e){if(e)return"string"==typeof e?{description:e,type:"error"}:transformObjectProperties(e)}function checkHasMsg(e,t){return!!e&&(!0===t||"error"!==(null==e?void 0:e.type))}const validateTouched=(e,t)=>{common.watchBoolean(e,"_touched",t)},FormFieldMsgFc=e=>{var{alert:t,msg:n,hideMsg:i,id:o,class:r}=e,s=Heading.__rest(e,["alert","msg","hideMsg","id","class"]);return index.h(Alert.KolAlertFc,Object.assign({id:`${o}-msg`,alert:t,type:"error",class:clsx.clsx("kol-form-field__msg",{"visually-hidden":!0===i},r)},n,s,{"aria-hidden":"true"}),(null==n?void 0:n.description)||void 0)};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,c=0,d=!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,c=t,s=e.apply(r,n)}function p(e){var n=e-l;return void 0===l||n>=t||n<0||h&&e-c>=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-c)):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 c=e,a=setTimeout(g,t),d?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)&&(d=!!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),c=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=common.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:c,showBadge:d}=e,h=Heading.__rest(e,["component","id","baseClassName","class","accessKey","shortKey","label","hideLabel","hasExpertSlot","showBadge"]);const u=!c&&l;return index.h(t,Object.assign({},h,{class:clsx.clsx(`${i}__label`,o),id:u?void 0:`${n}-label`,hidden:u,htmlFor:n}),index.h("span",{class:clsx.clsx(`${i}__label-text`)},index.h(LabelFc,{hasExpertSlot:c,accessKey:r,shortKey:s,label:a,showBadge:d})))},KolFormFieldHintFc=e=>{var{id:t,class:n,hint:i,baseClassName:o="kol-form-field"}=e,r=Heading.__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},KolFormFieldCounterFc=({currentLength:e,currentLengthDebounced:t,maxLength:n,maxLengthBehavior:i})=>{let o,r;const s=["kol-form-field__counter"];if("hard"===i)o="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)}}),r="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,a=i<0,l=n-t,c=l<0;o=a?i18n.translate("kol-character-limit-exceeded",{placeholders:{over:String(Math.abs(i))}}):i18n.translate("kol-character-limit-remaining",{placeholders:{remaining:String(i)}}),r=c?i18n.translate("kol-character-limit-exceeded",{placeholders:{over:String(Math.abs(l))}}):i18n.translate("kol-character-limit-remaining",{placeholders:{remaining:String(l)}}),a&&s.push("kol-form-field__counter--exceeded")}}return index.h(index.Fragment,null,index.h("span",{class:clsx.clsx(s),"aria-hidden":"true","data-testid":"input-counter"},o),index.h("span",{"aria-live":"polite",class:"visually-hidden","data-testid":"input-counter-aria"},r))},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)}})),FormFieldTooltipFc=({id:e,align:t,badgeText:n,hideLabel:i,label:o,class:r})=>index.h(componentNames.KolTooltipWcTag,{"aria-hidden":"true",class:clsx.clsx("kol-form-field__tooltip",r),_badgeText:n,_align:t,_id:i?`${e}-label`:void 0,_label:o});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=Heading.__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:c,disabled:d,class:h,msg:u,hideMsg:m,hideLabel:p,label:g,hint:b,accessKey:v,shortKey:f,tooltipAlign:x,counter:_,readOnly:y,touched:F,maxLength:L,formFieldLabelProps:K,formFieldHintProps:j,formFieldTooltipProps:T,formFieldMsgProps:S,formFieldInputProps:M}=e,C=Heading.__rest(e,["component","renderNoLabel","renderNoTooltip","renderNoHint","anotherChildren","id","required","alert","disabled","class","msg","hideMsg","hideLabel","label","hint","accessKey","shortKey","tooltipAlign","counter","readOnly","touched","maxLength","formFieldLabelProps","formFieldHintProps","formFieldTooltipProps","formFieldMsgProps","formFieldInputProps"]),O=!i,B=!r,H=!o,I=common.showExpertSlot(g),k=checkHasMsg(u,F),A=common.buildBadgeTextString(v,f),E=H&&!I&&p;let N={"kol-form-field--disabled":Boolean(d),"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(y),"kol-form-field--hidden-msg":Boolean(m)};return k&&(N=Object.assign(Object.assign({},N),{[`kol-form-field--${(null==u?void 0:u.type)||"error"}`]:!0,[`kol-form-field--${getModifierClassNameByMsgType(u)}`]:!0})),index.h(n,Object.assign({class:clsx.clsx("kol-form-field",N,h)},C),O&&index.h(KolFormFieldLabelFc,Object.assign({},K||{},{id:a,hasExpertSlot:I,hideLabel:p,label:g,accessKey:v,shortKey:f})),index.h(InputContainer,Object.assign({},M),t,E&&index.h(FormFieldTooltipFc,Object.assign({},T||{},{id:a,label:g,hideLabel:p,align:x,badgeText:A}))),_?index.h(KolFormFieldCounterFc,Object.assign({},_)):null,L?index.h(KolFormFieldCharacterLimitHintFc,{id:a,maxLength:L}):null,k&&index.h(FormFieldMsgFc,Object.assign({},S||{},{id:a,alert:c,msg:u,hideMsg:m})),B&&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)},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)}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=convertMsgToInternMsg(e._msg),n=Boolean((null==t?void 0:t.description)&&t.description.length>0),i="error"===(null==t?void 0:t.type)&&n&&!0===e._touched,o="string"==typeof e._hint&&e._hint.length>0,r=[];return n&&r.push(`${e._id}-msg`),o&&r.push(`${e._id}-hint`),{hasError:i,hasHint:o,ariaDescribedBy:r}};function getFormFieldProps(e){const t={id:e._id,disabled:e._disabled,msg:convertMsgToInternMsg(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=Heading.__rest(e,["state"]);return index.h(KolFormFieldFc,Object.assign({},getFormFieldProps(n),i),t)};exports.FormFieldStateWrapper=FormFieldStateWrapper,exports.FormFieldTooltipFc=FormFieldTooltipFc,exports.InputController=InputController,exports.KolFormFieldHintFc=KolFormFieldHintFc,exports.KolFormFieldLabelFc=KolFormFieldLabelFc,exports.checkHasMsg=checkHasMsg,exports.convertMsgToInternMsg=convertMsgToInternMsg,exports.getDefaultProps=getDefaultProps,exports.getRenderStates=getRenderStates;