@pnp/spfx-controls-react
Version:
Reusable React controls for SharePoint Framework solutions
152 lines • 7.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContentTypePicker = void 0;
var tslib_1 = require("tslib");
var React = tslib_1.__importStar(require("react"));
var cloneDeep_1 = tslib_1.__importDefault(require("lodash/cloneDeep"));
var Dropdown_1 = require("@fluentui/react/lib/Dropdown");
var Spinner_1 = require("@fluentui/react/lib/Spinner");
var telemetry = tslib_1.__importStar(require("../../common/telemetry"));
var SPServiceFactory_1 = require("../../services/SPServiceFactory");
var EMPTY_CONTENTTYPE_KEY = 'NO_CONTENTTYPE_SELECTED';
var ContentTypePicker = /** @class */ (function (_super) {
tslib_1.__extends(ContentTypePicker, _super);
function ContentTypePicker(props) {
var _this = _super.call(this, props) || this;
_this._selectedContentTypes = null;
/**
* Fires when an item has been selected from the dropdown.
* @param event Event that has been fired.
* @param option The new selection.
* @param index Index of the selection.
*/
_this.onChange = function (event, option, index) {
var _a = _this.props, multiSelect = _a.multiSelect, onSelectionChanged = _a.onSelectionChanged;
var contentTypes = _this.state.contentTypes;
if (multiSelect) {
var selectedContentTypes = _this._selectedContentTypes ? (0, cloneDeep_1.default)(_this._selectedContentTypes) : [];
if (option.selected) {
selectedContentTypes.push(option.key.toString());
}
else {
selectedContentTypes = selectedContentTypes.filter(function (ct) { return ct !== option.key; });
}
_this._selectedContentTypes = selectedContentTypes;
}
else {
_this._selectedContentTypes = option.key.toString();
}
if (onSelectionChanged) {
if (multiSelect) {
onSelectionChanged((0, cloneDeep_1.default)(contentTypes.filter(function (ct) { return _this._selectedContentTypes.some(function (sct) { return ct.StringId === sct; }); })));
}
else {
onSelectionChanged((0, cloneDeep_1.default)(contentTypes.find(function (ct) { return ct.StringId === _this._selectedContentTypes; })));
}
}
};
telemetry.track('ReactContentTypePicker');
_this.state = {
contentTypes: [],
loading: false,
};
return _this;
}
ContentTypePicker.prototype.componentDidMount = function () {
this.loadContentTypes().then(function () { }).catch(function () { });
};
ContentTypePicker.prototype.loadContentTypes = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var _a, context, listId, includeHidden, includeReadOnly, orderBy, filter, group, webAbsoluteUrl, filterItems, service, results;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = this.props, context = _a.context, listId = _a.listId, includeHidden = _a.includeHidden, includeReadOnly = _a.includeReadOnly, orderBy = _a.orderBy, filter = _a.filter, group = _a.group, webAbsoluteUrl = _a.webAbsoluteUrl, filterItems = _a.filterItems;
// Show the loading indicator and disable the dropdown
this.setState({ loading: true });
service = SPServiceFactory_1.SPServiceFactory.createService(context, true, 5000, webAbsoluteUrl);
return [4 /*yield*/, service.getContentTypes({
listId: listId,
filter: filter,
includeHidden: includeHidden,
includeReadOnly: includeReadOnly,
orderBy: orderBy,
group: group,
})];
case 1:
results = _b.sent();
// Check if custom filter is specified
if (filterItems) {
results = filterItems(results);
}
// Hide the loading indicator and set the dropdown options
this.setState({
loading: false,
contentTypes: results,
});
this.setSelectedContentTypes();
return [2 /*return*/];
}
});
});
};
/**
* Set the currently selected content type(s).
*/
ContentTypePicker.prototype.setSelectedContentTypes = function () {
this._selectedContentTypes = (0, cloneDeep_1.default)(this.props.selectedContentTypes);
this.setState({
selectedContentTypes: this._selectedContentTypes,
});
};
ContentTypePicker.prototype.componentDidUpdate = function (prevProps, prevState) {
var _a = this.props, includeHidden = _a.includeHidden, includeReadOnly = _a.includeReadOnly, orderBy = _a.orderBy, webAbsoluteUrl = _a.webAbsoluteUrl, selectedContentTypes = _a.selectedContentTypes, listId = _a.listId;
if (prevProps.includeHidden !== includeHidden ||
prevProps.includeReadOnly !== includeReadOnly ||
prevProps.orderBy !== orderBy ||
prevProps.webAbsoluteUrl !== webAbsoluteUrl ||
prevProps.listId !== listId) {
this.loadContentTypes().then(function () { }).catch(function () { });
}
if (prevProps.selectedContentTypes !== selectedContentTypes) {
this.setSelectedContentTypes();
}
};
ContentTypePicker.prototype.render = function () {
var _a = this.state, loading = _a.loading, contentTypes = _a.contentTypes, selectedContentTypes = _a.selectedContentTypes;
var _b = this.props, className = _b.className, disabled = _b.disabled, multiSelect = _b.multiSelect, label = _b.label, placeholder = _b.placeholder, showBlankOption = _b.showBlankOption;
var options = contentTypes.map(function (f) { return ({
key: f.StringId,
text: f.Name,
}); });
if (showBlankOption && !multiSelect) {
// Provide empty option
options.unshift({
key: EMPTY_CONTENTTYPE_KEY,
text: '',
});
}
var dropdownProps = {
className: className,
options: options,
disabled: loading || disabled,
label: label,
placeholder: placeholder,
onChange: this.onChange,
styles: this.props.styles
};
if (multiSelect) {
dropdownProps.multiSelect = true;
dropdownProps.selectedKeys = selectedContentTypes;
}
else {
dropdownProps.selectedKey = selectedContentTypes;
}
return (React.createElement(React.Fragment, null,
loading && React.createElement(Spinner_1.Spinner, { size: Spinner_1.SpinnerSize.xSmall }),
React.createElement(Dropdown_1.Dropdown, tslib_1.__assign({}, dropdownProps))));
};
return ContentTypePicker;
}(React.Component));
exports.ContentTypePicker = ContentTypePicker;
//# sourceMappingURL=ContentTypePicker.js.map