MCPcopy Create free account
hub / github.com/Noumena-Network/code / reducer

Function reducer

src/components/CustomSelect/use-select-navigation.ts:74–330  ·  view source on GitHub ↗
(state: State<T>, action: Action<T>)

Source from the content-addressed store, hash-verified

72}
73
74const reducer = <T>(state: State<T>, action: Action<T>): State<T> => {
75 switch (action.type) {
76 case 'focus-next-option': {
77 if (state.focusedValue === undefined) {
78 return state
79 }
80
81 const item = state.optionMap.get(state.focusedValue)
82
83 if (!item) {
84 return state
85 }
86
87 // Wrap to first item if at the end
88 const next = item.next || state.optionMap.first
89
90 if (!next) {
91 return state
92 }
93
94 // When wrapping to first, reset viewport to start
95 if (!item.next && next === state.optionMap.first) {
96 return {
97 ...state,
98 focusedValue: next.value,
99 visibleFromIndex: 0,
100 visibleToIndex: state.visibleOptionCount,
101 }
102 }
103
104 const needsToScroll = next.index >= state.visibleToIndex
105
106 if (!needsToScroll) {
107 return {
108 ...state,
109 focusedValue: next.value,
110 }
111 }
112
113 const nextVisibleToIndex = Math.min(
114 state.optionMap.size,
115 state.visibleToIndex + 1,
116 )
117
118 const nextVisibleFromIndex = nextVisibleToIndex - state.visibleOptionCount
119
120 return {
121 ...state,
122 focusedValue: next.value,
123 visibleFromIndex: nextVisibleFromIndex,
124 visibleToIndex: nextVisibleToIndex,
125 }
126 }
127
128 case 'focus-previous-option': {
129 if (state.focusedValue === undefined) {
130 return state
131 }

Callers

nothing calls this directly

Calls 2

maxMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected