@opentripplanner/from-to-location-picker 1.3.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/lib/types.d.ts ADDED
@@ -0,0 +1,42 @@
1
+ export declare type Location = {
2
+ lat: number;
3
+ lon: number;
4
+ name: string;
5
+ /**
6
+ * This is only used for locations that a user has saved. Can be either:
7
+ * "home" or "work"
8
+ */
9
+ type?: string;
10
+ };
11
+ export declare type FromToPickerProps = {
12
+ /**
13
+ * A specific location to associate with this. This is only used when combined
14
+ * with the setLocation prop.
15
+ */
16
+ location?: Location;
17
+ /**
18
+ * Triggered when the user clicks on the "from" button.
19
+ */
20
+ onFromClick: () => void;
21
+ /**
22
+ * Triggered when the user clicks on the "to" button.
23
+ */
24
+ onToClick: () => void;
25
+ /**
26
+ * Triggered when the user clicks either the "from" or "to" button and there
27
+ * are no from/to specific handler functions defined as props.
28
+ *
29
+ * Passes an argument as follows:
30
+ * { locationType: "from/to", location, reverseGeocode: false }
31
+ */
32
+ setLocation?: ({ locationType: string, location: Location, reverseGeocode: boolean }: {
33
+ locationType: any;
34
+ location: any;
35
+ reverseGeocode: any;
36
+ }) => void;
37
+ /**
38
+ * Determines whether icons are shown on the "from" and "to" buttons.
39
+ */
40
+ showIcons?: boolean;
41
+ };
42
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,oBAAY,QAAQ,GAAG;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,oBAAY,iBAAiB,GAAG;IAC9B;;;OAGG;IACH,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB;;OAEG;IACH,WAAW,EAAE,MAAM,IAAI,CAAC;IACxB;;OAEG;IACH,SAAS,EAAE,MAAM,IAAI,CAAC;IACtB;;;;;;OAMG;IACH,WAAW,CAAC,EAAE,CAAC,EACb,YAAY,EAAE,MAAM,EACpB,QAAQ,EAAE,QAAQ,EAClB,cAAc,EAAE,OAAO,EACxB;;;;KAAA,KAAK,IAAI,CAAC;IACX;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,CAAC"}
package/lib/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","sourcesContent":[],"file":"types.js"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opentripplanner/from-to-location-picker",
3
- "version": "1.3.0",
3
+ "version": "2.0.0",
4
4
  "description": "Location Picker Component",
5
5
  "author": "@binh-dam-ibigroup",
6
6
  "homepage": "https://github.com/opentripplanner/otp-ui/#readme",
@@ -9,12 +9,12 @@
9
9
  "module": "esm/index.js",
10
10
  "private": false,
11
11
  "dependencies": {
12
- "@opentripplanner/location-icon": "^1.3.0",
13
- "@opentripplanner/core-utils": "^4.1.0",
14
- "prop-types": "^15.7.2"
12
+ "@opentripplanner/location-icon": "^1.4.0",
13
+ "flat": "^5.0.2"
15
14
  },
16
15
  "peerDependencies": {
17
16
  "react": "^16.14.0",
17
+ "react-intl": "^5.20.4",
18
18
  "styled-components": "^5.3.0"
19
19
  },
20
20
  "repository": {
@@ -22,7 +22,7 @@
22
22
  "url": "git+https://github.com/opentripplanner/otp-ui.git"
23
23
  },
24
24
  "scripts": {
25
- "test": "echo \"Error: run tests from root\" && exit 1"
25
+ "tsc": "tsc && echo Remove the @ts-expect-error exception in index.tsx once core-utils is typed!"
26
26
  },
27
27
  "bugs": {
28
28
  "url": "https://github.com/opentripplanner/otp-ui/issues"
@@ -0,0 +1,2 @@
1
+ // Generic module definition for all YAML file imports.
2
+ declare module "*.yml";
@@ -0,0 +1,48 @@
1
+ import React from "react";
2
+ import { IntlProvider } from "react-intl";
3
+ import { action } from "@storybook/addon-actions";
4
+
5
+ import FromToLocationPicker from ".";
6
+
7
+ import "../__mocks__/trimet-mock.css";
8
+
9
+ export default {
10
+ title: "From-To-Picker",
11
+ component: FromToLocationPicker
12
+ };
13
+
14
+ const onFromClick = action("onFromClick");
15
+ const onToClick = action("onToClick");
16
+
17
+ export const fromTo = (): React.ReactElement => (
18
+ <IntlProvider locale="en-US">
19
+ <FromToLocationPicker onFromClick={onFromClick} onToClick={onToClick} />
20
+ </IntlProvider>
21
+ );
22
+
23
+ export const smallTextSansSerif = (): React.ReactElement => (
24
+ <IntlProvider locale="en-US">
25
+ <span style={{ fontSize: "75%", fontFamily: "sans-serif" }}>
26
+ Plan a trip:
27
+ <FromToLocationPicker onFromClick={onFromClick} onToClick={onToClick} />
28
+ </span>
29
+ </IntlProvider>
30
+ );
31
+
32
+ export const customStyleAndText = (): React.ReactElement => (
33
+ <IntlProvider
34
+ locale="en-US"
35
+ messages={{
36
+ "otpUi.FromToLocationPicker.from": "Start here",
37
+ "otpUi.FromToLocationPicker.to": "Go here"
38
+ }}
39
+ >
40
+ <div className="trimet-ambient">
41
+ <FromToLocationPicker
42
+ showIcons={false}
43
+ onFromClick={onFromClick}
44
+ onToClick={onToClick}
45
+ />
46
+ </div>
47
+ </IntlProvider>
48
+ );
package/src/index.tsx ADDED
@@ -0,0 +1,82 @@
1
+ import flatten from "flat";
2
+ import LocationIcon from "@opentripplanner/location-icon";
3
+ import { FormattedMessage } from "react-intl";
4
+ import React from "react";
5
+
6
+ import * as S from "./styled";
7
+
8
+ // eslint-disable-next-line prettier/prettier
9
+ import type { FromToPickerProps } from "./types";
10
+
11
+ // Load the default messages.
12
+ import defaultEnglishMessages from "../i18n/en-US.yml";
13
+
14
+ const iconSize = "0.9em";
15
+
16
+ // HACK: We should flatten the messages loaded above because
17
+ // the YAML loaders behave differently between webpack and our version of jest:
18
+ // - the yaml loader for webpack returns a nested object,
19
+ // - the yaml loader for jest returns messages with flattened ids.
20
+ const defaultMessages: Record<string, string> = flatten(defaultEnglishMessages);
21
+
22
+ const FromToLocationPicker = ({
23
+ location = null,
24
+ onFromClick = null,
25
+ onToClick = null,
26
+ setLocation = null,
27
+ showIcons = true
28
+ }: FromToPickerProps): React.ReactElement => {
29
+ const handleFromClick = () => {
30
+ if (onFromClick) {
31
+ onFromClick();
32
+ return;
33
+ }
34
+ setLocation({
35
+ location,
36
+ locationType: "from",
37
+ reverseGeocode: false
38
+ });
39
+ };
40
+
41
+ const handleToClick = () => {
42
+ if (onToClick) {
43
+ onToClick();
44
+ return;
45
+ }
46
+ setLocation({
47
+ location,
48
+ locationType: "to",
49
+ reverseGeocode: false
50
+ });
51
+ };
52
+
53
+ return (
54
+ <S.FromToPickerSpan>
55
+ <S.LocationPickerSpan>
56
+ {showIcons && <LocationIcon type="from" size={iconSize} />}
57
+ <S.Button onClick={handleFromClick}>
58
+ <FormattedMessage
59
+ defaultMessage={defaultMessages["otpUi.FromToLocationPicker.from"]}
60
+ description="Text for the 'from' button of the picker"
61
+ id="otpUi.FromToLocationPicker.from"
62
+ />
63
+ </S.Button>
64
+ </S.LocationPickerSpan>
65
+ <S.LocationPickerSpan>
66
+ {showIcons && <LocationIcon type="to" size={iconSize} />}
67
+ <S.Button onClick={handleToClick}>
68
+ <FormattedMessage
69
+ defaultMessage={defaultMessages["otpUi.FromToLocationPicker.to"]}
70
+ description="Text for the 'to' button of the picker"
71
+ id="otpUi.FromToLocationPicker.to"
72
+ />
73
+ </S.Button>
74
+ </S.LocationPickerSpan>
75
+ </S.FromToPickerSpan>
76
+ );
77
+ };
78
+
79
+ export default FromToLocationPicker;
80
+
81
+ // Rename styled components for export
82
+ export { S as Styled };
File without changes
package/src/types.ts ADDED
@@ -0,0 +1,42 @@
1
+ export type Location = {
2
+ lat: number;
3
+ lon: number;
4
+ name: string;
5
+ /**
6
+ * This is only used for locations that a user has saved. Can be either:
7
+ * "home" or "work"
8
+ */
9
+ type?: string;
10
+ };
11
+
12
+ export type FromToPickerProps = {
13
+ /**
14
+ * A specific location to associate with this. This is only used when combined
15
+ * with the setLocation prop.
16
+ */
17
+ location?: Location;
18
+ /**
19
+ * Triggered when the user clicks on the "from" button.
20
+ */
21
+ onFromClick: () => void;
22
+ /**
23
+ * Triggered when the user clicks on the "to" button.
24
+ */
25
+ onToClick: () => void;
26
+ /**
27
+ * Triggered when the user clicks either the "from" or "to" button and there
28
+ * are no from/to specific handler functions defined as props.
29
+ *
30
+ * Passes an argument as follows:
31
+ * { locationType: "from/to", location, reverseGeocode: false }
32
+ */
33
+ setLocation?: ({
34
+ locationType: string,
35
+ location: Location,
36
+ reverseGeocode: boolean
37
+ }) => void;
38
+ /**
39
+ * Determines whether icons are shown on the "from" and "to" buttons.
40
+ */
41
+ showIcons?: boolean;
42
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "composite": true,
5
+ "outDir": "./lib",
6
+ "rootDir": "./src",
7
+ "skipLibCheck": true
8
+ },
9
+ "include": ["src/**/*"],
10
+ "references": [
11
+ {
12
+ "path": "../types"
13
+ }
14
+ ]
15
+ }