Skip to content

Commit

Permalink
feat(ts/components/routePropertiesCard): pull section open state up i…
Browse files Browse the repository at this point in the history
…nto `RoutePropertiesCard.WithSectionState`
  • Loading branch information
firestack committed May 23, 2024
1 parent 99eddb7 commit afd1f61
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const DetourRouteSelectionPanel = ({
<section className="pb-3">
<h2 className="c-diversion-panel__h2">Choose direction</h2>
{selectedRouteInfo.selectedRoute ? (
<RoutePropertiesCard
<RoutePropertiesCard.WithSectionState
routePatterns={selectedRouteInfo.routePatterns}
selectedRoutePatternId={selectedRoutePatternFromInfo(
selectedRouteInfo
Expand Down
2 changes: 1 addition & 1 deletion assets/src/components/mapPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const SelectedRoute = ({
const routePatterns = usePatternsByIdForRoute(selectedRoutePattern.routeId)

return routePatterns ? (
<RoutePropertiesCard
<RoutePropertiesCard.WithSectionState
routePatterns={routePatterns}
selectedRoutePatternId={selectedRoutePattern.routePatternId}
selectRoutePattern={selectRoutePattern}
Expand Down
42 changes: 27 additions & 15 deletions assets/src/components/mapPage/routePropertiesCard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from "react"
import React, { ComponentPropsWithoutRef, useState } from "react"
import { useRoute } from "../../contexts/routesContext"
import { CircleCheckIcon } from "../../helpers/icon"
import {
Expand Down Expand Up @@ -231,17 +231,19 @@ const RoutePropertiesCard = ({
selectedRoutePatternId,
selectRoutePattern,
onClose,
defaultOpened = null,

openSection,
onChangeSection,
}: {
routePatterns: ByRoutePatternId<RoutePattern>
selectedRoutePatternId: RoutePatternId
selectRoutePattern: (routePattern: RoutePattern) => void

onClose?: () => void
defaultOpened?: RoutePropertiesCardOpened
}) => {
const [openedDetails, setOpenedDetails] =
useState<RoutePropertiesCardOpened>(defaultOpened)

openSection?: RoutePropertiesCardOpened
onChangeSection?: (newSection: RoutePropertiesCardOpened) => void
}) => {
const selectedRoutePattern = routePatterns[selectedRoutePatternId]
const route: Route | null = useRoute(selectedRoutePattern?.routeId)

Expand Down Expand Up @@ -274,11 +276,9 @@ const RoutePropertiesCard = ({
/>
<DetailSection
title="variants"
isOpen={openedDetails === "variants"}
isOpen={openSection === "variants"}
toggleOpen={() =>
setOpenedDetails((prevValue) =>
prevValue === "variants" ? null : "variants"
)
onChangeSection?.(openSection === "variants" ? null : "variants")
}
className="variant-picker"
toggleClassName="c-route-properties-card__disclosure_toggle--variants"
Expand All @@ -291,7 +291,6 @@ const RoutePropertiesCard = ({
selectedRoutePatternId={selectedRoutePattern.id}
selectRoutePattern={(routePattern: RoutePattern) => {
selectRoutePattern(routePattern)
setOpenedDetails(null)
}}
/>
</DetailSection>
Expand All @@ -300,11 +299,9 @@ const RoutePropertiesCard = ({
title={`${(
route.directionNames[selectedRoutePattern.directionId] || ""
).toLowerCase()} stops`}
isOpen={openedDetails === "stops"}
isOpen={openSection === "stops"}
toggleOpen={() =>
setOpenedDetails((prevValue) =>
prevValue === "stops" ? null : "stops"
)
onChangeSection?.(openSection === "stops" ? null : "stops")
}
className="variant-stop-list"
>
Expand All @@ -321,4 +318,19 @@ const RoutePropertiesCard = ({
)
}

interface RPCSectionStateProps extends Omit<ComponentPropsWithoutRef<typeof RoutePropertiesCard>, "openSection" | "onChangeSection"> {
defaultOpenSection?: RoutePropertiesCardOpened
}

const RoutePropertiesCardWithSectionState = ({defaultOpenSection: openSectionDefault, ...props}: RPCSectionStateProps) => {
const [openSection, setOpenSection] = useState<RoutePropertiesCardOpened>(openSectionDefault ?? null)

return <RoutePropertiesCard
openSection={openSection}
onChangeSection={setOpenSection}
{...props}
/>
}
RoutePropertiesCard.WithSectionState = RoutePropertiesCardWithSectionState

export default RoutePropertiesCard
10 changes: 5 additions & 5 deletions assets/stories/skate-components/routePropertiesCard.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const routePatterns = {
const route = routeFactory.build({ id: "52" })

const meta = {
component: RoutePropertiesCard,
component: RoutePropertiesCard.WithSectionState,
args: {
routePatterns: routePatterns,
selectedRoutePatternId: routePattern0.id,
Expand All @@ -84,10 +84,10 @@ const meta = {
</RoutesProvider>
),
],
} satisfies Meta<typeof RoutePropertiesCard>
} satisfies Meta<typeof RoutePropertiesCard.WithSectionState>
export default meta

type Story = StoryObj<typeof RoutePropertiesCard>
type Story = StoryObj<typeof RoutePropertiesCard.WithSectionState>

export const Default: Story = {}
export const WithInboundSelected: Story = {
Expand All @@ -98,11 +98,11 @@ export const WithInboundSelected: Story = {

export const WithVariantsOpenedByDefault: Story = {
args: {
defaultOpened: "variants",
defaultOpenSection: "variants",
},
}
export const WithStopsOpenedByDefault: Story = {
args: {
defaultOpened: "stops",
defaultOpenSection: "stops",
},
}
4 changes: 2 additions & 2 deletions assets/tests/components/mapPage/routePropertiesCard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ const RoutePropertiesCardWithDefaults = ({
}) => {
const thing = (
<RoutesProvider routes={routes}>
<RoutePropertiesCard
<RoutePropertiesCard.WithSectionState
routePatterns={routePatterns}
selectedRoutePatternId={selectedRoutePatternId}
selectRoutePattern={selectRoutePattern}
onClose={onClose}
defaultOpened={defaultOpened}
defaultOpenSection={defaultOpened}
/>
</RoutesProvider>
)
Expand Down

0 comments on commit afd1f61

Please sign in to comment.