Skip to content

Commit

Permalink
Carousel: Set CarouselNav totalSlides to controlled (#33453)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitch-At-Work authored Dec 11, 2024
1 parent c142edc commit 1175ab2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: Set totalSlides on CarouselNav to controlled state",
"packageName": "@fluentui/react-carousel",
"email": "mifraser@microsoft.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as React from 'react';

import { useCarouselContext_unstable as useCarouselContext } from '../CarouselContext';
import type { CarouselNavProps, CarouselNavState } from './CarouselNav.types';
import { useControllableState } from '@fluentui/react-utilities';

/**
* Create the state required to render CarouselNav.
Expand All @@ -25,14 +26,19 @@ export const useCarouselNav_unstable = (props: CarouselNavProps, ref: React.Ref<
unstable_hasDefault: true,
});

const [totalSlides, setTotalSlides] = React.useState(props.totalSlides ?? 0);
// Users can choose controlled or uncontrolled, if uncontrolled, the default is initialized by carousel context
const [totalSlides, setTotalSlides] = useControllableState({
state: props.totalSlides,
initialState: 0,
});

const subscribeForValues = useCarouselContext(ctx => ctx.subscribeForValues);

useIsomorphicLayoutEffect(() => {
return subscribeForValues(data => {
setTotalSlides(data.navItemsCount);
});
}, [subscribeForValues]);
}, [subscribeForValues, setTotalSlides]);

return {
totalSlides,
Expand Down

0 comments on commit 1175ab2

Please sign in to comment.