MCPcopy Index your code
hub / github.com/reactstrap/reactstrap / handleKeyDown

Method handleKeyDown

src/Dropdown.js:130–218  ·  view source on GitHub ↗
(e)

Source from the content-addressed store, hash-verified

128 }
129
130 handleKeyDown(e) {
131 const isTargetMenuItem =
132 e.target.getAttribute('role') === 'menuitem' ||
133 e.target.getAttribute('role') === 'option';
134 const isTargetMenuCtrl = this.getMenuCtrl() === e.target;
135 const isTab = keyCodes.tab === e.which;
136
137 if (
138 /input|textarea/i.test(e.target.tagName) ||
139 (isTab && !this.props.a11y) ||
140 (isTab && !(isTargetMenuItem || isTargetMenuCtrl))
141 ) {
142 return;
143 }
144
145 if (
146 preventDefaultKeys.indexOf(e.which) !== -1 ||
147 (e.which >= 48 && e.which <= 90)
148 ) {
149 e.preventDefault();
150 }
151
152 if (this.props.disabled) return;
153
154 if (isTargetMenuCtrl) {
155 if (
156 [keyCodes.space, keyCodes.enter, keyCodes.up, keyCodes.down].indexOf(
157 e.which,
158 ) > -1
159 ) {
160 // Open the menu (if not open) and focus the first menu item
161 if (!this.props.isOpen) {
162 this.toggle(e);
163 }
164 setTimeout(() => this.getMenuItems()[0]?.focus());
165 } else if (this.props.isOpen && isTab) {
166 // Focus the first menu item if tabbing from an open menu. We need this
167 // for cases where the DropdownMenu sets a custom container, which may
168 // not be the natural next item to tab to from the DropdownToggle.
169 e.preventDefault();
170 this.getMenuItems()[0]?.focus();
171 } else if (this.props.isOpen && e.which === keyCodes.esc) {
172 this.toggle(e);
173 }
174 }
175
176 if (this.props.isOpen && isTargetMenuItem) {
177 if ([keyCodes.tab, keyCodes.esc].indexOf(e.which) > -1) {
178 this.toggle(e);
179 this.getMenuCtrl().focus();
180 } else if ([keyCodes.space, keyCodes.enter].indexOf(e.which) > -1) {
181 e.target.click();
182 this.getMenuCtrl().focus();
183 } else if (
184 [keyCodes.down, keyCodes.up].indexOf(e.which) > -1 ||
185 ([keyCodes.n, keyCodes.p].indexOf(e.which) > -1 && e.ctrlKey)
186 ) {
187 const $menuitems = this.getMenuItems();

Callers

nothing calls this directly

Calls 4

getMenuCtrlMethod · 0.95
toggleMethod · 0.95
getMenuItemsMethod · 0.95
focusMethod · 0.80

Tested by

no test coverage detected