UNPKG

@zohodesk/hooks

Version:

Unified Component Library - Hooks

198 lines (178 loc) 4.49 kB
import React from "react"; import * as constants from "./constants"; function filterSearch(str, options) { const searchStr = str.toLowerCase(); let data = []; Object.keys(options).map(obj => { const text = obj && obj.toLowerCase(); if (Object.keys(options).indexOf(obj) !== -1 && text.indexOf(searchStr) !== -1) { data.push(obj); } }); return data; } const multiSelectCategory = function (state, action) { let initialState = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { selectedSuggestions: {}, filteredOptions: {}, list: [], groupList: [], selectedGroup: "", searchStr: "" }; let stateJson = initialState; const actionType = action.type; const { data } = action; let vals = [], val = {}, selected, value = action.data.value; let { selectedSuggestions, filteredOptions, list, selectedGroup } = state; let CN; switch (actionType) { case constants.ADD: let { options, selectedOptions, groupname } = action.data; options && options.map(option => { vals.push(Object.keys(option)[0]); val = { ...val, ...option }; }); let SG = val[groupname], fo = {}; SG && SG.map(option => { fo = { ...fo, [option.value]: option }; }); let data = { ...state, list: val, selectedGroup: selectedGroup ? selectedGroup : '', filteredOptions: selectedGroup ? fo : {}, selectedSuggestions: selectedOptions ? selectedOptions : {}, groupList: vals }; stateJson = { ...data }; return stateJson; case constants.GROUPSELECT: vals = list[value]; val = {}; vals.map(option => { val = { ...val, [option.value]: option }; }); stateJson = { ...state, selectedGroup: value, filteredOptions: val }; return stateJson; case constants.SELECT: vals = selectedSuggestions[selectedGroup] ? selectedSuggestions[selectedGroup] : []; let filter = 0; if (vals.length) { vals.map((val, i) => { if (val.value !== value) { filter++; } filter == vals.length ? vals.push(filteredOptions[value]) : null; }); } else { vals.push(filteredOptions[value]); } selected = selectedSuggestions[selectedGroup] ? { [selectedGroup]: [...vals] } : { [selectedGroup]: vals }; stateJson = { ...state, selectedSuggestions: { ...selectedSuggestions, ...selected } }; return stateJson; case constants.REMOVE: CN = action.data.groupname; selectedSuggestions[CN] && selectedSuggestions[CN].map((suggestion, i) => { if (suggestion.value !== value) { vals.push(suggestion); } }); let gp, ss = selectedSuggestions; if (vals.length) { gp = { [CN]: vals }; } else { ss[CN] && delete ss[CN]; } stateJson = { ...state, selectedSuggestions: { ...ss, ...gp } }; return stateJson; case constants.SEARCH: if (value) { vals = filterSearch(value, filteredOptions); vals.length && vals.map((item, i) => { val = { ...val, [item]: { ...filteredOptions[item] } }; }); } else { vals = list[selectedGroup]; vals.map(option => { val = { ...val, [option.value]: option }; }); } stateJson = { ...state, searchStr: value, filteredOptions: val }; return stateJson; case constants.SELECT_ALL: CN = action.data.groupname; val = selectedSuggestions[CN]; if (val) { delete selectedSuggestions[CN]; selected = { [CN]: list[CN] }; } else { selected = { [CN]: list[CN] }; } stateJson = { ...state, selectedSuggestions: { ...selectedSuggestions, ...selected } }; return stateJson; case constants.REMOVE_ALL: stateJson = { ...state, selectedSuggestions: {} }; return stateJson; default: return stateJson; } }; export default multiSelectCategory;