fonteva-design-guide
Version:
## Dev, Build and Test
149 lines (127 loc) • 5.43 kB
JavaScript
import { LightningElement, api, track } from 'lwc';
import { loadStyle } from 'lightning/platformResourceLoader';
import BASE from '@salesforce/resourceUrl/PFM_Base';
import { fireEvent } from 'c/actionutils';
export default class PfmInputDateRange extends LightningElement {
fromName; // name attribute for `from` input
fromValue; // value from wired list
fromReadOnly; // boolean for `from` input
fromTabIndex; // tab-index for `from` input
fromHelpText; // help text for `from` input
toName; // name attribute for `to` input
toValue; // value from wired list
toReadOnly; // boolean for `to` input
toTabIndex; // tab-index for `to` input
toHelpText; // help text for `to` input
required; // boolean
formatOptions;
errorMessageFromBeforeTo = 'From Date must be before To Date.';
label; // input parent label
cancelLabel = 'Cancel'; // cancel button label
applyLabel = 'Apply'; // apply button label
toLabel = 'To'; // label from `to` input
fromLabel = 'From'; // label from `from` input
backend; // boolean
backendLabelClass;
fromOutput; // displayed value in tile for `from`
toOutput; // displayed value in tile for `to`
classes;
showError = false;
newFromDate;
newToDate;
privateVal = {};
internalVal;
set val(newVal) {
this.internalVal = newVal;
this.update(newVal);
}
get val() {
return this.internalVal;
}
connectedCallback() {
this.loadDependentStyles();
const classBase = 'pfm-input_date-range';
const valBackend = this.backend;
let backend;
backend = valBackend ? '' : ' pfm-input_portal';
this.classes = classBase + backend;
this.update(this.val);
}
renderedCallback() {
if (this.backend) {
this.backendLabelClass = 'pfm-text_backend';
}
}
close() {
this.showError = false;
this.template.querySelector('.pfm-input_date-range-popover').classList.remove('js-popover');
this.template.querySelector('c-pfm-button[data-name="applyState"]').endLoader();
}
update(dateVal) {
if (dateVal) {
this.privateVal[this.fromValue] = dateVal[this.fromValue];
this.privateVal[this.toValue] = dateVal[this.toValue];
this.updateInputDates();
}
}
saveHandler() {
const fromInputVal = this.template.querySelector('c-pfm-input[data-name="' + this.fromName + '"]'),
toInputVal = this.template.querySelector('c-pfm-input[data-name="' + this.toName + '"]');
if (fromInputVal.localValue > toInputVal.localValue) {
this.showError = true;
this.template.querySelector('c-pfm-button[data-name="applyState"]').endLoader();
return;
}
this.privateVal[this.fromValue] = fromInputVal.localValue;
this.privateVal[this.toValue] = toInputVal.localValue;
fireEvent(this, 'save', {
fromDate: this.privateVal[this.fromValue],
toDate: this.privateVal[this.toValue]
});
this.updateDisplayRange();
}
updateDisplayRange() {
if (this.privateVal) {
this.fromOutput = this.privateVal[this.fromValue];
this.toOutput = this.privateVal[this.toValue];
}
}
openPopover() {
let popovers = document.querySelectorAll('.pfm-input_date-range-popover');
popovers.forEach(function (p) {
if (p.classList.contains('js-popover')) {
p.classList.remove('js-popover');
}
});
this.template.querySelector('.pfm-input_date-range-popover').classList.add('js-popover');
this.newFromDate = this.privateVal[this.fromValue];
this.newToDate = this.privateVal[this.toValue];
}
updateInputDates() {
const inputDateFieldsFound = this.template.querySelectorAll('div.pfm-input_date-range-popover c-pfm-row c-pfm-column').length > 1;
// const fromDateComp = this.template.querySelector('c-pfm-input[data-name="' + this.fromName + '"]');
const fromDateComp = inputDateFieldsFound && this.template.querySelectorAll('div.pfm-input_date-range-popover c-pfm-row c-pfm-column c-pfm-input')[0];
if (fromDateComp) {
fromDateComp.updateValue(this.privateVal[this.fromValue]);
}
// const toDateComp = this.template.querySelector('c-pfm-input[data-name="' + this.toName + '"]');
const toDateComp = inputDateFieldsFound && this.template.querySelectorAll('div.pfm-input_date-range-popover c-pfm-row c-pfm-column c-pfm-input')[1];
if (toDateComp) {
toDateComp.updateValue(this.privateVal[this.toValue]);
}
this.updateDisplayRange();
}
cancelPopover() {
this.updateInputDates();
this.close();
}
loadDependentStyles() {
if (!window.pfmDateRangeStylesLoaded) {
loadStyle(this, BASE + '/css/component/input/date-range.css');
window.pfmDateRangeStylesLoaded = true;
}
}
}