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

Function useWizardState

packages/react-aria-components/src/Wizard.tsx:235–353  ·  view source on GitHub ↗

* This hook controls the selected step, and derives which steps are completed and navigable. The * application decides which steps are completed, or by default, the steps before the selected step * are completed unless random access is allowed. The steps up to and including the one after the * la

(props: WizardStateOptions<T>)

Source from the content-addressed store, hash-verified

233 * and are not navigable unless random access is allowed.
234 */
235function useWizardState<T>(props: WizardStateOptions<T>): WizardState<T> {
236 let {
237 collection,
238 allowsSelectingIncompleteSteps = false,
239 disabledKeys: disabledStepKeys,
240 completedKeys: completedStepKeys,
241 onSelectionChange
242 } = props;
243
244 // Steps disabled by the user are never navigable.
245 let disabledSteps = useMemo(() => {
246 let set = new Set<Key>(disabledStepKeys);
247 // aggregate steps disabled via individual props as well
248 for (let step of collection) {
249 if (step.props?.isDisabled) {
250 set.add(step.key);
251 }
252 }
253 return set;
254 }, [collection, disabledStepKeys]);
255
256 let [selectedKeyState, setSelectedKeyState] = useControlledState<Key | null>(
257 props.selectedKey,
258 props.defaultSelectedKey ?? null,
259 onSelectionChange
260 ? key => {
261 if (key != null) {
262 onSelectionChange(key);
263 }
264 }
265 : undefined
266 );
267
268 // Like Tabs, select the first step that isn't disabled when no step is selected or the selected
269 // step is removed, or the first step if every step is disabled.
270 let selectedKey =
271 selectedKeyState != null && collection.getItem(selectedKeyState)
272 ? selectedKeyState
273 : (findEnabledStep(collection.getFirstKey(), disabledSteps, key =>
274 collection.getKeyAfter(key)
275 ) ?? collection.getFirstKey());
276
277 let completedKeys = useMemo(() => {
278 // controlled
279 if (completedStepKeys) {
280 return new Set<Key>(completedStepKeys);
281 }
282 // uncontrolled and linear
283 let set = new Set<Key>();
284 if (!allowsSelectingIncompleteSteps && selectedKey != null) {
285 let key = collection.getKeyBefore(selectedKey);
286 while (key != null) {
287 set.add(key);
288 key = collection.getKeyBefore(key);
289 }
290 }
291 return set;
292 }, [collection, completedStepKeys, allowsSelectingIncompleteSteps, selectedKey]);

Callers 1

WizardInnerFunction · 0.85

Calls 9

useControlledStateFunction · 0.90
findEnabledStepFunction · 0.85
getStepIndexFunction · 0.85
addMethod · 0.65
getItemMethod · 0.65
getFirstKeyMethod · 0.65
getKeyAfterMethod · 0.65
getKeyBeforeMethod · 0.65
onSelectionChangeFunction · 0.50

Tested by

no test coverage detected