UNPKG

15.2 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.DetailsColumnBase = void 0;
4var tslib_1 = require("tslib");
5var React = require("react");
6var Icon_1 = require("../../Icon");
7var Utilities_1 = require("../../Utilities");
8var DetailsList_types_1 = require("./DetailsList.types");
9var DetailsRow_styles_1 = require("./DetailsRow.styles");
10var MOUSEDOWN_PRIMARY_BUTTON = 0; // for mouse down event we are using ev.button property, 0 means left button
11var getClassNames = Utilities_1.classNamesFunction();
12var TRANSITION_DURATION_DRAG = 200; // ms
13var TRANSITION_DURATION_DROP = 1500; // ms
14var CLASSNAME_ADD_INTERVAL = 20; // ms
15var defaultOnRenderHeader = function (classNames) { return function (props) {
16 if (!props) {
17 return null;
18 }
19 if (props.column.isIconOnly) {
20 return React.createElement("span", { className: classNames.accessibleLabel }, props.column.name);
21 }
22 return React.createElement(React.Fragment, null, props.column.name);
23}; };
24/**
25 * Component for rendering columns in a `DetailsList`.
26 *
27 * {@docCategory DetailsList}
28 */
29var DetailsColumnBase = /** @class */ (function (_super) {
30 tslib_1.__extends(DetailsColumnBase, _super);
31 function DetailsColumnBase(props) {
32 var _this = _super.call(this, props) || this;
33 _this._root = React.createRef();
34 _this._onRenderFilterIcon = function (classNames) { return function (props) {
35 var columnProps = props.columnProps, iconProps = tslib_1.__rest(props, ["columnProps"]);
36 var IconComponent = (columnProps === null || columnProps === void 0 ? void 0 : columnProps.useFastIcons) ? Icon_1.FontIcon : Icon_1.Icon;
37 return React.createElement(IconComponent, tslib_1.__assign({}, iconProps));
38 }; };
39 _this._onRenderColumnHeaderTooltip = function (tooltipHostProps) {
40 return React.createElement("span", { className: tooltipHostProps.hostClassName }, tooltipHostProps.children);
41 };
42 _this._onColumnClick = function (ev) {
43 var _a = _this.props, onColumnClick = _a.onColumnClick, column = _a.column;
44 if (column.columnActionsMode === DetailsList_types_1.ColumnActionsMode.disabled) {
45 return;
46 }
47 if (column.onColumnClick) {
48 column.onColumnClick(ev, column);
49 }
50 if (onColumnClick) {
51 onColumnClick(ev, column);
52 }
53 };
54 _this._onDragStart = function (item, itemIndex, selectedItems, event) {
55 var classNames = _this._classNames;
56 if (itemIndex) {
57 _this._updateHeaderDragInfo(itemIndex);
58 _this._root.current.classList.add(classNames.borderWhileDragging);
59 _this._async.setTimeout(function () {
60 if (_this._root.current) {
61 _this._root.current.classList.add(classNames.noBorderWhileDragging);
62 }
63 }, CLASSNAME_ADD_INTERVAL);
64 }
65 };
66 _this._onDragEnd = function (item, event) {
67 var classNames = _this._classNames;
68 if (event) {
69 _this._updateHeaderDragInfo(-1, event);
70 }
71 _this._root.current.classList.remove(classNames.borderWhileDragging);
72 _this._root.current.classList.remove(classNames.noBorderWhileDragging);
73 };
74 _this._updateHeaderDragInfo = function (itemIndex, event) {
75 /* eslint-disable deprecation/deprecation */
76 if (_this.props.setDraggedItemIndex) {
77 _this.props.setDraggedItemIndex(itemIndex);
78 }
79 /* eslint-enable deprecation/deprecation */
80 if (_this.props.updateDragInfo) {
81 _this.props.updateDragInfo({ itemIndex: itemIndex }, event);
82 }
83 };
84 _this._onColumnContextMenu = function (ev) {
85 var _a = _this.props, onColumnContextMenu = _a.onColumnContextMenu, column = _a.column;
86 if (column.onColumnContextMenu) {
87 column.onColumnContextMenu(column, ev);
88 ev.preventDefault();
89 }
90 if (onColumnContextMenu) {
91 onColumnContextMenu(column, ev);
92 ev.preventDefault();
93 }
94 };
95 _this._onRootMouseDown = function (ev) {
96 var isDraggable = _this.props.isDraggable;
97 // Ignore anything except the primary button.
98 if (isDraggable && ev.button === MOUSEDOWN_PRIMARY_BUTTON) {
99 ev.stopPropagation();
100 }
101 };
102 Utilities_1.initializeComponentRef(_this);
103 _this._async = new Utilities_1.Async(_this);
104 _this._events = new Utilities_1.EventGroup(_this);
105 return _this;
106 }
107 DetailsColumnBase.prototype.render = function () {
108 var _a = this.props, column = _a.column, parentId = _a.parentId, isDraggable = _a.isDraggable, styles = _a.styles, theme = _a.theme, _b = _a.cellStyleProps, cellStyleProps = _b === void 0 ? DetailsRow_styles_1.DEFAULT_CELL_STYLE_PROPS : _b, _c = _a.useFastIcons, useFastIcons = _c === void 0 ? true : _c;
109 var _d = this.props.onRenderColumnHeaderTooltip, onRenderColumnHeaderTooltip = _d === void 0 ? this._onRenderColumnHeaderTooltip : _d;
110 this._classNames = getClassNames(styles, {
111 theme: theme,
112 headerClassName: column.headerClassName,
113 iconClassName: column.iconClassName,
114 isActionable: column.columnActionsMode !== DetailsList_types_1.ColumnActionsMode.disabled,
115 isEmpty: !column.name,
116 isIconVisible: column.isSorted || column.isGrouped || column.isFiltered,
117 isPadded: column.isPadded,
118 isIconOnly: column.isIconOnly,
119 cellStyleProps: cellStyleProps,
120 transitionDurationDrag: TRANSITION_DURATION_DRAG,
121 transitionDurationDrop: TRANSITION_DURATION_DROP,
122 });
123 var classNames = this._classNames;
124 var IconComponent = useFastIcons ? Icon_1.FontIcon : Icon_1.Icon;
125 var onRenderFilterIcon = column.onRenderFilterIcon
126 ? Utilities_1.composeRenderFunction(column.onRenderFilterIcon, this._onRenderFilterIcon(this._classNames))
127 : this._onRenderFilterIcon(this._classNames);
128 var onRenderHeader = column.onRenderHeader
129 ? Utilities_1.composeRenderFunction(column.onRenderHeader, defaultOnRenderHeader(this._classNames))
130 : defaultOnRenderHeader(this._classNames);
131 var hasInnerButton = column.columnActionsMode !== DetailsList_types_1.ColumnActionsMode.disabled &&
132 (column.onColumnClick !== undefined || this.props.onColumnClick !== undefined);
133 var accNameDescription = {
134 'aria-label': column.ariaLabel ? column.ariaLabel : column.isIconOnly ? column.name : undefined,
135 'aria-labelledby': column.ariaLabel || column.isIconOnly ? undefined : parentId + "-" + column.key + "-name",
136 'aria-describedby': !this.props.onRenderColumnHeaderTooltip && this._hasAccessibleDescription()
137 ? parentId + "-" + column.key + "-tooltip"
138 : undefined,
139 };
140 return (React.createElement(React.Fragment, null,
141 React.createElement("div", tslib_1.__assign({ key: column.key, ref: this._root, role: 'columnheader' }, (!hasInnerButton && accNameDescription), { "aria-sort": column.isSorted ? (column.isSortedDescending ? 'descending' : 'ascending') : 'none', "data-is-focusable": !hasInnerButton && column.columnActionsMode !== DetailsList_types_1.ColumnActionsMode.disabled ? 'true' : undefined, className: classNames.root, "data-is-draggable": isDraggable, draggable: isDraggable, style: {
142 width: column.calculatedWidth +
143 cellStyleProps.cellLeftPadding +
144 cellStyleProps.cellRightPadding +
145 (column.isPadded ? cellStyleProps.cellExtraRightPadding : 0),
146 }, "data-automationid": 'ColumnsHeaderColumn', "data-item-key": column.key }),
147 isDraggable && (React.createElement(IconComponent, { iconName: "GripperBarVertical", className: classNames.gripperBarVerticalStyle })),
148 onRenderColumnHeaderTooltip({
149 hostClassName: classNames.cellTooltip,
150 id: parentId + "-" + column.key + "-tooltip",
151 setAriaDescribedBy: false,
152 column: column,
153 content: column.columnActionsMode !== DetailsList_types_1.ColumnActionsMode.disabled ? column.ariaLabel : '',
154 children: (React.createElement("span", tslib_1.__assign({ id: parentId + "-" + column.key, className: classNames.cellTitle, "data-is-focusable": hasInnerButton && column.columnActionsMode !== DetailsList_types_1.ColumnActionsMode.disabled ? 'true' : undefined, role: hasInnerButton ? 'button' : undefined }, (hasInnerButton && accNameDescription), { onContextMenu: this._onColumnContextMenu, onClick: this._onColumnClick, "aria-haspopup": column.columnActionsMode === DetailsList_types_1.ColumnActionsMode.hasDropdown ? 'menu' : undefined, "aria-expanded": column.columnActionsMode === DetailsList_types_1.ColumnActionsMode.hasDropdown ? !!column.isMenuOpen : undefined }),
155 React.createElement("span", { id: parentId + "-" + column.key + "-name", className: classNames.cellName },
156 (column.iconName || column.iconClassName) && (React.createElement(IconComponent, { className: classNames.iconClassName, iconName: column.iconName })),
157 onRenderHeader(this.props)),
158 column.isFiltered && React.createElement(IconComponent, { className: classNames.nearIcon, iconName: "Filter" }),
159 (column.isSorted || column.showSortIconWhenUnsorted) && (React.createElement(IconComponent, { className: classNames.sortIcon, iconName: column.isSorted ? (column.isSortedDescending ? 'SortDown' : 'SortUp') : 'Sort' })),
160 column.isGrouped && React.createElement(IconComponent, { className: classNames.nearIcon, iconName: "GroupedDescending" }),
161 column.columnActionsMode === DetailsList_types_1.ColumnActionsMode.hasDropdown &&
162 !column.isIconOnly &&
163 onRenderFilterIcon({
164 'aria-hidden': true,
165 columnProps: this.props,
166 className: classNames.filterChevron,
167 iconName: 'ChevronDown',
168 }))),
169 }, this._onRenderColumnHeaderTooltip)),
170 !this.props.onRenderColumnHeaderTooltip ? this._renderAccessibleDescription() : null));
171 };
172 DetailsColumnBase.prototype.componentDidMount = function () {
173 var _this = this;
174 if (this.props.dragDropHelper && this.props.isDraggable) {
175 this._addDragDropHandling();
176 }
177 var classNames = this._classNames;
178 if (this.props.isDropped) {
179 if (this._root.current) {
180 this._root.current.classList.add(classNames.borderAfterDropping);
181 this._async.setTimeout(function () {
182 if (_this._root.current) {
183 _this._root.current.classList.add(classNames.noBorderAfterDropping);
184 }
185 }, CLASSNAME_ADD_INTERVAL);
186 }
187 this._async.setTimeout(function () {
188 if (_this._root.current) {
189 _this._root.current.classList.remove(classNames.borderAfterDropping);
190 _this._root.current.classList.remove(classNames.noBorderAfterDropping);
191 }
192 }, TRANSITION_DURATION_DROP + CLASSNAME_ADD_INTERVAL);
193 }
194 };
195 DetailsColumnBase.prototype.componentWillUnmount = function () {
196 if (this._dragDropSubscription) {
197 this._dragDropSubscription.dispose();
198 delete this._dragDropSubscription;
199 }
200 this._async.dispose();
201 this._events.dispose();
202 };
203 DetailsColumnBase.prototype.componentDidUpdate = function () {
204 if (!this._dragDropSubscription && this.props.dragDropHelper && this.props.isDraggable) {
205 this._addDragDropHandling();
206 }
207 if (this._dragDropSubscription && !this.props.isDraggable) {
208 this._dragDropSubscription.dispose();
209 this._events.off(this._root.current, 'mousedown');
210 delete this._dragDropSubscription;
211 }
212 };
213 DetailsColumnBase.prototype._getColumnDragDropOptions = function () {
214 var _this = this;
215 var columnIndex = this.props.columnIndex;
216 var options = {
217 selectionIndex: columnIndex,
218 context: { data: columnIndex, index: columnIndex },
219 canDrag: function () { return _this.props.isDraggable; },
220 canDrop: function () { return false; },
221 onDragStart: this._onDragStart,
222 updateDropState: function () { return undefined; },
223 onDrop: function () { return undefined; },
224 onDragEnd: this._onDragEnd,
225 };
226 return options;
227 };
228 DetailsColumnBase.prototype._hasAccessibleDescription = function () {
229 var column = this.props.column;
230 return !!(column.filterAriaLabel ||
231 column.sortAscendingAriaLabel ||
232 column.sortDescendingAriaLabel ||
233 column.groupAriaLabel ||
234 column.sortableAriaLabel);
235 };
236 DetailsColumnBase.prototype._renderAccessibleDescription = function () {
237 var _a = this.props, column = _a.column, parentId = _a.parentId;
238 var classNames = this._classNames;
239 return this._hasAccessibleDescription() && !this.props.onRenderColumnHeaderTooltip ? (React.createElement("label", { key: column.key + "_label", id: parentId + "-" + column.key + "-tooltip", className: classNames.accessibleLabel, hidden: true },
240 (column.isFiltered && column.filterAriaLabel) || null,
241 ((column.isSorted || column.showSortIconWhenUnsorted) &&
242 (column.isSorted
243 ? column.isSortedDescending
244 ? column.sortDescendingAriaLabel
245 : column.sortAscendingAriaLabel
246 : column.sortableAriaLabel)) ||
247 null,
248 (column.isGrouped && column.groupAriaLabel) || null)) : null;
249 };
250 DetailsColumnBase.prototype._addDragDropHandling = function () {
251 this._dragDropSubscription = this.props.dragDropHelper.subscribe(this._root.current, this._events, this._getColumnDragDropOptions());
252 // We need to use native on this to prevent MarqueeSelection from handling the event before us.
253 this._events.on(this._root.current, 'mousedown', this._onRootMouseDown);
254 };
255 return DetailsColumnBase;
256}(React.Component));
257exports.DetailsColumnBase = DetailsColumnBase;
258//# sourceMappingURL=DetailsColumn.base.js.map
\No newline at end of file