fonteva-design-guide
Version:
## Dev, Build and Test
212 lines (183 loc) • 6.58 kB
JavaScript
import { api, track } from 'lwc';
import { loadStyle } from 'lightning/platformResourceLoader';
import BASE from '@salesforce/resourceUrl/PFM_Base';
import PFM_Element from 'c/pfmElement';
import Pfm_Loader_State_Loading from '@salesforce/label/c.Pfm_Loader_State_Loading';
export default class PfmButton extends PFM_Element {
static delegatesFocus = true;
cref;
ctext;
hasCref;
// Native button attributes
autofocus;
disabled;
form;
formaction;
formenctype;
formmethod;
formnovalidate;
target;
name;
type;
value;
textAlign; // left, right
// style classes
subtype; // DEPRECATED
variant; // default, outline, success, danger, link, icon, flex
backend; // boolean
expand; // boolean: allow button to expand to parent width
additionalClasses; // should not be used in portal. should only be used in older products such as events
// loader
disableLoader;
loaderState;
group;
label; // string to be displayed inside of button
labelClass;
theme; // open attribute, but will be overwritten by parent component
disableClick = false; // boolean
// Deprecated
submit; // boolean: Only for legacy code where the type is not used.
refreshAttributes() {
this.connectedCallback();
}
classes;
isSubmit;
iconVariant;
loaderCondition;
status = false;
pfmLoaderStateLoading = Pfm_Loader_State_Loading
isRendered = false;
excludedAttributes = [
'group',
'disableLoader',
'disableClick',
'theme',
'label',
'labelClass',
'icon',
'iconPosition',
'iconSize',
'iconRight',
'iconRightSize',
'variant',
'backend',
'expand',
'additionalClasses',
'textAlign'
];
connectedCallback() {
this.hasCref = this.cref && !this.ctext;
if (this.disableLoader === undefined) {
this.disableLoader = this.variant && this.variant.includes('link'); // everything defaults to a loader except links
}
this.loadDependentStyles();
this.loaderState = this.disableLoader ? 'slds-hide' : 'pfm-button_loader';
this.mapTypeToVariant();
this.setClasses();
if (this.icon && this.iconPosition === 'right') {
this.rightIcon = true;
this.iconRightSize = this.iconSize;
this.iconRightClass = this.iconClass;
this.iconRightLabel = this.iconLabel;
}
}
mapTypeToVariant() {
if (PFM_Element.getStyleVariants().includes(this.type)) {
this.variant = this.type;
this.type = typeof this.submit === 'boolean' ? 'submit' : '';
}
}
renderedCallback() {
if (typeof this.label === 'number') {
this.label = this.label + '';
}
if (this.disabled || this.disableClick) {
this.classList.add('js-disabled');
} else {
this.classList.remove('js-disabled');
}
if (this.ariaExpanded.length > 0 && this.ariaExpanded) {
this.template.querySelector('button').setAttribute('aria-expanded', true);
} else if (this.ariaExpanded.length > 0 && !this.ariaExpanded) {
this.template.querySelector('button').setAttribute('aria-expanded', false);
}
this.leftIcon = this.icon && this.iconPosition === 'left';
this.rightIcon = this.rightIcon || this.iconRight;
// console.warn('button dataset', this.isRendered, this.dataset, this.template.dataset);
super.initializeElement(this, super.__proto__, 'button', this.excludedAttributes, this.isRendered);
this.isRendered = true;
}
// loading methods
startLoader() {
if (this.disableLoader) {
return;
}
this.classList.add('js-disabled');
const btn = this.template.querySelector('button');
btn.classList.add('js-load');
btn.removeAttribute('aria-hidden');
if (this.type !== 'submit') {
const changeEvent = new CustomEvent('submit', {
detail: {}
});
this.dispatchEvent(changeEvent);
}
}
endLoader() {
this.classList.remove('js-disabled');
const btn = this.template.querySelector('button');
btn.setAttribute('aria-hidden', 'true');
btn.classList.remove('js-load');
btn.classList.add('js-load-transition');
setTimeout(function () {
btn.classList.remove('js-load-transition');
}, 1000);
}
setClasses() {
const classes = ['pfm-button'];
if (this.expand) {
classes.push('pfm-button_expand');
}
if (this.variant) {
this.variant.split(' ').forEach(v => classes.push(`pfm-button_${v}`));
} else {
classes.push('pfm-button_default');
}
if (this.additionalClasses) {
this.additionalClasses.split(' ').forEach(c => classes.push(c));
}
if (this.backend) {
classes.push('pfm-button_backend');
}
if (this.theme) {
classes.push(`pfm-theme_${this.theme}`);
}
if (this.textAlign === 'left') {
classes.push('left');
}
if (this.textAlign === 'right') {
classes.push('right');
}
this.classes = classes.join(' ');
}
loadDependentStyles() {
if (!window.pfmButtonStyleLoaded) {
loadStyle(this, BASE + '/css/component/button/button.css');
loadStyle(this, BASE + '/css/component/button/button-animation.css');
window.pfmButtonStyleLoaded = true;
}
}
rotateIcon() {
if (this.icon === 'utility:chevrondown') {
this.icon = 'utility:chevronright';
} else if (this.icon === 'utility:chevronright') {
this.icon = 'utility:chevrondown';
}
}
// data-binding from Aura is not reliable; use this instead
setLabel(label) {
this.label = label;
}
}