UNPKG

@tripetto/block-date

Version:

Date block for Tripetto.

3 lines (2 loc) 2.02 kB
/*! Tripetto Date Block 4.0.3 - Copyright (C) 2023 Tripetto B.V. - All Rights Reserved */ import{tripetto as t,condition as e,ConditionBlock as o,isString as r,Slots as a,isNumberFinite as s,DateTime as l,Num as i,NodeBlock as u,assert as n,markdownifyToString as h,validator as c}from"@tripetto/runner";function d(t,e,o,r){var a,s=arguments.length,l=s<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)l=Reflect.decorate(t,e,o,r);else for(var i=t.length-1;i>=0;i--)(a=t[i])&&(l=(s<3?a(l):s>3?a(e,o,l):a(e,o))||l);return s>3&&l&&Object.defineProperty(e,o,l),l}let p=class extends o{getValue(t,e){if(r(e)&&t instanceof a.Date){const o=this.variableFor(e);return o&&o.hasValue?t.toValue(o.value):void 0}return t.toValue(s(e)?e:l.UTC)}verify(){const t=this.valueOf();if(t){const e=this.getValue(t.slot,this.props.value),o=t.hasValue?t.slot.toValue(t.value):void 0;switch(this.props.mode){case"equal":return o===e;case"not-equal":return o!==e;case"before":return s(e)&&s(o)&&o<e;case"after":return s(e)&&s(o)&&o>e;case"between":case"not-between":const r=this.getValue(t.slot,this.props.to);return s(e)&&s(r)&&(s(o)&&o>=i.min(e,r)&&o<=i.max(e,r))===("between"===this.props.mode);case"defined":return t.hasValue;case"undefined":return!t.hasValue}}return!1}};d([e],p.prototype,"verify",null),p=d([t({type:"condition",legacyBlock:!0,identifier:"@tripetto/block-date"})],p);class f extends u{constructor(){super(...arguments),this.dateSlot=n(this.valueOf("date")),this.toSlot=this.valueOf("to"),this.required=this.dateSlot.slot.required||!1}get fromError(){return!this.dateSlot.hasValue||!1}get toError(){return!this.toSlot||!this.toSlot.hasValue||this.toSlot.value<this.dateSlot.value}get toPlaceholder(){return h(this.props.placeholder||"",this.context)||""}validate(){return!this.props.range||(this.toSlot&&this.dateSlot.hasValue===this.toSlot.hasValue&&this.toSlot.value>=this.dateSlot.value||!1)}}d([c],f.prototype,"validate",null);export{f as DateTime};