MCPcopy Create free account
hub / github.com/adobe/react-spectrum / focusLast

Function focusLast

packages/react-aria/src/datepicker/useDatePickerGroup.ts:83–119  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

81
82 // Focus the first placeholder segment from the end on mouse down/touch up in the field.
83 let focusLast = () => {
84 if (!ref.current) {
85 return;
86 }
87 // Try to find the segment prior to the element that was clicked on.
88 let target = window.event ? (getEventTarget(window.event) as FocusableElement) : null;
89 let walker = getFocusableTreeWalker(ref.current, {tabbable: true});
90 if (target) {
91 walker.currentNode = target;
92 target = walker.previousNode() as FocusableElement;
93 }
94
95 // If no target found, find the last element from the end.
96 if (!target) {
97 let last: FocusableElement;
98 do {
99 last = walker.lastChild() as FocusableElement;
100 if (last) {
101 target = last;
102 }
103 } while (last);
104 }
105
106 // Now go backwards until we find an element that is not a placeholder.
107 while (target?.hasAttribute('data-placeholder')) {
108 let prev = walker.previousNode() as FocusableElement;
109 if (prev && prev.hasAttribute('data-placeholder')) {
110 target = prev;
111 } else {
112 break;
113 }
114 }
115
116 if (target) {
117 target.focus();
118 }
119 };
120
121 let {pressProps} = usePress({
122 preventFocusOnPress: true,

Callers 2

onPressStartFunction · 0.70
onPressFunction · 0.70

Calls 6

getEventTargetFunction · 0.90
getFocusableTreeWalkerFunction · 0.90
previousNodeMethod · 0.80
hasAttributeMethod · 0.80
focusMethod · 0.80
lastChildMethod · 0.45

Tested by

no test coverage detected