@zohodesk/hooks
Version:
Unified Component Library - Hooks
101 lines (94 loc) • 1.92 kB
JavaScript
/* eslint-disable camelcase */
import React, { useEffect } from 'react';
import multiSelectCategory from "./multiselectcategory";
import useCommonReducer from "../utils/useCommonReducer";
import * as constants from "./constants";
export default function useMultiSelectCategory(props, reducer) {
let {
options,
selectedGroup,
selectedOptions
} = props;
const {
state = {},
dispatch
} = useCommonReducer(multiSelectCategory, reducer, {
selectedSuggestions: {},
filteredOptions: {},
list: [],
groupList: [],
selectedGroup,
searchStr: ""
});
useEffect(() => {
dispatch({
type: constants.ADD,
data: {
options,
groupname: selectedGroup,
selectedOptions
}
});
}, []);
function handleSelect(value) {
dispatch({
type: constants.SELECT,
data: {
value
}
});
}
function handleSelectAll(groupname) {
dispatch({
type: constants.SELECT_ALL,
data: {
groupname
}
});
}
function handleRemoveAll() {
dispatch({
type: constants.REMOVE_ALL,
data: {}
});
}
function handleGroupSelect(value) {
dispatch({
type: constants.GROUPSELECT,
data: {
value
}
});
}
function handleRemove(value, groupname) {
dispatch({
type: constants.REMOVE,
data: {
value,
groupname
}
});
}
function handleSearch(value) {
dispatch({
type: constants.SEARCH,
data: {
value
}
});
}
return {
selectedOptions: state.selectedSuggestions,
handleSelect,
list: state.list,
filteredOptions: state.filteredOptions,
groupList: state.groupList,
selectedGroup: state.selectedGroup,
handleSelectAll,
handleRemoveAll,
handleGroupSelect,
handleRemove,
handleSearch,
searchString: state.searchStr
};
}