diff --git a/src/lib/components/container.jsx b/src/lib/components/container.jsx index aac01a2..cc37ad4 100644 --- a/src/lib/components/container.jsx +++ b/src/lib/components/container.jsx @@ -56,11 +56,8 @@ export default function Container(props) { } = props // Global state from context - const { - state, - dispatch, - customStyles - } = useContext(StateContext) + const { state, dispatch } = useContext(StateContext) + const { customStyles } = state // Component state const [debouncedQuery] = useDebounce(state.query, debounceWait) diff --git a/src/lib/components/item.jsx b/src/lib/components/item.jsx index 6869f39..2862230 100644 --- a/src/lib/components/item.jsx +++ b/src/lib/components/item.jsx @@ -11,20 +11,20 @@ export default function Item(props) { const { state, - dispatch, - customStyles, - splitCharState + dispatch } = useContext(StateContext) + const { customStyles, highlighted, splitChar } = state + const splitText = useMemo(() => { - if (isUndefined(splitCharState)) return [item.text] - const regex = new RegExp(escapeStringRegExp(splitCharState) + '(.+)') + if (isUndefined(splitChar)) return [item.text] + const regex = new RegExp(escapeStringRegExp(splitChar) + '(.+)') return item.text.split(regex) - }, [splitCharState, item]) + }, [splitChar, item]) const divClassName = useMemo(() => { let itemStyle = customStyles[ - (state.highlighted && index === state.highlighted.index) + (highlighted && index === highlighted.index) ? 'highlightedItem' : 'item' ] @@ -32,7 +32,7 @@ export default function Item(props) { return (index === 0 && customStyles.topItem) ? `${itemStyle} ${customStyles.topItem}` : itemStyle - }, [customStyles, state.highlighted, index]) + }, [customStyles, highlighted, index]) const handleMouseEnter = () => { dispatch(setHighlighted(index)) @@ -43,8 +43,8 @@ export default function Item(props) { } const itemElement = () => { - return isUndefined(splitCharState) || - !item.text.includes(splitCharState) ? ( + return isUndefined(splitChar) || + !item.text.includes(splitChar) ? ( ) : ( splitItemElement() @@ -55,7 +55,7 @@ export default function Item(props) { return ( - {splitCharState} + {splitChar} {splitText[1]} ) diff --git a/src/lib/components/itemFirst.jsx b/src/lib/components/itemFirst.jsx index f9fe3b3..81d94bc 100644 --- a/src/lib/components/itemFirst.jsx +++ b/src/lib/components/itemFirst.jsx @@ -4,7 +4,8 @@ import Item from './item' export default function ItemFirst(props) { const { groupName, index, item } = props - const { customStyles } = useContext(StateContext) + const { state } = useContext(StateContext) + const { customStyles } = state return ( diff --git a/src/lib/components/items.jsx b/src/lib/components/items.jsx index a026f7c..6ce39aa 100644 --- a/src/lib/components/items.jsx +++ b/src/lib/components/items.jsx @@ -7,7 +7,8 @@ import isUndefined from '../utils/isUndefined' export default function Items(props) { const { isLoading, items, loadingMessage, noItemsMessage } = props - const { customStyles } = useContext(StateContext) + const { state } = useContext(StateContext) + const { customStyles } = state const itemElements = () => { return ( diff --git a/src/lib/components/matchingText.jsx b/src/lib/components/matchingText.jsx index 030211d..ecfc20d 100644 --- a/src/lib/components/matchingText.jsx +++ b/src/lib/components/matchingText.jsx @@ -4,7 +4,8 @@ import escapeStringRegExp from 'escape-string-regexp' export default function MatchingText(props) { const { text } = props - const { customStyles, state } = useContext(StateContext) + const { state } = useContext(StateContext) + const { customStyles } = state const regex = new RegExp('(' + escapeStringRegExp(state.query) + ')', 'i') const parts = (state.query) ? text.split(regex) : [text] const matchingText = parts.map((part, index) => { diff --git a/src/lib/context/state.jsx b/src/lib/context/state.jsx index 341066c..80503c2 100644 --- a/src/lib/context/state.jsx +++ b/src/lib/context/state.jsx @@ -1,4 +1,4 @@ -import React, { createContext, useReducer, useState, useEffect } from 'react' +import React, { createContext, useReducer, useEffect } from 'react' import reducer from '../reducers/reducer' import {setQuery} from '../actions/actions' import undef from '../utils/undef' @@ -15,19 +15,12 @@ const StateContextProvider = (props) => { customStyles: styles, splitChar }) - const [customStyles] = useState(styles) - const [splitCharState] = useState(splitChar) useEffect(() => dispatch(setQuery(text)), [text]) // TODO: Is this needed? return ( + value={{state, dispatch}}> {children} )