@yamada-ui/stepper
Version:
Yamada UI stepper component
1 lines • 4.08 kB
Source Map (JSON)
{"version":3,"sources":["../src/use-stepper.ts"],"sourcesContent":["import type { CSSUIObject, HTMLUIProps, PropGetter } from \"@yamada-ui/core\"\nimport { createDescendant } from \"@yamada-ui/use-descendant\"\nimport { createContext, mergeRefs } from \"@yamada-ui/utils\"\nimport { useCallback } from \"react\"\n\ninterface StepperContext\n extends Omit<UseStepperReturn, \"descendants\" | \"getContainerProps\"> {\n styles: { [key: string]: CSSUIObject | undefined }\n}\n\nexport const {\n DescendantsContextProvider: StepperDescendantsContextProvider,\n useDescendant: useStepperDescendant,\n useDescendants: useStepperDescendants,\n} = createDescendant<HTMLDivElement>()\n\nexport const [StepperProvider, useStepperContext] =\n createContext<StepperContext>({\n name: \"StepperContext\",\n errorMessage: `useStepperContext returned is 'undefined'. Seems you forgot to wrap the components in \"<Stepper />\"`,\n })\n\nexport interface UseStepperProps extends HTMLUIProps {\n /**\n * The active step index.\n */\n index: number\n /**\n * The orientation of the stepper.\n *\n * @default 'horizontal'\n */\n orientation?: \"horizontal\" | \"vertical\"\n /**\n * Whether to show or not the last separator while in vertical orientation.\n *\n * @default false\n */\n showLastSeparator?: boolean\n}\n\nexport const useStepper = ({\n index,\n orientation = \"horizontal\",\n showLastSeparator = false,\n ...rest\n}: UseStepperProps) => {\n const descendants = useStepperDescendants()\n\n const getStepStatus = useCallback(\n (step: number): \"active\" | \"complete\" | \"incomplete\" => {\n if (step < index) return \"complete\"\n if (step > index) return \"incomplete\"\n\n return \"active\"\n },\n [index],\n )\n\n const getContainerProps: PropGetter = useCallback(\n (props = {}, ref = null) => ({\n ...rest,\n ...props,\n ref,\n \"data-orientation\": orientation,\n }),\n [orientation, rest],\n )\n\n return {\n descendants,\n getStepStatus,\n index,\n orientation,\n showLastSeparator,\n getContainerProps,\n }\n}\n\nexport type UseStepperReturn = ReturnType<typeof useStepper>\n\nexport const useStep = () => {\n const { getStepStatus, orientation } = useStepperContext()\n\n const { descendants, index, register } = useStepperDescendant()\n\n const isFirst = index === 0\n const isLast = index === descendants.lastValue()?.index\n\n const status = getStepStatus(index)\n\n const getStepProps: PropGetter = useCallback(\n (props = {}, ref = null) => ({\n ...props,\n ref: mergeRefs(ref, register),\n \"data-orientation\": orientation,\n \"data-status\": status,\n }),\n [orientation, register, status],\n )\n\n return { index, isFirst, isLast, status, getStepProps }\n}\n\nexport type UseStepReturn = ReturnType<typeof useStep>\n"],"mappings":";;;AACA,SAAS,wBAAwB;AACjC,SAAS,eAAe,iBAAiB;AACzC,SAAS,mBAAmB;AAOrB,IAAM;AAAA,EACX,4BAA4B;AAAA,EAC5B,eAAe;AAAA,EACf,gBAAgB;AAClB,IAAI,iBAAiC;AAE9B,IAAM,CAAC,iBAAiB,iBAAiB,IAC9C,cAA8B;AAAA,EAC5B,MAAM;AAAA,EACN,cAAc;AAChB,CAAC;AAqBI,IAAM,aAAa,CAAC;AAAA,EACzB;AAAA,EACA,cAAc;AAAA,EACd,oBAAoB;AAAA,EACpB,GAAG;AACL,MAAuB;AACrB,QAAM,cAAc,sBAAsB;AAE1C,QAAM,gBAAgB;AAAA,IACpB,CAAC,SAAuD;AACtD,UAAI,OAAO,MAAO,QAAO;AACzB,UAAI,OAAO,MAAO,QAAO;AAEzB,aAAO;AAAA,IACT;AAAA,IACA,CAAC,KAAK;AAAA,EACR;AAEA,QAAM,oBAAgC;AAAA,IACpC,CAAC,QAAQ,CAAC,GAAG,MAAM,UAAU;AAAA,MAC3B,GAAG;AAAA,MACH,GAAG;AAAA,MACH;AAAA,MACA,oBAAoB;AAAA,IACtB;AAAA,IACA,CAAC,aAAa,IAAI;AAAA,EACpB;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAIO,IAAM,UAAU,MAAM;AAjF7B;AAkFE,QAAM,EAAE,eAAe,YAAY,IAAI,kBAAkB;AAEzD,QAAM,EAAE,aAAa,OAA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