Merge pull request 'feat(ui): history stack for stepper' (#4834) from ui/fix-backwards-nav-installer into main
Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/4834
This commit is contained in:
@@ -3,7 +3,6 @@ import {
|
|||||||
createContext,
|
createContext,
|
||||||
createSignal,
|
createSignal,
|
||||||
JSX,
|
JSX,
|
||||||
Setter,
|
|
||||||
useContext,
|
useContext,
|
||||||
} from "solid-js";
|
} from "solid-js";
|
||||||
import { createStore, SetStoreFunction, Store } from "solid-js/store";
|
import { createStore, SetStoreFunction, Store } from "solid-js/store";
|
||||||
@@ -28,9 +27,15 @@ export function createStepper<
|
|||||||
s: { steps: T },
|
s: { steps: T },
|
||||||
stepOpts: StepOptions<StepId, StoreType>,
|
stepOpts: StepOptions<StepId, StoreType>,
|
||||||
): StepperReturn<T, T[number]["id"]> {
|
): StepperReturn<T, T[number]["id"]> {
|
||||||
const [activeStep, setActiveStep] = createSignal<T[number]["id"]>(
|
const [history, setHistory] = createSignal<T[number]["id"][]>([
|
||||||
stepOpts.initialStep,
|
stepOpts.initialStep,
|
||||||
);
|
]);
|
||||||
|
|
||||||
|
const activeStep = () => history()[history().length - 1];
|
||||||
|
|
||||||
|
const setActiveStep = (id: T[number]["id"]) => {
|
||||||
|
setHistory((prev) => [...prev, id]);
|
||||||
|
};
|
||||||
|
|
||||||
const store: StoreTuple<StoreType> = createStore<StoreType>(
|
const store: StoreTuple<StoreType> = createStore<StoreType>(
|
||||||
stepOpts.initialStoreData ?? ({} as StoreType),
|
stepOpts.initialStoreData ?? ({} as StoreType),
|
||||||
@@ -66,19 +71,19 @@ export function createStepper<
|
|||||||
setActiveStep(s.steps[currentIndex + 1].id);
|
setActiveStep(s.steps[currentIndex + 1].id);
|
||||||
},
|
},
|
||||||
previous: () => {
|
previous: () => {
|
||||||
const currentIndex = s.steps.findIndex(
|
const hist = history();
|
||||||
(step) => step.id === activeStep(),
|
if (hist.length <= 1) {
|
||||||
);
|
|
||||||
if (currentIndex <= 0) {
|
|
||||||
throw new Error("No previous step available");
|
throw new Error("No previous step available");
|
||||||
}
|
}
|
||||||
setActiveStep(s.steps[currentIndex - 1].id);
|
|
||||||
|
setHistory((prev) => {
|
||||||
|
const update = prev.slice(0, -1);
|
||||||
|
setActiveStep(update[update.length - 1]);
|
||||||
|
return update;
|
||||||
|
});
|
||||||
},
|
},
|
||||||
hasPrevious: () => {
|
hasPrevious: () => {
|
||||||
const currentIndex = s.steps.findIndex(
|
return history().length > 1;
|
||||||
(step) => step.id === activeStep(),
|
|
||||||
);
|
|
||||||
return currentIndex > 0;
|
|
||||||
},
|
},
|
||||||
hasNext: () => {
|
hasNext: () => {
|
||||||
const currentIndex = s.steps.findIndex(
|
const currentIndex = s.steps.findIndex(
|
||||||
@@ -96,7 +101,7 @@ export interface StepperReturn<
|
|||||||
> {
|
> {
|
||||||
_store: never;
|
_store: never;
|
||||||
activeStep: Accessor<StepId>;
|
activeStep: Accessor<StepId>;
|
||||||
setActiveStep: Setter<StepId>;
|
setActiveStep: (id: StepId) => void;
|
||||||
currentStep: () => T[number];
|
currentStep: () => T[number];
|
||||||
next: () => void;
|
next: () => void;
|
||||||
previous: () => void;
|
previous: () => void;
|
||||||
|
|||||||
Reference in New Issue
Block a user