@sandlada/mdc
Version:
@sandlada/mdc is an open source component library that follows the Material Design 3 design specifications.
7 lines (6 loc) • 1.66 kB
JavaScript
import{Validator as t}from"./validator.js";
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/class e extends t{computeValidity({state:t,renderedControl:e}){let r=e;n(t)&&!r?(r=this.inputControl||document.createElement("input"),this.inputControl=r):r||(r=this.textAreaControl||document.createElement("textarea"),this.textAreaControl=r);const a=n(t)?r:null;if(a&&(a.type=t.type),r.value!==t.value&&(r.value=t.value),r.required=t.required,a){const e=t;e.pattern?a.pattern=e.pattern:a.removeAttribute("pattern"),e.min?a.min=e.min:a.removeAttribute("min"),e.max?a.max=e.max:a.removeAttribute("max"),e.step?a.step=e.step:a.removeAttribute("step")}return(t.minLength??-1)>-1?r.setAttribute("minlength",String(t.minLength)):r.removeAttribute("minlength"),(t.maxLength??-1)>-1?r.setAttribute("maxlength",String(t.maxLength)):r.removeAttribute("maxlength"),{validity:r.validity,validationMessage:r.validationMessage}}equals({state:t},{state:e}){const r=t.type===e.type&&t.value===e.value&&t.required===e.required&&t.minLength===e.minLength&&t.maxLength===e.maxLength;return n(t)&&n(e)?r&&t.pattern===e.pattern&&t.min===e.min&&t.max===e.max&&t.step===e.step:r}copy({state:t}){return{state:n(t)?this.copyInput(t):this.copyTextArea(t),renderedControl:null}}copyInput(t){const{type:e,pattern:n,min:r,max:a,step:i}=t;return{...this.copySharedState(t),type:e,pattern:n,min:r,max:a,step:i}}copyTextArea(t){return{...this.copySharedState(t),type:t.type}}copySharedState({value:t,required:e,minLength:n,maxLength:r}){return{value:t,required:e,minLength:n,maxLength:r}}}function n(t){return"textarea"!==t.type}export{e as TextFieldValidator};