@public-ui/components
Version:
Contains all web components that belong to KoliBri - The accessible HTML-Standard.
4 lines • 15.2 kB
JavaScript
/*!
* KoliBri - The accessible HTML-Standard
*/
import{d as watchBoolean,e as watchString,o as objectObjectHandler,p as parseJson,w as watchValidator,G as isObject,g as isString,k as buildBadgeTextString,l as showExpertSlot,j as devHint,s as setState,t as a11yHint}from"./common-7RW_NFNf.js";import{v as validateAccessKey,a as validateAccessAndShortKey,b as validateShortKey,c as validateTabIndex}from"./access-and-short-key-CIkiLGbP.js";import{v as validateDisabled}from"./disabled-DOGGfuG4.js";import{v as validateHideLabel}from"./hide-label-B1sxX0if.js";import{a as validateLabelWithExpertSlot}from"./label-DAfXJvJZ.js";import{v as validateTooltipAlign}from"./tooltip-align-B_X7PRrT.js";import{d as dispatchDomEvent,K as KolEvent}from"./events-CoPFQEiC.js";import{A as AssociatedInputController}from"./associated.controller-DWb-3J2w.js";import{a as isObjectLike,c as baseGetTag,i as isObject$1,r as root}from"./isObject-DvSELytj.js";import{_ as __rest}from"./Heading-D6_TFw6T.js";import{h,F as Fragment}from"./index-CElQfBOC.js";import{c as clsx}from"./clsx-eK3rPvPS.js";import{K as KolAlertFc}from"./Alert-BscBrOtx.js";import{i as isString$1,K as KolInternalUnderlinedBadgeTextFc}from"./InternalUnderlinedBadgeText-DnxrXpNU.js";import{t as translate}from"./i18n-yBVLeHdv.js";import{a as KolTooltipWcTag}from"./component-names-CkE54Ym7.js";const validateAdjustHeight=(e,t)=>{watchBoolean(e,"_adjustHeight",t)},validateHideMsg=(e,t,n)=>{watchBoolean(e,"_hideMsg",t,n)},validateHint=(e,t)=>{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)=>{objectObjectHandler(t,(()=>{try{t=parseJson(t)}catch(e){}watchValidator(e,"_msg",(e=>{if(void 0===e)return!0;if("string"==typeof e&&e.length>0)return!0;if(isObject(e)&&null!==e){return isString(e._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)=>{watchBoolean(e,"_touched",t)},FormFieldMsgFc=e=>{var{alert:t,msg:n,hideMsg:i,id:o,class:a}=e,r=__rest(e,["alert","msg","hideMsg","id","class"]);return h(KolAlertFc,Object.assign({id:`${o}-msg`,alert:t,type:"error",class:clsx("kol-form-field__msg",{"visually-hidden":!0===i},a)},n,r,{"aria-hidden":"true"}),(null==n?void 0:n.description)||void 0)};var symbolTag="[object Symbol]";function isSymbol(e){return"symbol"==typeof e||isObjectLike(e)&&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$1(e)){var t="function"==typeof e.valueOf?e.valueOf():e;e=isObject$1(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 root.Date.now()},FUNC_ERROR_TEXT="Expected a function",nativeMax=Math.max,nativeMin=Math.min;function debounce(e,t,n){var i,o,a,r,s,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,a=o;return i=o=void 0,c=t,r=e.apply(a,n)}function p(e){var n=e-l;return void 0===l||n>=t||n<0||h&&e-c>=a}function g(){var e=now();if(p(e))return v(e);s=setTimeout(g,function(e){var n=t-(e-l);return h?nativeMin(n,a-(e-c)):n}(e))}function v(e){return s=void 0,u&&i?m(e):(i=o=void 0,r)}function b(){var e=now(),n=p(e);if(i=arguments,o=this,l=e,n){if(void 0===s)return function(e){return c=e,s=setTimeout(g,t),d?m(e):r}(l);if(h)return clearTimeout(s),s=setTimeout(g,t),m(l)}return void 0===s&&(s=setTimeout(g,t)),r}return t=toNumber(t)||0,isObject$1(n)&&(d=!!n.leading,a=(h="maxWait"in n)?nativeMax(toNumber(n.maxWait)||0,t):a,u="trailing"in n?!!n.trailing:u),b.cancel=function(){void 0!==s&&clearTimeout(s),c=0,i=l=o=s=void 0},b.flush=function(){return void 0===s?r:v(now())},b}const LabelFc=({hasExpertSlot:e,accessKey:t,shortKey:n,label:i,showBadge:o=!0})=>{if(e)return h("slot",{name:"expert"});if(!i)return null;const a=isString$1(t)||isString$1(n);if(!o||!a)return h("span",null,i);const r=buildBadgeTextString(t,n);return h(Fragment,null,h(KolInternalUnderlinedBadgeTextFc,{badgeText:r,label:i})," ",h("kbd",{class:"badge-text-hint","aria-hidden":"true"},r))},KolFormFieldLabelFc=e=>{var{component:t="label",id:n,baseClassName:i="kol-form-field",class:o,accessKey:a,shortKey:r,label:s,hideLabel:l,hasExpertSlot:c,showBadge:d}=e,u=__rest(e,["component","id","baseClassName","class","accessKey","shortKey","label","hideLabel","hasExpertSlot","showBadge"]);const m=!c&&l;return h(t,Object.assign({},u,{class:clsx(`${i}__label`,o),id:m?void 0:`${n}-label`,hidden:m,htmlFor:n}),h("span",{class:clsx(`${i}__label-text`)},h(LabelFc,{hasExpertSlot:c,accessKey:a,shortKey:r,label:s,showBadge:d})))},KolFormFieldHintFc=e=>{var{id:t,class:n,hint:i,baseClassName:o="kol-form-field"}=e,a=__rest(e,["id","class","hint","baseClassName"]);return i?h("span",Object.assign({class:clsx(`${o}__hint`,n),id:`${t}-hint`},a),i):null},KolFormFieldCounterFc=({currentLength:e,currentLengthDebounced:t,maxLength:n,maxLengthBehavior:i})=>{let o,a;const r=["kol-form-field__counter"];if("hard"===i)o="number"==typeof n?translate("kol-character-counter-current-of-max",{placeholders:{current:String(e),max:String(n)}}):translate("kol-character-counter-current",{placeholders:{current:String(e)}}),a="number"==typeof n?translate("kol-character-counter-current-of-max-aria",{placeholders:{current:String(t),max:String(n)}}):translate("kol-character-counter-current",{placeholders:{current:String(t)}});else{if("number"!=typeof n)return null;{const i=n-e,s=i<0,l=n-t,c=l<0;o=s?translate("kol-character-limit-exceeded",{placeholders:{over:String(Math.abs(i))}}):translate("kol-character-limit-remaining",{placeholders:{remaining:String(i)}}),a=c?translate("kol-character-limit-exceeded",{placeholders:{over:String(Math.abs(l))}}):translate("kol-character-limit-remaining",{placeholders:{remaining:String(l)}}),s&&r.push("kol-form-field__counter--exceeded")}}return h(Fragment,null,h("span",{class:clsx(r),"aria-hidden":"true","data-testid":"input-counter"},o),h("span",{"aria-live":"polite",class:"visually-hidden","data-testid":"input-counter-aria"},a))},KolFormFieldCharacterLimitHintFc=({id:e,maxLength:t})=>h("span",{id:`${e}-character-limit-hint`,class:"visually-hidden"},translate("kol-character-limit-hint",{placeholders:{limit:String(t)}})),FormFieldTooltipFc=({id:e,align:t,badgeText:n,hideLabel:i,label:o,class:a})=>h(KolTooltipWcTag,{"aria-hidden":"true",class:clsx("kol-form-field__tooltip",a),_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=__rest(e,["class"]);return h("div",Object.assign({class:clsx("kol-form-field__input",n)},i),t)},KolFormFieldFc=(e,t)=>{const{component:n="div",renderNoLabel:i,renderNoTooltip:o,renderNoHint:a,anotherChildren:r,id:s,required:l,alert:c,disabled:d,class:u,msg:m,hideMsg:p,hideLabel:g,label:v,hint:b,accessKey:f,shortKey:_,tooltipAlign:y,counter:F,readOnly:x,touched:L,maxLength:K,formFieldLabelProps:j,formFieldHintProps:T,formFieldTooltipProps:S,formFieldMsgProps:C,formFieldInputProps:O}=e,B=__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"]),M=!i,k=!a,H=!o,I=showExpertSlot(v),E=checkHasMsg(m,L),A=buildBadgeTextString(f,_),w=H&&!I&&g;let N={"kol-form-field--disabled":Boolean(d),"kol-form-field--required":Boolean(l),"kol-form-field--touched":Boolean(L),"kol-form-field--hide-label":Boolean(g),"kol-form-field--read-only":Boolean(x),"kol-form-field--hidden-msg":Boolean(p)};return E&&(N=Object.assign(Object.assign({},N),{[`kol-form-field--${(null==m?void 0:m.type)||"error"}`]:!0,[`kol-form-field--${getModifierClassNameByMsgType(m)}`]:!0})),h(n,Object.assign({class:clsx("kol-form-field",N,u)},B),M&&h(KolFormFieldLabelFc,Object.assign({},j||{},{id:s,hasExpertSlot:I,hideLabel:g,label:v,accessKey:f,shortKey:_})),h(InputContainer,Object.assign({},O),t,w&&h(FormFieldTooltipFc,Object.assign({},S||{},{id:s,label:v,hideLabel:g,align:y,badgeText:A}))),F?h(KolFormFieldCounterFc,Object.assign({},F)):null,K?h(KolFormFieldCharacterLimitHintFc,{id:s,maxLength:K}):null,E&&h(FormFieldMsgFc,Object.assign({},C||{},{id:s,alert:c,msg:m,hideMsg:p})),k&&h(KolFormFieldHintFc,Object.assign({},T||{},{id:s,hint:b})),r)};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 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=>{setState(this.component,"_currentLengthDebounced",e)}),500),this.component=e}validateAccessKey(e){validateAccessKey(this.component,e),validateAccessAndShortKey(e,this.component._shortKey)}validateAdjustHeight(e){validateAdjustHeight(this.component,e)}validateDisabled(e){validateDisabled(this.component,e)}validateTooltipAlign(e){validateTooltipAlign(this.component,e)}validateHideMsg(e){validateHideMsg(this.component,e,{hooks:{afterPatch:()=>{this.component.state._hideMsg&&a11yHint("Property _hideMsg for inputs: Only use when the error message is shown outside of the input component.")}}})}validateHideLabel(e){validateHideLabel(this.component,e,{hooks:{afterPatch:()=>{this.component.state._hideLabel&&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){watchString(this.component,"_id",e,{minLength:1}),""!==e&&void 0!==e||devHint("A unique ID on the input fields is not strictly required, but it might be relevant for E2E tests.")}validateLabel(e){validateLabelWithExpertSlot(this.component,e,{required:!0})}validateMsg(e){validateMsg(this.component,e)}validateOn(e){"object"==typeof e&&setState(this.component,"_on",e)}validateShortKey(e){validateShortKey(this.component,e),validateAccessAndShortKey(this.component._accessKey,e)}validateSmartButton(e){objectObjectHandler(e,(()=>{try{e=parseJson(e)}catch(e){}setState(this.component,"_smartButton",e)}))}validateTabIndex(e){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),validateAccessAndShortKey(this.component._accessKey,this.component._shortKey)}emitEvent(e,t){this.host&&dispatchDomEvent(this.host,e,t)}onBlur(e){var t;this.component._touched=!0,this.emitEvent(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(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(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(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(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,a=[];return n&&a.push(`${e._id}-msg`),o&&a.push(`${e._id}-hint`),{hasError:i,hasHint:o,ariaDescribedBy:a}};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=__rest(e,["state"]);return h(KolFormFieldFc,Object.assign({},getFormFieldProps(n),i),t)};export{FormFieldStateWrapper as F,InputController as I,KolFormFieldLabelFc as K,convertMsgToInternMsg as a,FormFieldTooltipFc as b,checkHasMsg as c,KolFormFieldHintFc as d,getDefaultProps as e,getRenderStates as g};