web: Components: Removed AdvancedSelector v1

This commit is contained in:
Alexey Safronov 2019-12-10 09:59:27 +03:00
parent d6e8dc7026
commit 3ead54b4e4
14 changed files with 0 additions and 2434 deletions

View File

@ -1,44 +0,0 @@
# AdvancedSelector
## Usage
```js
import { AdvancedSelector } from 'asc-web-components';
```
#### Description
Required to select some advanced data.
#### Usage
```js
let options = [{key: "self", label: "Me"}];
options = [...options, ...[...Array(100).keys()].map(
index => {
return { key: `user${index}`, label: `User ${index+1} of ${optionsCount}` };
}
)];
<AdvancedSelector
placeholder="Search users"
onSearchChanged={(e) => console.log(e.target.value)}
options={options}
isMultiSelect={false}
buttonLabel="Add members"
onSelect={(selectedOptions) => console.log("onSelect", selectedOptions)}
/>
```
#### Properties
| Props | Type | Required | Values | Default | Description |
| ------------------ | -------- | :------: | ----------------------------------------- | --------- | ----------------------------------------------------- |
| `placeholder` | `string` | - | | | |
| `options` | `array of objects` | - | | | |
| `isMultiSelect` | `bool` | - | - | | |
| `buttonLabel` | `string` | - | - | | |
| `onSearchChanged` | `func` | - | - | | |
| `onSelect` | `func` | - | - | | |

View File

@ -1,231 +0,0 @@
/* eslint-disable react/prop-types */
import React from "react";
import { storiesOf } from "@storybook/react";
import { action } from "@storybook/addon-actions";
import {
withKnobs,
text,
number,
boolean,
select
} from "@storybook/addon-knobs/react";
import withReadme from "storybook-readme/with-readme";
import Readme from "./README.md";
import AdvancedSelector from "./";
import Section from "../../../.storybook/decorators/section";
import Button from "../button";
import { isEqual, slice } from "lodash";
import { name } from "faker";
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
const sizes = ["compact", "full"];
const displayTypes = ["dropdown", "aside"];
class ADSelectorExample extends React.Component {
constructor(props) {
super(props);
const { isOpen, total } = props;
const groups = this.generateGroups();
const users = this.generateUsers(total, groups);
this.state = this.getDefaultState(isOpen, groups, users);
}
getDefaultState = (isOpen, groups, allOptions) => {
return {
isOpen: isOpen,
allOptions,
options: [],
groups,
hasNextPage: true,
isNextPageLoading: false
};
};
generateGroups = () => {
return [
{
key: "group-all",
label: "All groups",
total: 0
},
{
key: "group-dev",
label: "Development",
total: 0
},
{
key: "group-management",
label: "Management",
total: 0
},
{
key: "group-marketing",
label: "Marketing",
total: 0
},
{
key: "group-mobile",
label: "Mobile",
total: 0
},
{
key: "group-support",
label: "Support",
total: 0
},
{
key: "group-web",
label: "Web",
total: 0
}
];
};
generateUsers = (count, groups) => {
return Array.from({ length: count }, (v, index) => {
const additional_group = groups[getRandomInt(1, 6)];
groups[0].total++;
additional_group.total++;
return {
key: `user${index}`,
groups: ["group-all", additional_group.key],
label: `${name.findName()} (User${index})`
};
});
};
loadNextPage = (startIndex, stopIndex) => {
console.log(
`loadNextPage(startIndex=${startIndex}, stopIndex=${stopIndex})`
);
this.setState({ isNextPageLoading: true }, () => {
setTimeout(() => {
const { options } = this.state;
const newOptions = [...options].concat(
slice(this.state.allOptions, startIndex, startIndex + 100)
);
this.setState({
hasNextPage: newOptions.length < this.props.total,
isNextPageLoading: false,
options: newOptions
});
}, 1000);
});
};
componentDidUpdate(prevProps) {
const { total, options, isOpen } = this.props;
if (!isEqual(prevProps.options, options)) {
this.setState({
options: options
});
}
if (isOpen !== prevProps.isOpen) {
this.setState({
isOpen: isOpen
});
}
if (total !== prevProps.total) {
const groups = this.generateGroups();
const users = this.generateUsers(total, groups);
this.setState(this.getDefaultState(isOpen, groups, users));
}
}
toggle = () => {
this.setState({
isOpen: !this.state.isOpen
});
};
render() {
const {
isOpen,
options,
groups,
selectedOptions,
selectedGroups,
hasNextPage,
isNextPageLoading
} = this.state;
return (
<div style={{position: 'relative'}}>
<Button label="Toggle dropdown" onClick={this.toggle} />
<AdvancedSelector
options={options}
selectedOptions={selectedOptions}
hasNextPage={hasNextPage}
isNextPageLoading={isNextPageLoading}
loadNextPage={this.loadNextPage}
size={select("size", sizes, "full")}
placeholder={text("placeholder", "Search users")}
onSearchChanged={value => {
action("onSearchChanged")(value);
/*set(
options.filter(option => {
return option.label.indexOf(value) > -1;
})
);*/
}}
groups={groups}
selectedGroups={selectedGroups}
isMultiSelect={boolean("isMultiSelect", true)}
buttonLabel={text("buttonLabel", "Add members")}
selectAllLabel={text("selectAllLabel", "Select all")}
onSelect={selectedOptions => {
action("onSelect")(selectedOptions);
this.toggle();
}}
//onCancel={toggle}
onGroupSelect={selectedGroups => {
action("onGroupSelect")(selectedGroups);
/*set(
options.filter(option => {
return (
option.groups &&
option.groups.length > 0 &&
option.groups.indexOf(group.key) > -1
);
})
);*/
}}
onGroupChange={group => {
action("onGroupChange")(group);
/*set(
options.filter(option => {
return (
option.groups &&
option.groups.length > 0 &&
option.groups.indexOf(group.key) > -1
);
})
);*/
}}
allowCreation={boolean("allowCreation", false)}
onAddNewClick={() => action("onSelect")}
isOpen={isOpen}
displayType={select("displayType", displayTypes, "dropdown")}
/>
</div>
);
}
}
storiesOf("Components|AdvancedSelector", module)
.addDecorator(withKnobs)
.addDecorator(withReadme(Readme))
.add("base", () => {
return (
<Section>
<ADSelectorExample isOpen={boolean("isOpen", true)} total={number("Users count", 10000)} />
</Section>
);
});

View File

@ -1,64 +0,0 @@
import React from 'react';
import { mount, shallow } from 'enzyme';
import AdvancedSelector from '.';
const baseProps = {
placeholder: "Search users",
options: [],
isMultiSelect: false,
buttonLabel: "Add members",
onSearchChanged: jest.fn,
onSelect: jest.fn,
onCancel: jest.fn
};
describe('<AdvancedSelector />', () => {
it('renders without error', () => {
const wrapper = mount(
<AdvancedSelector {...baseProps} />
);
expect(wrapper).toExist();
});
it('not re-render test', () => {
const wrapper = shallow(<AdvancedSelector {...baseProps} />).instance();
const shouldUpdate = wrapper.shouldComponentUpdate(wrapper.props, wrapper.state);
expect(shouldUpdate).toBe(false);
});
it('re-render test by options', () => {
const wrapper = shallow(<AdvancedSelector {...baseProps} />).instance();
const shouldUpdate = wrapper.shouldComponentUpdate({
...baseProps,
options: [{key: 1, label: "Example"}]
}, wrapper.state);
expect(shouldUpdate).toBe(true);
});
it('re-render test by groups', () => {
const wrapper = shallow(<AdvancedSelector {...baseProps} />).instance();
const shouldUpdate = wrapper.shouldComponentUpdate({
...baseProps,
groups: [{key: 1, label: "Example"}]
}, wrapper.state);
expect(shouldUpdate).toBe(true);
});
it('re-render test by selectedOptions', () => {
const wrapper = shallow(<AdvancedSelector {...baseProps} />).instance();
const shouldUpdate = wrapper.shouldComponentUpdate({
...baseProps,
selectedOptions: [{key: 1, label: "Example"}]
}, wrapper.state);
expect(shouldUpdate).toBe(true);
});
});

View File

@ -1,220 +0,0 @@
/* eslint-disable react/prop-types */
import React, { useRef, useEffect, useState } from "react";
import { storiesOf } from "@storybook/react";
import {
withKnobs
} from "@storybook/addon-knobs/react";
import withReadme from "storybook-readme/with-readme";
import Readme from "./README.md";
import { FixedSizeList as List } from "react-window";
import InfiniteLoader from "react-window-infinite-loader";
import faker, { name } from "faker";
import uniqueId from "lodash/uniqueId";
import Section from "../../../.storybook/decorators/section";
//import ADSelectorRow from "./sub-components/row";
import Checkbox from "../checkbox";
import CustomScrollbarsVirtualList from "../scrollbar/custom-scrollbars-virtual-list";
import DropDown from "../drop-down";
const ExampleWrapper = ({
// Are there more items to load?
// (This information comes from the most recent API request.)
hasNextPage,
// Are we currently loading a page of items?
// (This may be an in-flight flag in your Redux store for example.)
isNextPageLoading,
// Array of items loaded so far.
items,
// Callback function responsible for loading the next page of items.
loadNextPage,
sortOrder
}) => {
// We create a reference for the InfiniteLoader
const listRef = useRef(null);
const hasMountedRef = useRef(false);
const [selected, setSelected] = useState([]);
// Each time the sort prop changed we called the method resetloadMoreItemsCache to clear the cache
useEffect(() => {
if (listRef.current && hasMountedRef.current) {
listRef.current.resetloadMoreItemsCache();
}
hasMountedRef.current = true;
}, [sortOrder]);
// If there are more items to be loaded then add an extra row to hold a loading indicator.
const itemCount = hasNextPage ? items.length + 1 : items.length;
// Only load 1 page of items at a time.
// Pass an empty callback to InfiniteLoader in case it asks us to load more than once.
const loadMoreItems = isNextPageLoading ? () => {} : loadNextPage;
// Every row is loaded except for our loading indicator row.
const isItemLoaded = index => !hasNextPage || index < items.length;
const onChange = (e, item) => {
const newSelected = e.target.checked
? [item, ...selected]
: selected.filter(el => el.name !== item.name);
//console.log("OnChange", newSelected);
setSelected(newSelected);
};
const onSelect = (e, item) => {
console.log("onSelect", item);
};
// Render an item or a loading indicator.
const Item = ({ index, style }) => {
let content;
if (!isItemLoaded(index)) {
content = "Loading...";
} else {
const item = items[index];
const checked = selected.findIndex(el => el.id === item.id) > -1;
const newStyle = Object.assign({}, style, {
padding: "0 0.5rem",
lineHeight: "30px"
});
//console.log("Item render", item, checked, selected);
content = (
<Checkbox
key={item.id}
label={item.name}
isChecked={checked}
className="option_checkbox"
onChange={e => onChange(e, item)}
/>
/*<>
<input
id={item.id}
type="checkbox"
onChange={e => onChange(e, item)}
checked={checked}
/>
<label htmlFor={item.id}>{item.name}</label>
</>*/
/*<ADSelectorRow
key={item.id}
label={item.name}
isChecked={checked}
isMultiSelect={true}
isSelected={false}
className="ListItem"
style={{ padding: "0 0.5rem", lineHeight: "30px" }}
onChange={e => onChange(e, item)}
onSelect={e => onSelect(e, item)}
/>*/
);
}
return <div style={style}>{content}</div>;
};
// We passed down the ref to the InfiniteLoader component
return (
<InfiniteLoader
ref={listRef}
isItemLoaded={isItemLoaded}
itemCount={itemCount}
loadMoreItems={loadMoreItems}
>
{({ onItemsRendered, ref }) => (
<List
className="List"
style={{border: "1px solid #ddd", borderRadius: "0.25rem"}}
height={300}
itemCount={itemCount}
itemSize={30}
onItemsRendered={onItemsRendered}
ref={ref}
width={500}
outerElementType={CustomScrollbarsVirtualList}
>
{Item}
</List>
)}
</InfiniteLoader>
);
}
class ADSelectorExample extends React.PureComponent {
state = {
hasNextPage: true,
isNextPageLoading: false,
sortOrder: "asc",
items: []
};
constructor(props) {
super(props);
faker.seed(123);
this.persons = new Array(1000)
.fill(true)
.map(() => ({ name: name.findName(), id: uniqueId() }));
this.persons.sort((a, b) => a.name.localeCompare(b.name));
}
_loadNextPage = (...args) => {
this.setState({ isNextPageLoading: true }, () => {
setTimeout(() => {
this.setState(state => ({
hasNextPage: state.items.length < 100,
isNextPageLoading: false,
items: [...state.items].concat(
this.persons.slice(args[0], args[0] + 10)
)
}));
}, 2500);
});
};
_handleSortOrderChange = e => {
this.persons.sort((a, b) => {
if (e.target.value === "asc") {
return a.name.localeCompare(b.name);
}
return b.name.localeCompare(a.name);
});
this.setState({
sortOrder: e.target.value,
items: []
});
};
render() {
const { hasNextPage, isNextPageLoading, items, sortOrder } = this.state;
return (
<>
<div style={{position: "relative"}}>
<select onChange={this._handleSortOrderChange}>
<option value="asc">ASC</option>
<option value="desc">DESC</option>
</select>
</div>
<ExampleWrapper
hasNextPage={hasNextPage}
isNextPageLoading={isNextPageLoading}
items={items}
sortOrder={sortOrder}
loadNextPage={this._loadNextPage}
/>
</>
);
}
}
storiesOf("Components|AdvancedSelector", module)
.addDecorator(withKnobs)
.addDecorator(withReadme(Readme))
.add("only list", () => {
return (
<Section>
<ADSelectorExample />
</Section>
);
});

View File

@ -1,433 +0,0 @@
import React from "react";
import PropTypes from "prop-types";
import SearchInput from "../search-input";
import CustomScrollbarsVirtualList from "../scrollbar/custom-scrollbars-virtual-list";
import { FixedSizeList } from "react-window";
import Link from "../link";
import Checkbox from "../checkbox";
import Button from "../button";
import { Icons } from "../icons";
import ComboBox from "../combobox";
import Text from "../text";
import findIndex from "lodash/findIndex";
import filter from "lodash/filter";
import isEqual from "lodash/isEqual";
import DropDown from "../drop-down";
import Aside from "../layout/sub-components/aside";
import ADSelector from "./sub-components/selector";
const displayTypes = ["dropdown", "aside"];
const sizes = ["compact", "full"];
class AdvancedSelector extends React.Component {
constructor(props) {
super(props);
this.ref = React.createRef();
const groups = this.convertGroups(this.props.groups);
const currentGroup = this.getCurrentGroup(groups);
this.state = {
selectedOptions: this.props.selectedOptions || [],
selectedAll: this.props.selectedAll || false,
groups: groups,
currentGroup: currentGroup
};
if (props.isOpen) handleAnyClick(true, this.handleClick);
}
handleClick = e => {
if (
this.props.isOpen &&
this.props.allowAnyClickClose &&
this.ref &&
this.ref.current &&
!this.ref.current.contains(e.target) &&
e &&
e.target &&
e.target.className &&
typeof e.target.className.indexOf === "function" &&
e.target.className.indexOf("option_checkbox") === -1
) {
this.props.onCancel && this.props.onCancel();
}
};
componentWillUnmount() {
handleAnyClick(false, this.handleClick);
}
shouldComponentUpdate(nextProps, nextState) {
return !isEqual(this.props, nextProps) || !isEqual(this.state, nextState);
}
componentDidUpdate(prevProps) {
if (this.props.isOpen !== prevProps.isOpen) {
handleAnyClick(this.props.isOpen, this.handleClick);
}
if (this.props.allowAnyClickClose !== prevProps.allowAnyClickClose) {
handleAnyClick(this.props.allowAnyClickClose, this.handleClick);
}
let newState = {};
if (!isEqual(this.props.selectedOptions, prevProps.selectedOptions)) {
newState = { selectedOptions: this.props.selectedOptions };
}
if (this.props.isMultiSelect !== prevProps.isMultiSelect) {
newState = Object.assign({}, newState, {
selectedOptions: []
});
}
if (this.props.selectedAll !== prevProps.selectedAll) {
newState = Object.assign({}, newState, {
selectedAll: this.props.selectedAll
});
}
if (!isEqual(this.props.groups, prevProps.groups)) {
const groups = this.convertGroups(this.props.groups);
const currentGroup = this.getCurrentGroup(groups);
newState = Object.assign({}, newState, {
groups,
currentGroup
});
}
if (!isEmpty(newState)) {
this.setState({ ...this.state, ...newState });
}
}
convertGroups = groups => {
if (!groups) return [];
const wrappedGroups = groups.map(this.convertGroup);
return wrappedGroups;
};
convertGroup = group => {
return {
key: group.key,
label: `${group.label} (${group.total})`,
total: group.total
};
};
getCurrentGroup = groups => {
const currentGroup = groups.length > 0 ? groups[0] : "No groups";
return currentGroup;
};
onButtonClick = () => {
this.props.onSelect &&
this.props.onSelect(
this.state.selectedAll ? this.props.options : this.state.selectedOptions
);
};
onSelect = option => {
this.props.onSelect && this.props.onSelect(option);
};
onChange = (option, e) => {
const { selectedOptions } = this.state;
const newSelectedOptions = e.target.checked
? [...selectedOptions, option]
: filter(selectedOptions, obj => obj.key !== option.key);
//console.log("onChange", option, e.target.checked, newSelectedOptions);
this.setState({
selectedOptions: newSelectedOptions,
selectedAll: newSelectedOptions.length === this.props.options.length
});
};
onSelectedAllChange = e => {
this.setState({
selectedAll: e.target.checked,
selectedOptions: e.target.checked ? this.props.options : []
});
};
onCurrentGroupChange = group => {
this.setState({
currentGroup: group
});
this.props.onChangeGroup && this.props.onChangeGroup(group);
};
renderRow = ({ data, index, style }) => {
const option = data[index];
var isChecked =
this.state.selectedAll ||
findIndex(this.state.selectedOptions, { key: option.key }) > -1;
//console.log("renderRow", option, isChecked, this.state.selectedOptions);
return (
<div className="option" style={style} key={option.key}>
{this.props.isMultiSelect ? (
<Checkbox
label={option.label}
isChecked={isChecked}
className="option_checkbox"
onChange={this.onChange.bind(this, option)}
/>
) : (
<Link
as="span"
truncate={true}
className="option_link"
onClick={this.onSelect.bind(this, option)}
>
{option.label}
</Link>
)}
</div>
);
};
renderBody = () => {
const {
value,
placeholder,
isDisabled,
onSearchChanged,
options,
isMultiSelect,
buttonLabel,
selectAllLabel,
size,
displayType,
onAddNewClick,
allowCreation
} = this.props;
const { selectedOptions, selectedAll, currentGroup, groups } = this.state;
/*const containerHeight =
size === "compact" ? (!groups || !groups.length ? 336 : 326) : 614;
const containerWidth =
size === "compact" ? (!groups || !groups.length ? 325 : 326) : isDropDown ? 690 : 326;
const listHeight =
size === "compact"
? !groups || !groups.length
? isMultiSelect
? 176
: 226
: 120
: 488;
const listWidth = isDropDown ? 320 : "100%";*/
let containerHeight;
let containerWidth;
let listHeight;
let listWidth;
const itemHeight = 32;
const hasGroups = groups && groups.length > 0;
switch (size) {
case "compact":
containerHeight = hasGroups ? "326px" : "100%";
containerWidth = "379px";
listWidth = displayType === "dropdown" ? 356 : 356;
listHeight = hasGroups ? 488 : isMultiSelect ? 176 : 226;
break;
case "full":
default:
containerHeight = "100%";
containerWidth = displayType === "dropdown" ? "690px" : "326px";
listWidth = displayType === "dropdown" ? 320 : 302;
listHeight = 488;
break;
}
return (
<StyledBodyContainer
containerHeight={containerHeight}
containerWidth={containerWidth}
{...this.props}
>
<div ref={this.ref}>
<div className="data_container">
<div className="data_column_one">
<div className="head_container">
<SearchInput
className="options_searcher"
isDisabled={isDisabled}
size="base"
scale={true}
isNeedFilter={false}
placeholder={placeholder}
value={value}
onChange={onSearchChanged}
onClearSearch={onSearchChanged.bind(this, "")}
/>
{allowCreation && (
<Button
className="add_new_btn"
primary={false}
size="base"
label=""
icon={
<Icons.PlusIcon
size="medium"
isfill={true}
color="#D8D8D8"
/>
}
onClick={onAddNewClick}
/>
)}
</div>
{displayType === "aside" && groups && groups.length > 0 && (
<ComboBox
className="options_group_selector"
isDisabled={isDisabled}
options={groups}
selectedOption={currentGroup}
dropDownMaxHeight={200}
scaled={true}
scaledOptions={true}
size="content"
onSelect={this.onCurrentGroupChange}
/>
)}
{isMultiSelect && !groups && !groups.length && (
<Checkbox
label={selectAllLabel}
isChecked={
selectedAll || selectedOptions.length === options.length
}
isIndeterminate={!selectedAll && selectedOptions.length > 0}
className="option_select_all_checkbox"
onChange={this.onSelectedAllChange}
/>
)}
<FixedSizeList
className="options_list"
height={listHeight}
width={listWidth}
itemSize={itemHeight}
itemCount={this.props.options.length}
itemData={this.props.options}
outerElementType={CustomScrollbarsVirtualList}
>
{this.renderRow.bind(this)}
</FixedSizeList>
</div>
{displayType === "dropdown" &&
size === "full" &&
groups &&
groups.length > 0 && (
<div className="data_column_two">
<Text
as="p"
className="group_header"
fontSize={15}
isBold={true}
>
Groups
</Text>
<FixedSizeList
className="group_list"
height={listHeight}
itemSize={itemHeight}
itemCount={this.props.groups.length}
itemData={this.props.groups}
outerElementType={CustomScrollbarsVirtualList}
>
{this.renderRow.bind(this)}
</FixedSizeList>
</div>
)}
</div>
{isMultiSelect && (
<div className="button_container">
<Button
className="add_members_btn"
primary={true}
size="big"
label={buttonLabel}
scale={true}
isDisabled={
!this.state.selectedOptions ||
!this.state.selectedOptions.length
}
onClick={this.onButtonClick}
/>
</div>
)}
</div>
</StyledBodyContainer>
);
};
render() {
const { displayType, isOpen } = this.props;
//console.log("AdvancedSelector render()");
return (
displayType === "dropdown"
? <DropDown opened={isOpen} className="dropdown-container">
<ADSelector {...this.props} />
</DropDown>
: <Aside visible={isOpen} scale={false} className="aside-container">
<ADSelector {...this.props} />
</Aside>
);
}
}
AdvancedSelector.propTypes = {
options: PropTypes.array,
selectedOptions: PropTypes.array,
groups: PropTypes.array,
selectedGroups: PropTypes.array,
value: PropTypes.string,
placeholder: PropTypes.string,
selectAllLabel: PropTypes.string,
buttonLabel: PropTypes.string,
size: PropTypes.oneOf(sizes),
displayType: PropTypes.oneOf(displayTypes),
maxHeight: PropTypes.number,
isMultiSelect: PropTypes.bool,
isDisabled: PropTypes.bool,
selectedAll: PropTypes.bool,
isOpen: PropTypes.bool,
allowCreation: PropTypes.bool,
allowAnyClickClose: PropTypes.bool,
hasNextPage: PropTypes.bool,
isNextPageLoading: PropTypes.bool,
onSearchChanged: PropTypes.func,
onSelect: PropTypes.func,
onGroupChange: PropTypes.func,
onCancel: PropTypes.func,
onAddNewClick: PropTypes.func,
loadNextPage: PropTypes.func,
};
AdvancedSelector.defaultProps = {
isMultiSelect: false,
size: "compact",
buttonLabel: "Add members",
selectAllLabel: "Select all",
allowAnyClickClose: true,
displayType: "dropdown",
options: []
};
export default AdvancedSelector;

View File

@ -1,53 +0,0 @@
import React from "react";
import PropTypes from "prop-types";
import Button from "../../button";
import styled from "styled-components";
/* eslint-disable no-unused-vars */
/* eslint-disable react/prop-types */
const Container = ({
buttonLabel,
isDisabled,
onClick,
isMultiSelect,
...props
}) => <div {...props} />;
/* eslint-enable react/prop-types */
/* eslint-enable no-unused-vars */
const StyledContainer = styled(Container)`
border-top: 1px solid #eceef1;
display: ${props => !props.isMultiSelect || !props.selectedOptions || !props.selectedOptions.length ? 'none' : 'flex'};
.add_members_btn {
margin: 16px;
}
`;
const ADSelectorFooter = props => {
const { buttonLabel, isDisabled, onClick } = props;
return (
<StyledContainer {...props}>
<Button
className="add_members_btn"
primary={true}
size="big"
label={buttonLabel}
scale={true}
isDisabled={isDisabled}
onClick={onClick}
/>
</StyledContainer>
);
};
ADSelectorFooter.propTypes = {
buttonLabel: PropTypes.string,
isDisabled: PropTypes.bool,
isMultiSelect: PropTypes.bool,
selectedOptions: PropTypes.array,
onClick: PropTypes.func
};
export default ADSelectorFooter;

View File

@ -1,70 +0,0 @@
import React from "react";
import PropTypes from "prop-types";
import CustomScrollbarsVirtualList from "../../../scrollbar/custom-scrollbars-virtual-list";
import { FixedSizeList } from "react-window";
import ADSelectorRow from "../row";
import findIndex from "lodash/findIndex";
import isEqual from "lodash/isEqual";
class ADSelectorGroupsBody extends React.Component {
shouldComponentUpdate(nextProps) {
const needUpdate = !isEqual(this.props, nextProps);
return needUpdate;
}
renderRow = ({ data, index, style }) => {
const {isMultiSelect, selectedAll, selectedOptions, currentGroup} = this.props;
const option = data[index];
const isChecked = isMultiSelect
? selectedAll ||
findIndex(selectedOptions, { key: option.key }) > -1
: undefined;
const isSelected = currentGroup.key === option.key;
//console.log("renderRow", option, isChecked, this.state.selectedOptions);
return (
<ADSelectorRow
key={option.key}
label={option.label}
isChecked={isChecked}
isMultiSelect={isMultiSelect}
style={style}
isSelected={isSelected}
onChange={this.props.onRowChecked.bind(this, option)}
/>
);
};
render() {
const { options, listHeight, itemHeight } = this.props;
return (
<FixedSizeList
className="group_list"
height={listHeight}
itemSize={itemHeight}
itemCount={options.length}
itemData={options}
outerElementType={CustomScrollbarsVirtualList}
>
{this.renderRow.bind(this)}
</FixedSizeList>
);
}
}
ADSelectorGroupsBody.propTypes = {
options: PropTypes.array,
selectedOptions: PropTypes.array,
selectedAll: PropTypes.bool,
currentGroup: PropTypes.object,
isMultiSelect: PropTypes.bool,
listHeight: PropTypes.number,
itemHeight: PropTypes.number,
onRowChecked: PropTypes.func,
onRowSelect: PropTypes.func
};
export default ADSelectorGroupsBody;

View File

@ -1,18 +0,0 @@
import React from "react";
import PropTypes from "prop-types";
import Text from "../../../text";
const ADSelectorGroupsHeader = (props) => {
const {headerLabel} = props;
return (
<Text as="p" className="group_header" fontSize={15} isBold={true}>
{headerLabel}
</Text>
);
}
ADSelectorGroupsHeader.propTypes = {
headerLabel: PropTypes.string
}
export default ADSelectorGroupsHeader;

View File

@ -1,161 +0,0 @@
import React from "react";
import PropTypes from "prop-types";
import CustomScrollbarsVirtualList from "../../../scrollbar/custom-scrollbars-virtual-list";
import { FixedSizeList as List } from "react-window";
import InfiniteLoader from "react-window-infinite-loader";
//import ADSelectorRow from "../row";
import Checkbox from "../../../checkbox";
import Loader from "../../../loader";
import Text from "../../../text";
import findIndex from "lodash/findIndex";
//import isEqual from "lodash/isEqual";
class ADSelectorOptionsBody extends React.Component {
constructor(props) {
super(props);
this.listRef = React.createRef();
//this.hasMountedRef = React.createRef(false);
}
/*componentDidMount() {
this.hasMountedRef.current = true;
}
componentDidUpdate(prevProps) {
if(!isEqual(this.props.selectedOptions, prevProps.selectedOptions)) {
if(this.listRef.current && this.hasMountedRef.current) {
//this.listRef.current.resetloadMoreItemsCache(true);
//console.log("this.listRef.current", this.listRef.current)
}
}
}*/
renderRow = ({ index, style }) => {
//console.log("renderRow", option, isChecked, this.state.selectedOptions);
let content;
const isLoaded = this.isItemLoaded(index);
let key = "loader";
if (!isLoaded) {
content = (
<div className="option" style={style} key="loader">
<Loader
type="oval"
size={16}
style={{
display: "inline",
marginRight: "10px"
}}
/>
<Text as="span">Loading... Please wait...</Text>
</div>
);
} else {
const {options, isMultiSelect, selectedAll, selectedOptions} = this.props;
const option = options[index];
var isChecked = isMultiSelect
? selectedAll ||
findIndex(selectedOptions, { key: option.key }) > -1
: undefined;
key = option.key;
content = (
<Checkbox
key={option.key}
label={option.label}
isChecked={isChecked}
className="option_checkbox"
onChange={this.props.onRowChecked.bind(this, option)}
/>
/*<ADSelectorRow
key={option.key}
label={option.label}
isChecked={isChecked}
isMultiSelect={isMultiSelect}
style={style}
onChange={this.props.onRowChecked.bind(this, option)}
onSelect={this.props.onRowSelect.bind(this, option)}
/>*/
/*<div className="option" style={style}>
<input
id={option.key}
type="checkbox"
onChange={(e) => {
console.log("checkbox click", e);
this.props.onRowChecked(option, e);
}}
checked={isChecked}
/>
<label htmlFor={option.key}>{option.label}</label>
</div>*/
);
}
return <div className="option" style={style} key={key}>{content}</div>;
};
isItemLoaded = index =>
!this.props.hasNextPage || index < this.props.options.length;
loadMoreItems = this.props.isNextPageLoading
? () => {}
: this.props.loadNextPage;
render() {
const {
options,
hasNextPage,
listHeight,
listWidth,
itemHeight
} = this.props;
const itemCount = hasNextPage ? options.length + 1 : options.length;
return (
<div>
<InfiniteLoader
ref={this.listRef}
isItemLoaded={this.isItemLoaded}
itemCount={itemCount}
loadMoreItems={this.loadMoreItems}
>
{({ onItemsRendered, ref }) => (
<List
className="options_list"
height={listHeight}
width={listWidth}
itemCount={itemCount}
itemSize={itemHeight}
onItemsRendered={onItemsRendered}
ref={ref}
outerElementType={CustomScrollbarsVirtualList}
>
{this.renderRow}
</List>
)}
</InfiniteLoader>
</div>
);
}
}
ADSelectorOptionsBody.propTypes = {
options: PropTypes.array,
hasNextPage: PropTypes.bool,
isNextPageLoading: PropTypes.bool,
loadNextPage: PropTypes.func,
selectedOptions: PropTypes.array,
selectedAll: PropTypes.bool,
isMultiSelect: PropTypes.bool,
listHeight: PropTypes.number,
listWidth: PropTypes.number,
itemHeight: PropTypes.number,
onRowChecked: PropTypes.func,
onRowSelect: PropTypes.func
};
export default ADSelectorOptionsBody;

View File

@ -1,53 +0,0 @@
import React from "react";
import PropTypes from "prop-types";
import SearchInput from "../../../search-input";
import Button from "../../../button";
import { Icons } from "../../../icons";
const ADSelectorOptionsHeader = (props) => {
const { searchPlaceHolder, value, isDisabled, allowCreation, onSearchChanged, onAddNewClick } = props;
return (
<div className="head_container">
<SearchInput
className="options_searcher"
isDisabled={isDisabled}
size="base"
scale={true}
isNeedFilter={false}
placeholder={searchPlaceHolder}
value={value}
onChange={onSearchChanged}
onClearSearch={onSearchChanged}
/>
{allowCreation && (
<Button
className="add_new_btn"
primary={false}
size="base"
label=""
icon={
<Icons.PlusIcon
size="medium"
isfill={true}
color="#D8D8D8"
/>
}
onClick={onAddNewClick}
/>
)}
</div>
);
};
ADSelectorOptionsHeader.propTypes = {
searchPlaceHolder: PropTypes.string,
value: PropTypes.string,
isDisabled: PropTypes.bool,
allowCreation: PropTypes.bool,
style: PropTypes.object,
onSearchChanged: PropTypes.func,
onAddNewClick: PropTypes.func
}
export default ADSelectorOptionsHeader;

View File

@ -1,54 +0,0 @@
import React from "react";
import PropTypes from "prop-types";
import Checkbox from "../../checkbox";
import Link from "../../link";
class ADSelectorRow extends React.Component {
render() {
const {
label,
isChecked,
style,
onChange,
onSelect,
isMultiSelect,
isSelected
} = this.props;
//console.log("ADSelectorRow render", label, isChecked);
return (
<div className={`option${isSelected ? "selected" : ""}`} style={style}>
{isMultiSelect ? (
<Checkbox
label={label}
isChecked={isChecked}
className="option_checkbox"
onChange={onChange}
/>
) : (
<Link
as="span"
truncate={true}
className="option_link"
onClick={onSelect}
>
{label}
</Link>
)}
</div>
);
}
}
ADSelectorRow.propTypes = {
label: PropTypes.string,
isSelected: PropTypes.bool,
isChecked: PropTypes.bool,
isMultiSelect: PropTypes.bool,
style: PropTypes.object,
onChange: PropTypes.func,
onSelect: PropTypes.func
};
export default ADSelectorRow;

View File

@ -1,495 +0,0 @@
import React, { useRef, useState } from "react";
import PropTypes from "prop-types";
import styled, { css } from "styled-components";
import { FixedSizeList as List } from "react-window";
import InfiniteLoader from "react-window-infinite-loader";
import Checkbox from "../../checkbox";
import ComboBox from "../../combobox";
import Loader from "../../loader";
import Text from "../../text";
import CustomScrollbarsVirtualList from "../../scrollbar/custom-scrollbars-virtual-list";
import ADSelectorOptionsHeader from "./options/header";
import ADSelectorGroupsHeader from "./groups/header";
import ADSelectorGroupsBody from "./groups/body";
import ADSelectorFooter from "./footer";
/* eslint-disable no-unused-vars */
/* eslint-disable react/prop-types */
const Container = ({
value,
placeholder,
isMultiSelect,
size,
width,
maxHeight,
isDisabled,
onSelect,
onSearchChanged,
options,
selectedOptions,
buttonLabel,
selectAllLabel,
groups,
selectedGroups,
onGroupSelect,
onGroupChange,
isOpen,
displayType,
containerWidth,
containerHeight,
allowCreation,
onAddNewClick,
allowAnyClickClose,
hasNextPage,
isNextPageLoading,
loadNextPage,
isSelected,
...props
}) => <div {...props} />;
/* eslint-enable react/prop-types */
/* eslint-enable no-unused-vars */
const StyledContainer = styled(Container)`
display: flex;
flex-direction: column;
${props => (props.containerWidth ? `width: ${props.containerWidth};` : "")}
${props =>
props.containerHeight ? `height: ${props.containerHeight};` : ""}
.data_container {
margin: 16px 16px -5px 16px;
.head_container {
display: flex;
margin-bottom: ${props =>
props.displayType === "dropdown" ? 8 : 16}px;
.options_searcher {
display: inline-block;
width: 100%;
${props =>
props.displayType === "dropdown" &&
props.size === "full" &&
css`
margin-right: ${props => (props.allowCreation ? 8 : 16)}px;
`}
/*${props =>
props.allowCreation
? css`
width: 272px;
margin-right: 8px;
`
: css`
width: ${props => (props.isDropDown ? "313px" : "100%")};
`}*/
}
.add_new_btn {
${props =>
props.allowCreation &&
css`
display: inline-block;
vertical-align: top;
height: 32px;
width: 36px;
margin-right: 16px;
line-height: 18px;
`}
}
}
.options_group_selector {
margin-bottom: 12px;
}
.data_column_one {
${props =>
props.size === "full" &&
props.displayType === "dropdown" &&
props.groups &&
props.groups.length > 0
? css`
width: 50%;
display: inline-block;
`
: ""}
.options_list {
margin-top: 4px;
margin-left: -8px;
.option {
line-height: 32px;
padding-left: ${props => (props.isMultiSelect ? 8 : 0)}px;
cursor: pointer;
.option_checkbox {
/*margin-left: 8px;*/
}
.option_link {
padding-left: 8px;
}
&:hover {
background-color: #f8f9f9;
}
}
}
}
.data_column_two {
${props =>
props.displayType === "dropdown" &&
props.groups &&
props.groups.length > 0
? css`
width: 50%;
display: inline-block;
border-left: 1px solid #eceef1;
`
: ""}
.group_header {
font-weight: 600;
padding-left: 16px;
padding-bottom: 14px;
}
.group_list {
margin-left: 8px;
.option {
line-height: 32px;
padding-left: 8px;
cursor: pointer;
.option_checkbox {
/*margin-left: 8px;*/
}
.option_link {
padding-left: 8px;
}
&:hover {
background-color: #f8f9f9;
}
}
.option.selected {
background-color: #ECEEF1;
}
}
}
}
`;
const ADSelector = props => {
const {
options,
groups,
hasNextPage,
isNextPageLoading,
loadNextPage,
value,
placeholder,
isDisabled,
onSearchChanged,
isMultiSelect,
buttonLabel,
selectAllLabel,
size,
displayType,
onAddNewClick,
allowCreation,
selectedOptions,
selectedGroups,
onSelect
} = props;
// We create a reference for the InfiniteLoader
const listRef = useRef(null);
//const hasMountedRef = useRef(false);
const [selected, setSelected] = useState(selectedOptions || []);
const [selectedGrps, setSelectedGroups] = useState(selectedGroups || []);
const [selectedAll, setSelectedAll] = useState(false);
const convertGroups = items => {
if (!items) return [];
const wrappedGroups = items.map(convertGroup);
return wrappedGroups;
};
const convertGroup = group => {
return {
key: group.key,
label: `${group.label} (${group.total})`,
total: group.total
};
};
const getCurrentGroup = items => {
const currentGroup = items.length > 0 ? items[0] : "No groups";
return currentGroup;
};
const convertedGroups = convertGroups(groups);
const curGroup = getCurrentGroup(convertedGroups);
const [currentGroup, setCurrentGroup] = useState(curGroup);
// Each time the sort prop changed we called the method resetloadMoreItemsCache to clear the cache
/*useEffect(() => {
if (listRef.current && hasMountedRef.current) {
listRef.current.resetloadMoreItemsCache();
}
hasMountedRef.current = true;
}, [sortOrder]);*/
// If there are more items to be loaded then add an extra row to hold a loading indicator.
const itemCount = hasNextPage ? options.length + 1 : options.length;
// Only load 1 page of items at a time.
// Pass an empty callback to InfiniteLoader in case it asks us to load more than once.
const loadMoreItems = isNextPageLoading ? () => {} : loadNextPage;
// Every row is loaded except for our loading indicator row.
const isItemLoaded = index => !hasNextPage || index < options.length;
const onChange = (e, item) => {
const newSelected = e.target.checked
? [item, ...selected]
: selected.filter(el => el.name !== item.name);
//console.log("OnChange", newSelected);
setSelected(newSelected);
};
const onGroupChange = (e, item) => {
const newSelectedGroups = e.target.checked
? [item, ...selectedGrps]
: selectedGrps.filter(el => el.name !== item.name);
//console.log("onGroupChange", item);
setSelectedGroups(newSelectedGroups);
};
const onCurrentGroupChange = (e, item) => {
//console.log("onCurrentGroupChange", item);
setCurrentGroup(item);
};
const onSelectedAllChange = (e, item) => {
//console.log("onSelectedAllChange", item);
setSelectedAll(item);
};
const onSearchReset = () => {
onSearchChanged && onSearchChanged("");
}
const onButtonClick = () => {
onSelect && onSelect(selectedAll ? options : selectedOptions);
};
// Render an item or a loading indicator.
const Item = ({ index, style }) => {
let content;
if (!isItemLoaded(index)) {
content = <div className="option" style={style} key="loader">
<Loader
type="oval"
size={16}
style={{
display: "inline",
marginRight: "10px"
}}
/>
<Text as="span">Loading... Please wait...</Text>
</div>;
} else {
const option = options[index];
const checked = selected.findIndex(el => el.key === option.key) > -1;
//console.log("Item render", item, checked, selected);
content = (
<Checkbox
key={option.key}
label={option.label}
isChecked={checked}
className="option_checkbox"
onChange={e => onChange(e, option)}
/>
/*<>
<input
id={item.id}
type="checkbox"
onChange={e => onChange(e, item)}
checked={checked}
/>
<label htmlFor={item.id}>{item.name}</label>
</>*/
/*<ADSelectorRow
key={item.id}
label={item.name}
isChecked={checked}
isMultiSelect={true}
isSelected={false}
className="ListItem"
style={{ padding: "0 0.5rem", lineHeight: "30px" }}
onChange={e => onChange(e, item)}
onSelect={e => onSelect(e, item)}
/>*/
);
}
return <div style={style}>{content}</div>;
};
let containerHeight;
let containerWidth;
let listHeight;
let listWidth;
const itemHeight = 32;
const hasGroups = convertedGroups && convertedGroups.length > 0;
switch (size) {
case "compact":
containerHeight = hasGroups ? "326px" : "100%";
containerWidth = "379px";
listWidth = displayType === "dropdown" ? 356 : 356;
listHeight = hasGroups ? 488 : isMultiSelect ? 176 : 226;
break;
case "full":
default:
containerHeight = "100%";
containerWidth = displayType === "dropdown" ? "690px" : "326px";
listWidth = displayType === "dropdown" ? 320 : 300;
listHeight = 488;
break;
}
// We passed down the ref to the InfiniteLoader component
return (
<StyledContainer
containerHeight={containerHeight}
containerWidth={containerWidth}
{...props}
>
<div className="data_container">
<div className="data_column_one">
<ADSelectorOptionsHeader
value={value}
searchPlaceHolder={placeholder}
isDisabled={isDisabled}
allowCreation={allowCreation}
onAddNewClick={onAddNewClick}
onChange={onSearchChanged}
onClearSearch={onSearchReset}
/>
{displayType === "aside" && convertedGroups && convertedGroups.length > 0 && (
<ComboBox
className="options_group_selector"
isDisabled={isDisabled}
options={convertedGroups}
selectedOption={currentGroup}
dropDownMaxHeight={200}
scaled={true}
scaledOptions={true}
size="content"
onSelect={onCurrentGroupChange}
/>
)}
{isMultiSelect && !convertedGroups && !convertedGroups.length && (
<Checkbox
label={selectAllLabel}
isChecked={
selectedAll || selectedOptions.length === options.length
}
isIndeterminate={!selectedAll && selectedOptions.length > 0}
className="option_select_all_checkbox"
onChange={onSelectedAllChange}
/>
)}
<InfiniteLoader
ref={listRef}
isItemLoaded={isItemLoaded}
itemCount={itemCount}
loadMoreItems={loadMoreItems}
>
{({ onItemsRendered, ref }) => (
<List
className="options_list"
//style={{ border: "1px solid #ddd", borderRadius: "0.25rem" }}
height={listHeight}
itemCount={itemCount}
itemSize={itemHeight}
onItemsRendered={onItemsRendered}
ref={ref}
width={listWidth}
outerElementType={CustomScrollbarsVirtualList}
>
{Item}
</List>
)}
</InfiniteLoader>
</div>
{displayType === "dropdown" &&
size === "full" &&
convertedGroups &&
convertedGroups.length > 0 && (
<div className="data_column_two">
<ADSelectorGroupsHeader headerLabel="Groups" />
<ADSelectorGroupsBody
options={convertedGroups}
selectedOptions={selectedGrps}
isMultiSelect={isMultiSelect}
currentGroup={currentGroup}
listHeight={listHeight}
itemHeight={itemHeight}
onRowChecked={onGroupChange}
/>
</div>
)}
</div>
<ADSelectorFooter
buttonLabel={buttonLabel}
isDisabled={!selectedOptions || !selectedOptions.length}
isMultiSelect={isMultiSelect}
onClick={onButtonClick}
selectedOptions={selectedOptions}
/>
</StyledContainer>
);
};
ADSelector.propTypes = {
isOpen: PropTypes.bool,
options: PropTypes.array,
groups: PropTypes.array,
hasNextPage: PropTypes.bool,
isNextPageLoading: PropTypes.bool,
loadNextPage: PropTypes.func,
value: PropTypes.string,
placeholder: PropTypes.string,
isDisabled: PropTypes.bool,
onSearchChanged: PropTypes.func,
isMultiSelect: PropTypes.bool,
buttonLabel: PropTypes.string,
selectAllLabel: PropTypes.string,
size: PropTypes.string,
displayType: PropTypes.oneOf(["dropdown", "aside"]),
onAddNewClick: PropTypes.func,
allowCreation: PropTypes.bool,
onSelect: PropTypes.func,
onChange: PropTypes.func,
onGroupSelect: PropTypes.func,
onGroupChange: PropTypes.func,
selectedOptions: PropTypes.array,
selectedGroups: PropTypes.array,
selectedAll: PropTypes.bool,
onCancel: PropTypes.func,
allowAnyClickClose: PropTypes.bool
};
export default ADSelector;

View File

@ -1,537 +0,0 @@
import React from "react";
import PropTypes from "prop-types";
import styled, { css } from "styled-components";
import Checkbox from "../../checkbox";
import ComboBox from "../../combobox";
import filter from "lodash/filter";
import isEqual from "lodash/isEqual";
import isEmpty from "lodash/isEmpty";
import ADSelectorOptionsHeader from "./options/header";
import ADSelectorOptionsBody from "./options/body";
import ADSelectorGroupsHeader from "./groups/header";
import ADSelectorGroupsBody from "./groups/body";
import ADSelectorFooter from "./footer";
import { handleAnyClick } from "../../../utils/event";
/* eslint-disable no-unused-vars */
/* eslint-disable react/prop-types */
const Container = ({
value,
placeholder,
isMultiSelect,
size,
width,
maxHeight,
isDisabled,
onSelect,
onSearchChanged,
options,
selectedOptions,
buttonLabel,
selectAllLabel,
groups,
selectedGroups,
onGroupSelect,
onGroupChange,
isOpen,
displayType,
containerWidth,
containerHeight,
allowCreation,
onAddNewClick,
allowAnyClickClose,
hasNextPage,
isNextPageLoading,
loadNextPage,
isSelected,
...props
}) => <div {...props} />;
/* eslint-enable react/prop-types */
/* eslint-enable no-unused-vars */
const StyledContainer = styled(Container)`
display: flex;
flex-direction: column;
${props => (props.containerWidth ? `width: ${props.containerWidth};` : "")}
${props => (props.containerHeight ? `height: ${props.containerHeight};` : "")}
.data_container {
margin: 16px 16px -5px 16px;
.head_container {
display: flex;
margin-bottom: ${props => (props.displayType === "dropdown" ? 8 : 16)}px;
.options_searcher {
display: inline-block;
width: 100%;
${props =>
props.displayType === "dropdown" &&
props.size === "full" &&
css`
margin-right: ${props => (props.allowCreation ? 8 : 16)}px;
`}
/*${props =>
props.allowCreation
? css`
width: 272px;
margin-right: 8px;
`
: css`
width: ${props => (props.isDropDown ? "313px" : "100%")};
`}*/
}
.add_new_btn {
${props =>
props.allowCreation &&
css`
display: inline-block;
vertical-align: top;
height: 32px;
width: 36px;
margin-right: 16px;
line-height: 18px;
`}
}
}
.options_group_selector {
margin-bottom: 12px;
}
.data_column_one {
${props =>
props.size === "full" &&
props.displayType === "dropdown" &&
props.groups &&
props.groups.length > 0
? css`
width: 50%;
display: inline-block;
`
: ""}
.options_list {
margin-top: 4px;
margin-left: -8px;
.option {
line-height: 32px;
padding-left: ${props => (props.isMultiSelect ? 8 : 0)}px;
cursor: pointer;
.option_checkbox {
/*margin-left: 8px;*/
}
.option_link {
padding-left: 8px;
}
&:hover {
background-color: #f8f9f9;
}
}
}
}
.data_column_two {
${props =>
props.displayType === "dropdown" &&
props.groups &&
props.groups.length > 0
? css`
width: 50%;
display: inline-block;
border-left: 1px solid #eceef1;
`
: ""}
.group_header {
font-weight: 600;
padding-left: 16px;
padding-bottom: 14px;
}
.group_list {
margin-left: 8px;
.option {
line-height: 32px;
padding-left: 8px;
cursor: pointer;
.option_checkbox {
/*margin-left: 8px;*/
}
.option_link {
padding-left: 8px;
}
&:hover {
background-color: #f8f9f9;
}
}
.option.selected {
background-color: #ECEEF1;
}
}
}
}
`;
class ADSelector extends React.Component {
constructor(props) {
super(props);
this.ref = React.createRef();
const { groups, selectedOptions, selectedGroups, selectedAll, isOpen } = props;
const convertedGroups = this.convertGroups(groups);
const currentGroup = this.getCurrentGroup(convertedGroups);
this.state = {
selectedOptions: selectedOptions || [],
selectedAll: selectedAll || false,
groups: convertedGroups,
selectedGroups: selectedGroups || [],
currentGroup: currentGroup
};
if (isOpen) handleAnyClick(true, this.handleClick);
}
handleClick = e => {
const { onCancel, allowAnyClickClose, isOpen } = this.props;
if (
isOpen &&
allowAnyClickClose &&
this.ref &&
this.ref.current &&
!this.ref.current.contains(e.target) &&
e &&
e.target &&
e.target.className &&
typeof e.target.className.indexOf === "function" &&
e.target.className.indexOf("option_checkbox") === -1
) {
onCancel && onCancel();
}
};
componentWillUnmount() {
handleAnyClick(false, this.handleClick);
}
shouldComponentUpdate(nextProps, nextState) {
return !isEqual(this.props, nextProps) || !isEqual(this.state, nextState);
}
componentDidUpdate(prevProps) {
const {
groups,
selectedAll,
isMultiSelect,
selectedOptions,
selectedGroups,
allowAnyClickClose,
isOpen
} = this.props;
if (isOpen !== prevProps.isOpen) {
handleAnyClick(isOpen, this.handleClick);
}
if (allowAnyClickClose !== prevProps.allowAnyClickClose) {
handleAnyClick(allowAnyClickClose, this.handleClick);
}
let newState = {};
if (!isEqual(selectedOptions, prevProps.selectedOptions)) {
newState = { selectedOptions };
}
if (!isEqual(selectedGroups, prevProps.selectedGroups)) {
newState = Object.assign({}, newState, { selectedGroups });
}
if (isMultiSelect !== prevProps.isMultiSelect) {
newState = Object.assign({}, newState, {
selectedOptions: []
});
}
if (selectedAll !== prevProps.selectedAll) {
newState = Object.assign({}, newState, {
selectedAll
});
}
if (!isEqual(groups, prevProps.groups)) {
const newGroups = this.convertGroups(groups);
const currentGroup = this.getCurrentGroup(newGroups);
newState = Object.assign({}, newState, {
groups: newGroups,
currentGroup
});
}
if (!isEmpty(newState)) {
this.setState({ ...this.state, ...newState });
}
}
convertGroups = groups => {
if (!groups) return [];
const wrappedGroups = groups.map(this.convertGroup);
return wrappedGroups;
};
convertGroup = group => {
return {
key: group.key,
label: `${group.label} (${group.total})`,
total: group.total
};
};
getCurrentGroup = groups => {
const currentGroup = groups.length > 0 ? groups[0] : "No groups";
return currentGroup;
};
onButtonClick = () => {
this.props.onSelect &&
this.props.onSelect(
this.state.selectedAll ? this.props.options : this.state.selectedOptions
);
};
onSelectedAllChange = e => {
this.setState({
selectedAll: e.target.checked,
selectedOptions: e.target.checked ? this.props.options : []
});
};
onOptionSelect = option => {
this.props.onSelect && this.props.onSelect(option);
};
onOptionChange = (option, e) => {
const { selectedOptions } = this.state;
const newSelectedOptions = e.target.checked
? [...selectedOptions, option]
: filter(selectedOptions, obj => obj.key !== option.key);
//console.log("onChange", option, e.target.checked, newSelectedOptions);
this.setState({
selectedOptions: newSelectedOptions,
selectedAll: newSelectedOptions.length === this.props.options.length
});
};
onGroupChange = (group, e) => {
/*this.setState({
currentGroup: group
});
this.props.onGroupChange && this.props.onGroupChange(group);*/
const { selectedGroups } = this.state;
const newSelectedGroups = e.target.checked
? [...selectedGroups, group]
: filter(selectedGroups, obj => obj.key !== group.key);
//console.log("onChange", option, e.target.checked, newSelectedOptions);
this.setState({
selectedGroups: newSelectedGroups,
currentGroup: group
});
this.props.onGroupSelect && this.props.onGroupSelect(group);
};
render() {
//console.log("ADSelector render");
const {
options,
hasNextPage,
isNextPageLoading,
loadNextPage,
value,
placeholder,
isDisabled,
onSearchChanged,
isMultiSelect,
buttonLabel,
selectAllLabel,
size,
displayType,
onAddNewClick,
allowCreation
} = this.props;
const { selectedOptions, selectedAll, currentGroup, groups, selectedGroups } = this.state;
let containerHeight;
let containerWidth;
let listHeight;
let listWidth;
const itemHeight = 32;
const hasGroups = groups && groups.length > 0;
switch (size) {
case "compact":
containerHeight = hasGroups ? "326px" : "100%";
containerWidth = "379px";
listWidth = displayType === "dropdown" ? 356 : 356;
listHeight = hasGroups ? 488 : isMultiSelect ? 176 : 226;
break;
case "full":
default:
containerHeight = "100%";
containerWidth = displayType === "dropdown" ? "690px" : "326px";
listWidth = displayType === "dropdown" ? 320 : 300;
listHeight = 488;
break;
}
// If there are more items to be loaded then add an extra row to hold a loading indicator.
//const itemCount = hasNextPage ? options.length + 1 : options.length;
return (
<StyledContainer
containerHeight={containerHeight}
containerWidth={containerWidth}
{...this.props}
>
<div ref={this.ref}>
<div className="data_container">
<div className="data_column_one">
<ADSelectorOptionsHeader
value={value}
searchPlaceHolder={placeholder}
isDisabled={isDisabled}
allowCreation={allowCreation}
onAddNewClick={onAddNewClick}
onChange={onSearchChanged}
onClearSearch={onSearchChanged.bind(this, "")}
/>
{displayType === "aside" && groups && groups.length > 0 && (
<ComboBox
className="options_group_selector"
isDisabled={isDisabled}
options={groups}
selectedOption={currentGroup}
dropDownMaxHeight={200}
scaled={true}
scaledOptions={true}
size="content"
onSelect={this.onCurrentGroupChange}
/>
)}
{isMultiSelect && !groups && !groups.length && (
<Checkbox
label={selectAllLabel}
isChecked={
selectedAll || selectedOptions.length === options.length
}
isIndeterminate={!selectedAll && selectedOptions.length > 0}
className="option_select_all_checkbox"
onChange={this.onSelectedAllChange}
/>
)}
<ADSelectorOptionsBody
options={options}
hasNextPage={hasNextPage}
isNextPageLoading={isNextPageLoading}
loadNextPage={loadNextPage}
isMultiSelect={isMultiSelect}
listHeight={listHeight}
listWidth={listWidth}
itemHeight={itemHeight}
onRowChecked={this.onOptionChange}
onRowSelect={this.onOptionSelect}
selectedOptions={selectedOptions}
selectedAll={selectedAll}
/>
</div>
{displayType === "dropdown" &&
size === "full" &&
groups &&
groups.length > 0 && (
<div className="data_column_two">
<ADSelectorGroupsHeader headerLabel="Groups" />
<ADSelectorGroupsBody
options={groups}
selectedOptions={selectedGroups}
isMultiSelect={isMultiSelect}
currentGroup={currentGroup}
listHeight={listHeight}
itemHeight={itemHeight}
onRowChecked={this.onGroupChange}
/>
</div>
)}
</div>
<ADSelectorFooter
buttonLabel={buttonLabel}
isDisabled={
!this.state.selectedOptions ||
!this.state.selectedOptions.length
}
isMultiSelect={isMultiSelect}
onClick={this.onButtonClick}
selectedOptions={selectedOptions}
/>
</div>
</StyledContainer>
);
}
}
ADSelector.propTypes = {
isOpen: PropTypes.bool,
options: PropTypes.array,
groups: PropTypes.array,
hasNextPage: PropTypes.bool,
isNextPageLoading: PropTypes.bool,
loadNextPage: PropTypes.func,
value: PropTypes.string,
placeholder: PropTypes.string,
isDisabled: PropTypes.bool,
onSearchChanged: PropTypes.func,
isMultiSelect: PropTypes.bool,
buttonLabel: PropTypes.string,
selectAllLabel: PropTypes.string,
size: PropTypes.string,
displayType: PropTypes.oneOf(["dropdown", "aside"]),
onAddNewClick: PropTypes.func,
allowCreation: PropTypes.bool,
onSelect: PropTypes.func,
onChange: PropTypes.func,
onGroupSelect: PropTypes.func,
onGroupChange: PropTypes.func,
selectedOptions: PropTypes.array,
selectedGroups: PropTypes.array,
selectedAll: PropTypes.bool,
onCancel: PropTypes.func,
allowAnyClickClose: PropTypes.bool,
};
export default ADSelector;

View File

@ -51,7 +51,6 @@ export { default as EmptyScreenContainer} from './components/empty-screen-contai
export { default as CustomScrollbarsVirtualList } from './components/scrollbar/custom-scrollbars-virtual-list'
export { default as RowContent } from './components/row-content'
export { default as Calendar } from './components/calendar'
export { default as AdvancedSelector } from './components/advanced-selector'
export { default as AdvancedSelector2 } from './components/advanced-selector2'
export { default as ContextMenu } from './components/context-menu'
export { default as RowContainer } from './components/row-container'