mirror of
https://github.com/fergalmoran/turnstone.git
synced 2025-12-22 09:49:56 +00:00
Simplify rules for whether listbox is expanded
This commit is contained in:
@@ -47,25 +47,25 @@ const defaultItemGroups = [
|
|||||||
const App = () => {
|
const App = () => {
|
||||||
const onChange = useCallback(
|
const onChange = useCallback(
|
||||||
(text) => {
|
(text) => {
|
||||||
console.log('Changed to:', text)
|
//console.log('Changed to:', text)
|
||||||
}, []
|
}, []
|
||||||
)
|
)
|
||||||
|
|
||||||
const onSelect = useCallback(
|
const onSelect = useCallback(
|
||||||
(selectedResult) => {
|
(selectedResult) => {
|
||||||
console.log('Selected Result:', selectedResult)
|
//console.log('Selected Result:', selectedResult)
|
||||||
}, []
|
}, []
|
||||||
)
|
)
|
||||||
|
|
||||||
const onEnter = useCallback(
|
const onEnter = useCallback(
|
||||||
(query, selectedResult) => {
|
(query, selectedResult) => {
|
||||||
console.log('Enter Pressed. Selected Result:', selectedResult, query)
|
//console.log('Enter Pressed. Selected Result:', selectedResult, query)
|
||||||
}, []
|
}, []
|
||||||
)
|
)
|
||||||
|
|
||||||
const onTab = useCallback(
|
const onTab = useCallback(
|
||||||
(query, selectedResult) => {
|
(query, selectedResult) => {
|
||||||
console.log('Tab Pressed. Selected Result:', selectedResult, query)
|
//console.log('Tab Pressed. Selected Result:', selectedResult, query)
|
||||||
}, []
|
}, []
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import React, { useState, useMemo, useRef, useContext } from 'react'
|
import React, { useState, useRef, useContext } from 'react'
|
||||||
import { StateContext } from '../context/state'
|
import { StateContext } from '../context/state'
|
||||||
import ListBox from './listBox'
|
import ListBox from './listBox'
|
||||||
import { useDebounce } from 'use-debounce'
|
import { useDebounce } from 'use-debounce'
|
||||||
@@ -115,7 +115,6 @@ export default function Container(props) {
|
|||||||
useSelected(state.selected, queryInput, typeaheadInput, onSelect)
|
useSelected(state.selected, queryInput, typeaheadInput, onSelect)
|
||||||
|
|
||||||
const onTabOrEnter = (keyPressed) => {
|
const onTabOrEnter = (keyPressed) => {
|
||||||
// keyPressed must be 'enter' or 'tab'
|
|
||||||
const highlightedIndex = state.highlighted && state.highlighted.index
|
const highlightedIndex = state.highlighted && state.highlighted.index
|
||||||
const highlightedItem = !isUndefined(highlightedIndex)
|
const highlightedItem = !isUndefined(highlightedIndex)
|
||||||
? state.items[highlightedIndex]
|
? state.items[highlightedIndex]
|
||||||
@@ -129,11 +128,9 @@ export default function Container(props) {
|
|||||||
return clearButton && !!state.query
|
return clearButton && !!state.query
|
||||||
}
|
}
|
||||||
|
|
||||||
const isExpanded = useMemo(() => {
|
const isExpanded = () => {
|
||||||
if (hasFocus && !state.query && defaultItemGroups) return true
|
return hasFocus && state.itemsLoaded
|
||||||
if (state.query.length < minQueryLength) return false
|
}
|
||||||
return hasFocus && !!state.query
|
|
||||||
}, [hasFocus, state.query, defaultItemGroups, minQueryLength])
|
|
||||||
|
|
||||||
// Handle different keypresses and call the appropriate action creators
|
// Handle different keypresses and call the appropriate action creators
|
||||||
const checkKey = (evt) => {
|
const checkKey = (evt) => {
|
||||||
@@ -243,7 +240,7 @@ export default function Container(props) {
|
|||||||
aria-label={clearButtonAriaLabel}>{clearButtonText}</div>
|
aria-label={clearButtonAriaLabel}>{clearButtonText}</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{isExpanded && (
|
{isExpanded() && (
|
||||||
<ListBox
|
<ListBox
|
||||||
id={listBoxId}
|
id={listBoxId}
|
||||||
items={state.items}
|
items={state.items}
|
||||||
|
|||||||
@@ -9,8 +9,6 @@ const filterSuppliedData = (group, query) => {
|
|||||||
? dataSearchType.toLowerCase()
|
? dataSearchType.toLowerCase()
|
||||||
: dataSearchType
|
: dataSearchType
|
||||||
|
|
||||||
if(!query.length) return []
|
|
||||||
|
|
||||||
switch (searchType) {
|
switch (searchType) {
|
||||||
case 'startswith':
|
case 'startswith':
|
||||||
return data.filter((item) =>
|
return data.filter((item) =>
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ const StateContextProvider = (props) => {
|
|||||||
const [state, dispatch] = useReducer(reducer, {
|
const [state, dispatch] = useReducer(reducer, {
|
||||||
query: text,
|
query: text,
|
||||||
items,
|
items,
|
||||||
|
itemsLoaded: false,
|
||||||
highlighted: undef,
|
highlighted: undef,
|
||||||
selected: undef,
|
selected: undef,
|
||||||
customStyles: styles,
|
customStyles: styles,
|
||||||
|
|||||||
@@ -9,8 +9,17 @@ const reducer = (state, action) => {
|
|||||||
const newState = (() => {
|
const newState = (() => {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case types.SET_QUERY:
|
case types.SET_QUERY:
|
||||||
|
// Disallow listbox until user has entered a long enough query
|
||||||
|
if(action.payload.query.length < state.props.minQueryLength)
|
||||||
|
action.payload.itemsLoaded = false
|
||||||
|
|
||||||
|
// Allow listbox if there is no query and we have default items to show
|
||||||
|
if(action.payload.query.length === 0 && state.props.defaultItemGroups)
|
||||||
|
action.payload.itemsLoaded = true
|
||||||
return action.payload
|
return action.payload
|
||||||
case types.SET_ITEMS:
|
case types.SET_ITEMS:
|
||||||
|
if(action.payload.items.length)
|
||||||
|
action.payload.itemsLoaded = true
|
||||||
return action.payload
|
return action.payload
|
||||||
case types.SET_HIGHLIGHTED:
|
case types.SET_HIGHLIGHTED:
|
||||||
return { highlighted: highlightedItem(action.index, state) }
|
return { highlighted: highlightedItem(action.index, state) }
|
||||||
|
|||||||
Reference in New Issue
Block a user