DocSpace-client/packages/asc-web-components/combobox/index.js
Alexey Safronov 3aeac42444 Merge branch 'master' into develop
# Conflicts:
#	common/ASC.Core.Common/Data/DbSubscriptionService.cs
#	common/ASC.Core.Common/Notify/Signalr/SignalrServiceClient.cs
#	common/services/ASC.ElasticSearch/Engine/FactoryIndexer.cs
#	common/services/ASC.ElasticSearch/Service/Settings.cs
#	common/services/ASC.Socket.IO.Svc/ASC.Socket.IO.Svc.csproj
#	common/services/ASC.Socket.IO.Svc/SocketServiceLauncher.cs
#	packages/asc-web-components/combobox/index.js
#	packages/asc-web-components/table-container/StyledTableContainer.js
#	packages/asc-web-components/table-container/TableGroupMenu.js
#	packages/asc-web-components/table-container/TableHeader.js
#	packages/asc-web-components/table-container/TableHeaderCell.js
#	packages/asc-web-components/table-container/TableRow.js
#	products/ASC.Files/Client/public/locales/az/Home.json
#	products/ASC.Files/Client/public/locales/bg/Home.json
#	products/ASC.Files/Client/public/locales/bg/Translations.json
#	products/ASC.Files/Client/public/locales/cs/Home.json
#	products/ASC.Files/Client/public/locales/de/ConvertDialog.json
#	products/ASC.Files/Client/public/locales/de/Home.json
#	products/ASC.Files/Client/public/locales/de/Translations.json
#	products/ASC.Files/Client/public/locales/de/UploadPanel.json
#	products/ASC.Files/Client/public/locales/el/ConvertDialog.json
#	products/ASC.Files/Client/public/locales/el/Home.json
#	products/ASC.Files/Client/public/locales/en/ConvertDialog.json
#	products/ASC.Files/Client/public/locales/en/Home.json
#	products/ASC.Files/Client/public/locales/en/Translations.json
#	products/ASC.Files/Client/public/locales/en/UploadPanel.json
#	products/ASC.Files/Client/public/locales/es/ConvertDialog.json
#	products/ASC.Files/Client/public/locales/es/Home.json
#	products/ASC.Files/Client/public/locales/es/UploadPanel.json
#	products/ASC.Files/Client/public/locales/fi/Home.json
#	products/ASC.Files/Client/public/locales/fr/ConvertDialog.json
#	products/ASC.Files/Client/public/locales/fr/Home.json
#	products/ASC.Files/Client/public/locales/fr/Translations.json
#	products/ASC.Files/Client/public/locales/it/ConvertDialog.json
#	products/ASC.Files/Client/public/locales/it/Home.json
#	products/ASC.Files/Client/public/locales/it/Translations.json
#	products/ASC.Files/Client/public/locales/it/UploadPanel.json
#	products/ASC.Files/Client/public/locales/ja/Home.json
#	products/ASC.Files/Client/public/locales/ja/UploadPanel.json
#	products/ASC.Files/Client/public/locales/lv/Home.json
#	products/ASC.Files/Client/public/locales/nl/Home.json
#	products/ASC.Files/Client/public/locales/nl/Translations.json
#	products/ASC.Files/Client/public/locales/pl/Home.json
#	products/ASC.Files/Client/public/locales/pt-BR/ConvertDialog.json
#	products/ASC.Files/Client/public/locales/pt-BR/Home.json
#	products/ASC.Files/Client/public/locales/pt-BR/Translations.json
#	products/ASC.Files/Client/public/locales/ro/Home.json
#	products/ASC.Files/Client/public/locales/ro/UploadPanel.json
#	products/ASC.Files/Client/public/locales/ru/ConvertDialog.json
#	products/ASC.Files/Client/public/locales/ru/Home.json
#	products/ASC.Files/Client/public/locales/ru/Translations.json
#	products/ASC.Files/Client/public/locales/ru/UploadPanel.json
#	products/ASC.Files/Client/public/locales/sk/Home.json
#	products/ASC.Files/Client/public/locales/tr/Home.json
#	products/ASC.Files/Client/public/locales/uk/Home.json
#	products/ASC.Files/Client/public/locales/vi/ConvertDialog.json
#	products/ASC.Files/Client/public/locales/vi/Home.json
#	products/ASC.Files/Client/public/locales/zh-CN/Home.json
#	products/ASC.Files/Client/public/locales/zh-CN/UploadPanel.json
#	products/ASC.Files/Client/src/HOCs/withBadges.js
#	products/ASC.Files/Client/src/HOCs/withContent.js
#	products/ASC.Files/Client/src/components/Badges.js
#	products/ASC.Files/Client/src/components/EditingWrapperComponent.js
#	products/ASC.Files/Client/src/components/panels/OperationsPanel/index.js
#	products/ASC.Files/Client/src/components/panels/SharingPanel/index.js
#	products/ASC.Files/Client/src/components/panels/UploadPanel/FileRow.js
#	products/ASC.Files/Client/src/pages/Home/Section/Body/RowsView/SimpleFilesRow.js
#	products/ASC.Files/Client/src/pages/Home/Section/Body/TableView/TableHeader.js
#	products/ASC.Files/Client/src/pages/Home/Section/Body/TableView/TableRow.js
#	products/ASC.Files/Client/src/pages/Home/Section/Body/TilesView/FileTile.js
#	products/ASC.Files/Client/src/pages/Home/Section/Body/TilesView/sub-components/Tile.js
#	products/ASC.Files/Client/src/pages/Home/Section/Body/index.js
#	products/ASC.Files/Client/src/store/DialogsStore.js
#	products/ASC.Files/Client/src/store/FilesActionsStore.js
#	products/ASC.Files/Core/HttpHandlers/FileHandler.ashx.cs
#	products/ASC.Files/Core/Utils/FileConverter.cs
#	products/ASC.Files/Server/Helpers/FilesControllerHelper.cs
#	products/ASC.Files/Service/Thumbnail/Builder.cs
#	products/ASC.Files/Service/Thumbnail/ThumbnailSettings.cs
#	products/ASC.Files/Service/Thumbnail/Worker.cs
#	products/ASC.People/Client/src/pages/Home/Section/Body/TableView/TableRow.js
#	public/locales/de/Common.json
2022-02-21 22:22:38 +03:00

253 lines
7.1 KiB
JavaScript

import PropTypes from "prop-types";
import React from "react";
import equal from "fast-deep-equal/react";
import ComboButton from "./sub-components/combo-button";
import DropDown from "../drop-down";
import DropDownItem from "../drop-down-item";
import StyledComboBox from "./styled-combobox";
class ComboBox extends React.Component {
constructor(props) {
super(props);
this.ref = React.createRef();
this.state = {
isOpen: props.opened,
selectedOption: props.selectedOption,
};
}
shouldComponentUpdate(nextProps, nextState) {
const needUpdate =
!equal(this.props, nextProps) || !equal(this.state, nextState);
return needUpdate;
}
stopAction = (e) => e.preventDefault();
setIsOpen = (isOpen) => this.setState({ isOpen: isOpen });
handleClickOutside = (e) => {
if (this.ref.current.contains(e.target)) return;
this.setState({ isOpen: !this.state.isOpen }, () => {
this.props.toggleAction && this.props.toggleAction(e, this.state.isOpen);
});
};
comboBoxClick = (e) => {
const { disableIconClick, isDisabled, toggleAction } = this.props;
if (
isDisabled ||
(disableIconClick && e && e.target.closest(".optionalBlock"))
)
return;
this.setState({ isOpen: !this.state.isOpen }, () => {
toggleAction && toggleAction(e, this.state.isOpen);
});
};
optionClick = (option) => {
this.setState({
isOpen: !this.state.isOpen,
selectedOption: option,
});
this.props.onSelect && this.props.onSelect(option);
};
componentDidUpdate(prevProps) {
if (this.props.opened !== prevProps.opened) {
this.setIsOpen(this.props.opened);
}
if (this.props.selectedOption !== prevProps.selectedOption) {
this.setState({ selectedOption: this.props.selectedOption });
}
}
render() {
//console.log("ComboBox render");
const {
dropDownMaxHeight,
directionX,
directionY,
scaled,
size,
options,
advancedOptions,
isDisabled,
children,
noBorder,
scaledOptions,
displayType,
toggleAction,
textOverflow,
showDisabledItems,
comboIcon,
manualY,
manualX,
isDefaultMode,
manualWidth,
} = this.props;
const { isOpen, selectedOption } = this.state;
const dropDownMaxHeightProp = dropDownMaxHeight
? { maxHeight: dropDownMaxHeight }
: {};
const dropDownManualWidthProp =
scaledOptions && !isDefaultMode
? { manualWidth: "100%" }
: scaledOptions && this.ref.current
? { manualWidth: this.ref.current.clientWidth + "px" }
: { manualWidth: manualWidth };
const optionsLength = options.length
? options.length
: displayType !== "toggle"
? 0
: 1;
const advancedOptionsLength = advancedOptions
? advancedOptions.props.children.length
: 0;
return (
<StyledComboBox
ref={this.ref}
isDisabled={isDisabled}
scaled={scaled}
size={size}
data={selectedOption}
onClick={this.comboBoxClick}
toggleAction={toggleAction}
{...this.props}
>
<ComboButton
noBorder={noBorder}
isDisabled={isDisabled}
selectedOption={selectedOption}
withOptions={optionsLength > 0}
optionsLength={optionsLength}
withAdvancedOptions={advancedOptionsLength > 0}
innerContainer={children}
innerContainerClassName="optionalBlock"
isOpen={isOpen}
size={size}
scaled={scaled}
comboIcon={comboIcon}
/>
{displayType !== "toggle" && (
<DropDown
className="dropdown-container not-selectable"
directionX={directionX}
directionY={directionY}
manualY={manualY}
manualX={manualX}
open={isOpen}
forwardedRef={this.ref}
clickOutsideAction={this.handleClickOutside}
style={advancedOptions && { padding: "6px 0px" }}
{...dropDownMaxHeightProp}
{...dropDownManualWidthProp}
showDisabledItems={showDisabledItems}
isDefaultMode={isDefaultMode}
>
{advancedOptions
? advancedOptions
: options.map((option) => (
<DropDownItem
{...option}
textOverflow={textOverflow}
key={option.key}
disabled={
option.disabled || option.label === selectedOption.label
}
onClick={this.optionClick.bind(this, option)}
/>
))}
</DropDown>
)}
</StyledComboBox>
);
}
}
ComboBox.propTypes = {
/** If you need display options not basic options */
advancedOptions: PropTypes.element,
/** Children elements */
children: PropTypes.any,
/** Accepts class */
className: PropTypes.string,
/** X direction selection */
directionX: PropTypes.oneOf(["left", "right"]),
/** Y direction selection */
directionY: PropTypes.oneOf(["bottom", "top", "both"]),
/** Component Display Type */
displayType: PropTypes.oneOf(["default", "toggle"]),
/** Height of Dropdown */
dropDownMaxHeight: PropTypes.number,
/** Display disabled items or not when displayType !== toggle */
showDisabledItems: PropTypes.bool,
/** Accepts id */
id: PropTypes.string,
/** Indicates that component is disabled */
isDisabled: PropTypes.bool,
/** Indicates that component is displayed without borders */
noBorder: PropTypes.bool,
/** Will be triggered whenever an ComboBox is selected option */
onSelect: PropTypes.func,
/** Tells when a component is open */
opened: PropTypes.bool,
/** Combo box options */
options: PropTypes.array.isRequired,
/** Indicates that component is scaled by parent */
scaled: PropTypes.bool,
/** Indicates that component`s options is scaled by ComboButton */
scaledOptions: PropTypes.bool,
/** Selected option */
selectedOption: PropTypes.object.isRequired,
/** Select component width, one of default */
size: PropTypes.oneOf(["base", "middle", "big", "huge", "content"]),
/** Accepts css style */
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
/** The event will be raised when using `displayType: toggle` when clicking on a component */
toggleAction: PropTypes.func,
/** Accepts css text-overflow */
textOverflow: PropTypes.bool,
/** Disables clicking on the icon */
disableIconClick: PropTypes.bool,
/** Defines the operation mode of the component, by default with the portal */
isDefaultMode: PropTypes.bool,
/** Y offset */
offsetDropDownY: PropTypes.string,
comboIcon: PropTypes.string,
manualY: PropTypes.string,
manualX: PropTypes.string,
//** Dropdown manual width */
manualWidth: PropTypes.string,
};
ComboBox.defaultProps = {
displayType: "default",
isDisabled: false,
noBorder: false,
scaled: true,
scaledOptions: false,
size: "base",
disableIconClick: true,
showDisabledItems: false,
manualY: "102%",
isDefaultMode: true,
manualWidth: "200px",
};
export default ComboBox;