@opentripplanner/trip-details 1.6.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__mocks__/custom-messages.yml +7 -0
- package/esm/index.js +215 -140
- package/esm/index.js.map +1 -1
- package/esm/styled.js +30 -18
- package/esm/styled.js.map +1 -1
- package/esm/trip-detail.js +0 -9
- package/esm/trip-detail.js.map +1 -1
- package/esm/types.js +2 -0
- package/esm/types.js.map +1 -0
- package/i18n/en-US.yml +43 -0
- package/i18n/fr.yml +31 -0
- package/lib/index.d.ts +10 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +204 -131
- package/lib/index.js.map +1 -1
- package/lib/styled.d.ts +23 -0
- package/lib/styled.d.ts.map +1 -0
- package/lib/styled.js +40 -19
- package/lib/styled.js.map +1 -1
- package/lib/trip-detail.d.ts +18 -0
- package/lib/trip-detail.d.ts.map +1 -0
- package/lib/trip-detail.js +0 -12
- package/lib/trip-detail.js.map +1 -1
- package/lib/types.d.ts +62 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +2 -0
- package/lib/types.js.map +1 -0
- package/package.json +10 -7
- package/src/TripDetails.story.tsx +315 -0
- package/src/__unpublished__/external-types.d.ts +22 -0
- package/src/__unpublished__/messages.d.ts +2 -0
- package/src/index.tsx +402 -0
- package/src/{styled.js → styled.ts} +6 -0
- package/src/{trip-detail.js → trip-detail.tsx} +17 -18
- package/src/types.ts +69 -0
- package/tsconfig.json +15 -0
- package/tsconfig.tsbuildinfo +4496 -0
- package/src/TripDetails.story.js +0 -155
- package/src/index.js +0 -324
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import flatten from "flat";
|
|
2
|
+
import React, { ReactElement } from "react";
|
|
3
|
+
import { IntlProvider } from "react-intl";
|
|
4
|
+
import { Meta, Story as StoryType } from "@storybook/react";
|
|
5
|
+
import styled from "styled-components";
|
|
6
|
+
// The below eslint-disable is due to https://github.com/storybookjs/storybook/issues/13408
|
|
7
|
+
// eslint-disable-next-line import/no-named-as-default
|
|
8
|
+
import TripDetails from ".";
|
|
9
|
+
import * as TripDetailsClasses from "./styled";
|
|
10
|
+
import {
|
|
11
|
+
CaloriesDetailsProps,
|
|
12
|
+
DepartureDetailsProps,
|
|
13
|
+
FareDetailsProps,
|
|
14
|
+
TripDetailsProps
|
|
15
|
+
} from "./types";
|
|
16
|
+
|
|
17
|
+
import englishMessages from "../i18n/en-US.yml";
|
|
18
|
+
import frenchMessages from "../i18n/fr.yml";
|
|
19
|
+
import customMessages from "../__mocks__/custom-messages.yml";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Describes args passed to stories.
|
|
23
|
+
*/
|
|
24
|
+
interface StoryArgs {
|
|
25
|
+
locale?: string;
|
|
26
|
+
useCustomMessages?: boolean;
|
|
27
|
+
useLocalizedMessages?: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// import mock itinaries. These are all trip plan outputs from OTP.
|
|
31
|
+
const bikeOnlyItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/bike-only.json");
|
|
32
|
+
const bikeRentalItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/bike-rental.json");
|
|
33
|
+
const bikeRentalTransitBikeRentalItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/bike-rental-transit-bike-rental.json");
|
|
34
|
+
const bikeTransitBikeItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/bike-transit-bike.json");
|
|
35
|
+
const eScooterRentalItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/e-scooter-rental.json");
|
|
36
|
+
const eScooterRentalTransiteScooterRentalItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/e-scooter-transit-e-scooter.json");
|
|
37
|
+
const parkAndRideItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/park-and-ride.json");
|
|
38
|
+
const tncTransitTncItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/tnc-transit-tnc.json");
|
|
39
|
+
const walkInterlinedTransitItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/walk-interlined-transit-walk.json");
|
|
40
|
+
const walkOnlyItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/walk-only.json");
|
|
41
|
+
const walkTransitWalkItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/walk-transit-walk.json");
|
|
42
|
+
const walkTransitWalkTransitWalkItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/walk-transit-walk-transit-walk.json");
|
|
43
|
+
const fareComponentsItinerary = require("@opentripplanner/itinerary-body/src/__mocks__/itineraries/fare-components.json");
|
|
44
|
+
|
|
45
|
+
// Change currency code on one of the itineraries for illustration.
|
|
46
|
+
// (other currency fields in the itinerary object are not used for display).
|
|
47
|
+
const itinCurrency =
|
|
48
|
+
walkTransitWalkTransitWalkItinerary.fare.fare.regular.currency;
|
|
49
|
+
itinCurrency.currencyCode = "EUR";
|
|
50
|
+
|
|
51
|
+
const StyledTripDetails = styled(TripDetails)`
|
|
52
|
+
${TripDetailsClasses.TripDetailsHeader} {
|
|
53
|
+
background-color: pink;
|
|
54
|
+
}
|
|
55
|
+
`;
|
|
56
|
+
|
|
57
|
+
const longDateFormat = "MMMM D, YYYY";
|
|
58
|
+
|
|
59
|
+
const englishFareKeyMap = {
|
|
60
|
+
regular: "Transit Fare",
|
|
61
|
+
student: "Student Fare",
|
|
62
|
+
senior: "Senior Fare",
|
|
63
|
+
tram: "Tram Fare",
|
|
64
|
+
special: "Special Fare",
|
|
65
|
+
youth: "Youth Fare",
|
|
66
|
+
electronicRegular: "Orca Fare",
|
|
67
|
+
electronicYouth: "Orca Youth Fare",
|
|
68
|
+
electronicSpecial: "Orca Special Fare",
|
|
69
|
+
electronicSenior: "Orca Senior Fare"
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const frenchFareKeyMap = {
|
|
73
|
+
regular: "Tarif en transports",
|
|
74
|
+
student: "Tarif étudiants",
|
|
75
|
+
senior: "Tarif séniors",
|
|
76
|
+
tram: "Tarif tram",
|
|
77
|
+
special: "Tarif spécial",
|
|
78
|
+
youth: "Tarif jeunes",
|
|
79
|
+
electronicRegular: "Tarif Orca",
|
|
80
|
+
electronicYouth: "Tarif Orca jeunes",
|
|
81
|
+
electronicSpecial: "Tarif Orca spécial",
|
|
82
|
+
electronicSenior: "Tarif Orca séniors"
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// Custom slots for expandable detail sections.
|
|
86
|
+
const CustomDepartureDetails = ({
|
|
87
|
+
departureDate
|
|
88
|
+
}: DepartureDetailsProps): ReactElement => (
|
|
89
|
+
<>
|
|
90
|
+
Custom messages about {departureDate.format(longDateFormat)} can be
|
|
91
|
+
constructed dynamically using any markup.
|
|
92
|
+
</>
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
const CustomFareDetails = ({
|
|
96
|
+
maxTNCFare,
|
|
97
|
+
minTNCFare,
|
|
98
|
+
transitFares
|
|
99
|
+
}: FareDetailsProps): ReactElement => (
|
|
100
|
+
<>
|
|
101
|
+
Custom details about fares (transitFares: {JSON.stringify(transitFares)}{" "}
|
|
102
|
+
(cents), minTNCFare: {minTNCFare} and maxTNCFare: {maxTNCFare} can be
|
|
103
|
+
constructed dynamically using any markup.
|
|
104
|
+
</>
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
const CustomCaloriesDetails = ({
|
|
108
|
+
bikeSeconds,
|
|
109
|
+
calories,
|
|
110
|
+
walkSeconds
|
|
111
|
+
}: CaloriesDetailsProps): ReactElement => (
|
|
112
|
+
<>
|
|
113
|
+
Custom message about {calories} calories burned,
|
|
114
|
+
{walkSeconds} seconds and {bikeSeconds} seconds.
|
|
115
|
+
</>
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Create a template component for each TripDetails story.
|
|
120
|
+
* A Component argument is passed. Once bound,
|
|
121
|
+
* the Component to render should not change.
|
|
122
|
+
* @param Component The component to render.
|
|
123
|
+
*/
|
|
124
|
+
function createTripDetailsTemplate(
|
|
125
|
+
Component: React.ElementType<TripDetailsProps> = TripDetails
|
|
126
|
+
) {
|
|
127
|
+
const TripDetailsTemplate = ({
|
|
128
|
+
CaloriesDetails,
|
|
129
|
+
defaultFare,
|
|
130
|
+
DepartureDetails,
|
|
131
|
+
FareDetails,
|
|
132
|
+
itinerary,
|
|
133
|
+
locale,
|
|
134
|
+
useCustomFareKeyMap
|
|
135
|
+
}: TripDetailsProps): ReactElement => {
|
|
136
|
+
const fareKeyNameMap = useCustomFareKeyMap
|
|
137
|
+
? locale === "en-US"
|
|
138
|
+
? englishFareKeyMap
|
|
139
|
+
: frenchFareKeyMap
|
|
140
|
+
: {};
|
|
141
|
+
return (
|
|
142
|
+
<Component
|
|
143
|
+
CaloriesDetails={CaloriesDetails}
|
|
144
|
+
defaultFare={defaultFare}
|
|
145
|
+
DepartureDetails={DepartureDetails}
|
|
146
|
+
FareDetails={FareDetails}
|
|
147
|
+
fareKeyNameMap={fareKeyNameMap}
|
|
148
|
+
itinerary={itinerary}
|
|
149
|
+
/>
|
|
150
|
+
);
|
|
151
|
+
};
|
|
152
|
+
return TripDetailsTemplate;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const intlDecorator = (
|
|
156
|
+
Story: StoryType,
|
|
157
|
+
context: {
|
|
158
|
+
args: StoryArgs;
|
|
159
|
+
}
|
|
160
|
+
): ReactElement => {
|
|
161
|
+
const { args } = context;
|
|
162
|
+
const { locale, useCustomMessages, useLocalizedMessages } = args;
|
|
163
|
+
const messages = locale === "en-US" ? englishMessages : frenchMessages;
|
|
164
|
+
|
|
165
|
+
// Construct a messages object that customizes a subset
|
|
166
|
+
// of the default messages of the desired locale.
|
|
167
|
+
// The structure of the message objects is
|
|
168
|
+
// flattened before it is passed to IntlProvider.
|
|
169
|
+
// Note: the spread operator (...) is rejected if used with flatten,
|
|
170
|
+
// so we use Object.assign instead
|
|
171
|
+
// (see e.g. https://github.com/Microsoft/TypeScript/issues/10727#issuecomment-423712256).
|
|
172
|
+
let mergedMessages: Record<string, string> = flatten(messages);
|
|
173
|
+
if (useCustomMessages) {
|
|
174
|
+
mergedMessages = Object.assign(mergedMessages, flatten(customMessages));
|
|
175
|
+
}
|
|
176
|
+
return (
|
|
177
|
+
<IntlProvider
|
|
178
|
+
locale={locale}
|
|
179
|
+
messages={useLocalizedMessages ? mergedMessages : null}
|
|
180
|
+
>
|
|
181
|
+
<Story />
|
|
182
|
+
</IntlProvider>
|
|
183
|
+
);
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Helper to simplify story declaration.
|
|
188
|
+
*/
|
|
189
|
+
function makeStory(
|
|
190
|
+
args: StoryArgs | TripDetailsProps,
|
|
191
|
+
Component?: React.ElementType<TripDetailsProps>
|
|
192
|
+
) {
|
|
193
|
+
const BoundTripDetails = createTripDetailsTemplate(Component).bind({});
|
|
194
|
+
BoundTripDetails.args = args;
|
|
195
|
+
return BoundTripDetails;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const decoratorPropDescription = "(This prop is used by the decorator.)";
|
|
199
|
+
// Hide story controls for some props.
|
|
200
|
+
const noControl = {
|
|
201
|
+
control: { type: false }
|
|
202
|
+
};
|
|
203
|
+
// Hide some story args completely.
|
|
204
|
+
const hiddenProp = {
|
|
205
|
+
table: { disable: true }
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
export default {
|
|
209
|
+
argTypes: {
|
|
210
|
+
CaloriesDetails: noControl,
|
|
211
|
+
className: noControl,
|
|
212
|
+
Component: hiddenProp,
|
|
213
|
+
defaultFareKey: noControl,
|
|
214
|
+
DepartureDetails: noControl,
|
|
215
|
+
FareDetails: noControl,
|
|
216
|
+
fareKeyNameMap: noControl,
|
|
217
|
+
itinerary: noControl,
|
|
218
|
+
locale: {
|
|
219
|
+
control: "radio",
|
|
220
|
+
description: decoratorPropDescription,
|
|
221
|
+
options: ["en-US", "fr"]
|
|
222
|
+
},
|
|
223
|
+
useCustomFareKeyMap: hiddenProp,
|
|
224
|
+
useCustomMessages: {
|
|
225
|
+
description: decoratorPropDescription
|
|
226
|
+
},
|
|
227
|
+
useLocalizedMessages: {
|
|
228
|
+
description: decoratorPropDescription
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
args: {
|
|
232
|
+
defaultFareKey: "regular",
|
|
233
|
+
locale: "en-US",
|
|
234
|
+
useCustomMessages: false,
|
|
235
|
+
useLocalizedMessages: true
|
|
236
|
+
},
|
|
237
|
+
component: TripDetails,
|
|
238
|
+
decorators: [intlDecorator],
|
|
239
|
+
parameters: { controls: { sort: "alpha" } },
|
|
240
|
+
title: "TripDetails"
|
|
241
|
+
} as Meta;
|
|
242
|
+
|
|
243
|
+
export const WalkOnlyItinerary = makeStory({
|
|
244
|
+
itinerary: walkOnlyItinerary
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
export const BikeOnlyItinerary = makeStory({
|
|
248
|
+
itinerary: bikeOnlyItinerary
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
export const WalkTransitWalkItinerary = makeStory({
|
|
252
|
+
itinerary: walkTransitWalkItinerary
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
export const StyledWalkTransitWalkItinerary = makeStory(
|
|
256
|
+
{
|
|
257
|
+
itinerary: walkTransitWalkItinerary
|
|
258
|
+
},
|
|
259
|
+
StyledTripDetails
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
export const BikeTransitBikeItinerary = makeStory({
|
|
263
|
+
itinerary: bikeTransitBikeItinerary
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
export const WalkInterlinedTransitItinerary = makeStory({
|
|
267
|
+
defaultFareKey: "electronicRegular",
|
|
268
|
+
itinerary: walkInterlinedTransitItinerary,
|
|
269
|
+
useCustomFareKeyMap: true
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
export const WalkTransitTransferItinerary = makeStory({
|
|
273
|
+
itinerary: walkTransitWalkTransitWalkItinerary
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
export const BikeRentalItinerary = makeStory({
|
|
277
|
+
itinerary: bikeRentalItinerary
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
export const EScooterRentalItinerary = makeStory({
|
|
281
|
+
itinerary: eScooterRentalItinerary
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
export const ParkAndRideItinerary = makeStory({
|
|
285
|
+
itinerary: parkAndRideItinerary
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
export const BikeRentalTransitItinerary = makeStory({
|
|
289
|
+
itinerary: bikeRentalTransitBikeRentalItinerary
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
export const EScooterRentalTransitItinerary = makeStory({
|
|
293
|
+
itinerary: eScooterRentalTransiteScooterRentalItinerary
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
export const TncTransitItinerary = makeStory({
|
|
297
|
+
itinerary: tncTransitTncItinerary,
|
|
298
|
+
useCustomFareKeyMap: true
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
export const TncTransitItineraryWithCustomDetails = makeStory(
|
|
302
|
+
{
|
|
303
|
+
CaloriesDetails: CustomCaloriesDetails,
|
|
304
|
+
defaultFareKey: "electronicRegular",
|
|
305
|
+
DepartureDetails: CustomDepartureDetails,
|
|
306
|
+
FareDetails: CustomFareDetails,
|
|
307
|
+
itinerary: tncTransitTncItinerary,
|
|
308
|
+
useCustomFareKeyMap: true
|
|
309
|
+
},
|
|
310
|
+
StyledTripDetails
|
|
311
|
+
);
|
|
312
|
+
|
|
313
|
+
export const FlexItinerary = makeStory({
|
|
314
|
+
itinerary: fareComponentsItinerary
|
|
315
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Type declarations for dependencies that don't export types.
|
|
2
|
+
|
|
3
|
+
// Package velocity-react does not export types
|
|
4
|
+
// and does not exist in the @types repository.
|
|
5
|
+
declare module "velocity-react" {
|
|
6
|
+
// Important! imports must be within modules.
|
|
7
|
+
import { ReactElement } from "react";
|
|
8
|
+
|
|
9
|
+
interface TransitionInfo {
|
|
10
|
+
animation: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface VelocityTransitionGroupProps {
|
|
14
|
+
enter?: TransitionInfo;
|
|
15
|
+
leave?: TransitionInfo;
|
|
16
|
+
children?: ReactElement;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export declare function VelocityTransitionGroup(
|
|
20
|
+
props: VelocityTransitionGroupProps
|
|
21
|
+
): ReactElement;
|
|
22
|
+
}
|