Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Carousel: Set CarouselNav totalSlides to controlled #33453

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
Mitch-At-Work marked this conversation as resolved.
Show resolved Hide resolved
"type": "patch",
"comment": "fix: Set totalSlides on CarouselNav to controlled state",
"packageName": "@fluentui/react-carousel",
"email": "[email protected]",
"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
Loading