| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {ReactNode} from 'react'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import {useMemo, useState} from 'react'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | export type StepConfig<TKey> = Readonly<{ |
| b69ab31 | | | 13 | /** |
| b69ab31 | | | 14 | * Key to identify this step. |
| b69ab31 | | | 15 | */ |
| b69ab31 | | | 16 | key: TKey; |
| b69ab31 | | | 17 | |
| b69ab31 | | | 18 | /** |
| b69ab31 | | | 19 | * Label to display (in the stepper, header, etc.) |
| b69ab31 | | | 20 | */ |
| b69ab31 | | | 21 | label: ReactNode; |
| b69ab31 | | | 22 | }>; |
| b69ab31 | | | 23 | |
| b69ab31 | | | 24 | export type MultiStepperContext<TKey> = Readonly<{ |
| b69ab31 | | | 25 | /* Getters */ |
| b69ab31 | | | 26 | |
| b69ab31 | | | 27 | /** |
| b69ab31 | | | 28 | * Gets the current step config. |
| b69ab31 | | | 29 | */ |
| b69ab31 | | | 30 | getCurrentStep: () => StepConfig<TKey>; |
| b69ab31 | | | 31 | |
| b69ab31 | | | 32 | /** |
| b69ab31 | | | 33 | * Gets the step config for the given key. |
| b69ab31 | | | 34 | */ |
| b69ab31 | | | 35 | getStep: (step: TKey) => StepConfig<TKey> | undefined; |
| b69ab31 | | | 36 | |
| b69ab31 | | | 37 | /** |
| b69ab31 | | | 38 | * Gets the index of the current step. |
| b69ab31 | | | 39 | */ |
| b69ab31 | | | 40 | getStepIndex: () => number; |
| b69ab31 | | | 41 | |
| b69ab31 | | | 42 | /** |
| b69ab31 | | | 43 | * Gets the total number of steps. |
| b69ab31 | | | 44 | */ |
| b69ab31 | | | 45 | getStepCount: () => number; |
| b69ab31 | | | 46 | |
| b69ab31 | | | 47 | /** |
| b69ab31 | | | 48 | * Get all step configs in order. |
| b69ab31 | | | 49 | */ |
| b69ab31 | | | 50 | getAllSteps: () => Array<StepConfig<TKey>>; |
| b69ab31 | | | 51 | |
| b69ab31 | | | 52 | /* Navigation Methods */ |
| b69ab31 | | | 53 | |
| b69ab31 | | | 54 | /** |
| b69ab31 | | | 55 | * Go to the step at the given index. |
| b69ab31 | | | 56 | */ |
| b69ab31 | | | 57 | goToStep: (index: number) => void; |
| b69ab31 | | | 58 | |
| b69ab31 | | | 59 | /** |
| b69ab31 | | | 60 | * Go to the step with the given key. |
| b69ab31 | | | 61 | */ |
| b69ab31 | | | 62 | goToStepByKey: (key: TKey) => void; |
| b69ab31 | | | 63 | |
| b69ab31 | | | 64 | /** |
| b69ab31 | | | 65 | * Go to the next step. |
| b69ab31 | | | 66 | */ |
| b69ab31 | | | 67 | goToNextStep: () => void; |
| b69ab31 | | | 68 | |
| b69ab31 | | | 69 | /** |
| b69ab31 | | | 70 | * Go to the previous step. |
| b69ab31 | | | 71 | */ |
| b69ab31 | | | 72 | goToPreviousStep: () => void; |
| b69ab31 | | | 73 | |
| b69ab31 | | | 74 | /** |
| b69ab31 | | | 75 | * Go to the first step. |
| b69ab31 | | | 76 | */ |
| b69ab31 | | | 77 | goToFirstStep: () => void; |
| b69ab31 | | | 78 | |
| b69ab31 | | | 79 | /** |
| b69ab31 | | | 80 | * Go to the last step. |
| b69ab31 | | | 81 | */ |
| b69ab31 | | | 82 | goToLastStep: () => void; |
| b69ab31 | | | 83 | }>; |
| b69ab31 | | | 84 | |
| b69ab31 | | | 85 | /** |
| b69ab31 | | | 86 | * Hook to access the current step and navigate to other steps. |
| b69ab31 | | | 87 | */ |
| b69ab31 | | | 88 | export function useMultiStepperContext<TKey>( |
| b69ab31 | | | 89 | pages: Array<StepConfig<TKey>>, |
| b69ab31 | | | 90 | ): MultiStepperContext<TKey> { |
| b69ab31 | | | 91 | const [currentStep, setCurrentStep] = useState<number>(0); |
| b69ab31 | | | 92 | |
| b69ab31 | | | 93 | const stepByKey = useMemo( |
| b69ab31 | | | 94 | () => new Map<TKey, number>(pages.map((page, index) => [page.key, index])), |
| b69ab31 | | | 95 | [pages], |
| b69ab31 | | | 96 | ); |
| b69ab31 | | | 97 | |
| b69ab31 | | | 98 | const value = useMemo( |
| b69ab31 | | | 99 | () => ({ |
| b69ab31 | | | 100 | _steps: pages, |
| b69ab31 | | | 101 | _currentStep: currentStep, |
| b69ab31 | | | 102 | _setCurrentStep: setCurrentStep, |
| b69ab31 | | | 103 | |
| b69ab31 | | | 104 | getCurrentStep: () => pages[currentStep], |
| b69ab31 | | | 105 | |
| b69ab31 | | | 106 | getStep: (step: TKey) => pages.at(stepByKey.get(step) ?? -1), |
| b69ab31 | | | 107 | |
| b69ab31 | | | 108 | getStepIndex: () => currentStep, |
| b69ab31 | | | 109 | |
| b69ab31 | | | 110 | getStepCount: () => pages.length, |
| b69ab31 | | | 111 | |
| b69ab31 | | | 112 | getAllSteps: () => pages, |
| b69ab31 | | | 113 | |
| b69ab31 | | | 114 | goToStep: (index: number) => setCurrentStep(index), |
| b69ab31 | | | 115 | |
| b69ab31 | | | 116 | goToStepByKey: (key: TKey) => { |
| b69ab31 | | | 117 | const index = stepByKey.get(key); |
| b69ab31 | | | 118 | if (index != null) { |
| b69ab31 | | | 119 | setCurrentStep(index); |
| b69ab31 | | | 120 | } |
| b69ab31 | | | 121 | }, |
| b69ab31 | | | 122 | |
| b69ab31 | | | 123 | goToNextStep: () => { |
| b69ab31 | | | 124 | if (currentStep < pages.length - 1) { |
| b69ab31 | | | 125 | setCurrentStep(currentStep + 1); |
| b69ab31 | | | 126 | } |
| b69ab31 | | | 127 | }, |
| b69ab31 | | | 128 | |
| b69ab31 | | | 129 | goToPreviousStep: () => { |
| b69ab31 | | | 130 | if (currentStep > 0) { |
| b69ab31 | | | 131 | setCurrentStep(currentStep - 1); |
| b69ab31 | | | 132 | } |
| b69ab31 | | | 133 | }, |
| b69ab31 | | | 134 | |
| b69ab31 | | | 135 | goToFirstStep: () => setCurrentStep(0), |
| b69ab31 | | | 136 | |
| b69ab31 | | | 137 | goToLastStep: () => setCurrentStep(pages.length - 1), |
| b69ab31 | | | 138 | }), |
| b69ab31 | | | 139 | [currentStep, pages, stepByKey], |
| b69ab31 | | | 140 | ); |
| b69ab31 | | | 141 | |
| b69ab31 | | | 142 | return value; |
| b69ab31 | | | 143 | } |