@fluentui/react
Version:
Reusable React components for building web experiences.
178 lines • 11.2 kB
JavaScript
import { __assign, __extends } from "tslib";
import * as React from 'react';
import { buttonProperties, classNamesFunction, getId, getNativeProps, initializeComponentRef } from '../../Utilities';
import { OverflowButtonType } from './Facepile.types';
import { FacepileButton } from './FacepileButton';
import { Icon } from '../../Icon';
import { Persona } from '../../Persona';
import { PersonaCoin, PersonaSize, PersonaInitialsColor } from '../../PersonaCoin';
var getClassNames = classNamesFunction();
/**
* FacePile with no default styles.
* [Use the `styles` API to add your own styles.](https://github.com/microsoft/fluentui/wiki/Component-Styling)
*/
export var FacepileBase = /** @class */ (function (_super) {
__extends(FacepileBase, _super);
function FacepileBase(props) {
var _this = _super.call(this, props) || this;
_this._classNames = getClassNames(_this.props.styles, {
theme: _this.props.theme,
className: _this.props.className,
});
_this._getPersonaControl = function (persona) {
var _a;
var getPersonaProps = (_a = _this.props, _a.getPersonaProps), personaSize = _a.personaSize;
var personaStyles = {
details: {
flex: '1 0 auto',
},
};
return (React.createElement(Persona, __assign({ imageInitials: persona.imageInitials, imageUrl: persona.imageUrl, initialsColor: persona.initialsColor, allowPhoneInitials: persona.allowPhoneInitials, text: persona.personaName, size: personaSize }, (getPersonaProps ? getPersonaProps(persona) : null), { styles: personaStyles })));
};
_this._getPersonaCoinControl = function (persona) {
var _a;
var getPersonaProps = (_a = _this.props, _a.getPersonaProps), personaSize = _a.personaSize;
return (React.createElement(PersonaCoin, __assign({ imageInitials: persona.imageInitials, imageUrl: persona.imageUrl, initialsColor: persona.initialsColor, allowPhoneInitials: persona.allowPhoneInitials, text: persona.personaName, size: personaSize }, (getPersonaProps ? getPersonaProps(persona) : null))));
};
initializeComponentRef(_this);
_this._ariaDescriptionId = getId();
return _this;
}
FacepileBase.prototype.render = function () {
var _a, _b;
var overflowButtonProps = this.props.overflowButtonProps;
var chevronButtonProps = (_a = this.props, _a.chevronButtonProps), // eslint-disable-line deprecation/deprecation
maxDisplayablePersonas = _a.maxDisplayablePersonas, personas = _a.personas, overflowPersonas = _a.overflowPersonas, showAddButton = _a.showAddButton, ariaLabel = _a.ariaLabel, showTooltip = (_b = _a.showTooltip, _b === void 0 ? true : _b);
var _classNames = this._classNames;
// Add a check to make sure maxDisplayalePersonas is defined to cover the edge case of it being 0.
var numPersonasToShow = typeof maxDisplayablePersonas === 'number' ? Math.min(personas.length, maxDisplayablePersonas) : personas.length;
// Added for deprecating chevronButtonProps. Can remove after v1.0
if (chevronButtonProps && !overflowButtonProps) {
overflowButtonProps = chevronButtonProps;
}
var hasOverflowPersonas = overflowPersonas && overflowPersonas.length > 0;
var personasPrimary = hasOverflowPersonas ? personas : personas.slice(0, numPersonasToShow);
var personasOverflow = (hasOverflowPersonas ? overflowPersonas : personas.slice(numPersonasToShow)) || [];
return (React.createElement("div", { className: _classNames.root },
this.onRenderAriaDescription(),
React.createElement("div", { className: _classNames.itemContainer },
showAddButton ? this._getAddNewElement() : null,
React.createElement("ul", { className: _classNames.members, "aria-label": ariaLabel }, this._onRenderVisiblePersonas(personasPrimary, personasOverflow.length === 0 && personas.length === 1, showTooltip)),
overflowButtonProps ? this._getOverflowElement(personasOverflow) : null)));
};
FacepileBase.prototype.onRenderAriaDescription = function () {
var ariaDescription = this.props.ariaDescription;
var _classNames = this._classNames;
// If ariaDescription is given, descriptionId will be assigned to ariaDescriptionSpan,
// otherwise it will be assigned to descriptionSpan.
return (ariaDescription && (React.createElement("span", { className: _classNames.screenReaderOnly, id: this._ariaDescriptionId }, ariaDescription)));
};
FacepileBase.prototype._onRenderVisiblePersonas = function (personas, singlePersona, showTooltip) {
var _a, _b, _c;
var _this = this;
var onRenderPersona = (_a = this.props, _b = _a.onRenderPersona, _b === void 0 ? this._getPersonaControl : _b), onRenderPersonaCoin = (_c = _a.onRenderPersonaCoin, _c === void 0 ? this._getPersonaCoinControl : _c), onRenderPersonaWrapper = _a.onRenderPersonaWrapper;
return personas.map(function (persona, index) {
var personaControl = singlePersona
? onRenderPersona(persona, _this._getPersonaControl)
: onRenderPersonaCoin(persona, _this._getPersonaCoinControl);
var defaultPersonaRender = persona.onClick
? function () { return _this._getElementWithOnClickEvent(personaControl, persona, showTooltip, index); }
: function () { return _this._getElementWithoutOnClickEvent(personaControl, persona, showTooltip, index); };
return (React.createElement("li", { key: "".concat(singlePersona ? 'persona' : 'personaCoin', "-").concat(index), className: _this._classNames.member }, onRenderPersonaWrapper ? onRenderPersonaWrapper(persona, defaultPersonaRender) : defaultPersonaRender()));
});
};
FacepileBase.prototype._getElementWithOnClickEvent = function (personaControl, persona, showTooltip, index) {
var keytipProps = persona.keytipProps;
return (React.createElement(FacepileButton, __assign({}, getNativeProps(persona, buttonProperties), this._getElementProps(persona, showTooltip, index), { keytipProps: keytipProps,
// eslint-disable-next-line react/jsx-no-bind
onClick: this._onPersonaClick.bind(this, persona) }), personaControl));
};
FacepileBase.prototype._getElementWithoutOnClickEvent = function (personaControl, persona, showTooltip, index) {
return (React.createElement("div", __assign({}, getNativeProps(persona, buttonProperties), this._getElementProps(persona, showTooltip, index)), personaControl));
};
FacepileBase.prototype._getElementProps = function (persona, showTooltip, index) {
var _classNames = this._classNames;
return {
key: (persona.imageUrl ? 'i' : '') + index,
'data-is-focusable': true,
className: _classNames.itemButton,
title: showTooltip ? persona.personaName : undefined,
onMouseMove: this._onPersonaMouseMove.bind(this, persona),
onMouseOut: this._onPersonaMouseOut.bind(this, persona),
};
};
FacepileBase.prototype._getOverflowElement = function (personasOverflow) {
switch (this.props.overflowButtonType) {
case OverflowButtonType.descriptive:
return this._getDescriptiveOverflowElement(personasOverflow);
case OverflowButtonType.downArrow:
return this._getIconElement('ChevronDown');
case OverflowButtonType.more:
return this._getIconElement('More');
default:
return null;
}
};
FacepileBase.prototype._getDescriptiveOverflowElement = function (personasOverflow) {
var personaSize = this.props.personaSize;
if (!personasOverflow || personasOverflow.length < 1) {
return null;
}
var personaNames = personasOverflow.map(function (p) { return p.personaName; }).join(', ');
var overflowButtonProps = __assign({ title: personaNames }, this.props.overflowButtonProps);
var numPersonasNotPictured = Math.max(personasOverflow.length, 0);
var _classNames = this._classNames;
return (React.createElement(FacepileButton, __assign({}, overflowButtonProps, { ariaDescription: overflowButtonProps.title, className: _classNames.descriptiveOverflowButton }),
React.createElement(PersonaCoin, { size: personaSize, onRenderInitials: this._renderInitialsNotPictured(numPersonasNotPictured), initialsColor: PersonaInitialsColor.transparent })));
};
FacepileBase.prototype._getIconElement = function (icon) {
var _a;
var overflowButtonProps = (_a = this.props, _a.overflowButtonProps), personaSize = _a.personaSize;
var overflowInitialsIcon = true;
var _classNames = this._classNames;
return (React.createElement(FacepileButton, __assign({}, overflowButtonProps, { className: _classNames.overflowButton }),
React.createElement(PersonaCoin, { size: personaSize, onRenderInitials: this._renderInitials(icon, overflowInitialsIcon), initialsColor: PersonaInitialsColor.transparent })));
};
FacepileBase.prototype._getAddNewElement = function () {
var _a;
var addButtonProps = (_a = this.props, _a.addButtonProps), personaSize = _a.personaSize;
var _classNames = this._classNames;
return (React.createElement(FacepileButton, __assign({}, addButtonProps, { className: _classNames.addButton }),
React.createElement(PersonaCoin, { size: personaSize, onRenderInitials: this._renderInitials('AddFriend') })));
};
FacepileBase.prototype._onPersonaClick = function (persona, ev) {
persona.onClick(ev, persona);
ev.preventDefault();
ev.stopPropagation();
};
FacepileBase.prototype._onPersonaMouseMove = function (persona, ev) {
if (persona.onMouseMove) {
persona.onMouseMove(ev, persona);
}
};
FacepileBase.prototype._onPersonaMouseOut = function (persona, ev) {
if (persona.onMouseOut) {
persona.onMouseOut(ev, persona);
}
};
FacepileBase.prototype._renderInitials = function (iconName, overflowButton) {
var _classNames = this._classNames;
return function () {
return React.createElement(Icon, { iconName: iconName, className: overflowButton ? _classNames.overflowInitialsIcon : '' });
};
};
FacepileBase.prototype._renderInitialsNotPictured = function (numPersonasNotPictured) {
var _classNames = this._classNames;
return function () {
return (React.createElement("span", { className: _classNames.overflowInitialsIcon }, numPersonasNotPictured < 100 ? '+' + numPersonasNotPictured : '99+'));
};
};
FacepileBase.defaultProps = {
maxDisplayablePersonas: 5,
personas: [],
overflowPersonas: [],
personaSize: PersonaSize.size32,
};
return FacepileBase;
}(React.Component));
//# sourceMappingURL=Facepile.base.js.map