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

feat(trip-form): Advanced Mode Settings Panel #24

Closed
wants to merge 3 commits into from
Closed
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
8 changes: 5 additions & 3 deletions packages/trip-form/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@
"types": "lib/index.d.ts",
"private": false,
"dependencies": {
"@opentripplanner/core-utils": "^11.3.1",
"@floating-ui/react": "^0.19.2",
"@opentripplanner/core-utils": "^11.3.1",
"@opentripplanner/building-blocks": "^1.0.3",
"@styled-icons/bootstrap": "^10.34.0",
"@styled-icons/boxicons-regular": "^10.38.0",
"@styled-icons/fa-regular": "^10.37.0",
"@styled-icons/fa-solid": "^10.37.0",
"date-fns": "^2.28.0",
"flat": "^5.0.2",
"react-animate-height": "^3.2.3",
"react-indiana-drag-scroll": "^2.0.1",
"react-inlinesvg": "^2.3.0"
},
"devDependencies": {
"@types/flat": "^5.0.2",
"@opentripplanner/types": "6.5.0"
"@opentripplanner/types": "6.5.0",
"@types/flat": "^5.0.2"
},
"peerDependencies": {
"react": "^16.14.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import React, { ReactElement, useState } from "react";
import { ModeButtonDefinition } from "@opentripplanner/types";
import * as Core from "..";
import { QueryParamChangeEvent } from "../types";
import {
addSettingsToButton,
extractModeSettingDefaultsToObject,
populateSettingWithValue,
setModeButtonEnabled
} from "./utils";
import {
defaultModeButtonDefinitions,
getIcon,
modeSettingDefinitionsWithDropdown
} from "./mockButtons.story";

const initialState = {
enabledModeButtons: ["transit"],
modeSettingValues: {}
};

function pipe<T>(...fns: Array<(arg: T) => T>) {
return (value: T) => fns.reduce((acc, fn) => fn(acc), value);
}

const MetroModeSubsettingsComponent = ({
fillModeIcons,
modeButtonDefinitions,
onSetModeSettingValue,
onToggleModeButton
}: {
fillModeIcons?: boolean;
modeButtonDefinitions: Array<ModeButtonDefinition>;
onSetModeSettingValue: (event: QueryParamChangeEvent) => void;
onToggleModeButton: (key: string, newState: boolean) => void;
}): ReactElement => {
const [modeSettingValues, setModeSettingValues] = useState({});
const modeSettingValuesWithDefaults = {
...extractModeSettingDefaultsToObject(modeSettingDefinitionsWithDropdown),
...initialState.modeSettingValues,
...modeSettingValues
};

const [activeModeButtonKeys, setModeButtonKeys] = useState(
initialState.enabledModeButtons
);

const addIconToModeSetting = msd => ({
...msd,
icon: getIcon(msd.iconName)
});

const processedModeSettings = modeSettingDefinitionsWithDropdown.map(
pipe(
addIconToModeSetting,
populateSettingWithValue(modeSettingValuesWithDefaults)
)
);

const processedModeButtons = modeButtonDefinitions.map(
pipe(
addSettingsToButton(processedModeSettings),
setModeButtonEnabled(activeModeButtonKeys)
)
);

const toggleModeButtonAction = (key: string, newState: boolean) => {
if (newState) {
setModeButtonKeys([...activeModeButtonKeys, key]);
} else {
setModeButtonKeys(activeModeButtonKeys.filter(button => button !== key));
}
// Storybook Action:
onToggleModeButton(key, newState);
};

const setModeSettingValueAction = (event: QueryParamChangeEvent) => {
setModeSettingValues({ ...modeSettingValues, ...event });
// Storybook Action:
onSetModeSettingValue(event);
};

return (
<Core.AdvancedModeSubsettingsContainer
fillModeIcons={fillModeIcons}
label="Select a transit mode"
modeButtons={processedModeButtons}
onSettingsUpdate={setModeSettingValueAction}
onToggleModeButton={toggleModeButtonAction}
/>
);
};

const Template = (args: {
fillModeIcons?: boolean;
onSetModeSettingValue: (event: QueryParamChangeEvent) => void;
onToggleModeButton: (key: string, newState: boolean) => void;
}): ReactElement => (
<MetroModeSubsettingsComponent
modeButtonDefinitions={defaultModeButtonDefinitions}
// eslint-disable-next-line react/jsx-props-no-spreading
{...args}
/>
);

export const AdvancedModeSettingsButtons = Template.bind({});

export default {
argTypes: {
fillModeIcons: { control: "boolean" },
onSetModeSettingValue: { action: "set mode setting value" },
onToggleModeButton: { action: "toggle button" }
},
component: MetroModeSubsettingsComponent,
title: "Trip Form Components/Advanced Mode Settings Buttons"
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import React from "react";
import AnimateHeight from "react-animate-height";
import styled from "styled-components";
import colors from "@opentripplanner/building-blocks";
import { Check2 } from "@styled-icons/bootstrap";
import { ModeButtonDefinition } from "@opentripplanner/types";
import { useIntl } from "react-intl";
import SubSettingsPane from "../SubSettingsPane";
import generateModeButtonLabel from "../i18n";
import { invisibleCss } from "..";
import { QueryParamChangeEvent } from "../../types";

const { blue, grey } = colors;

const SettingsContainer = styled.div`
width: 100%;
max-width: 500px;
`;

const StyledModeSettingsButton = styled.div<{
accentColor: string;
fillModeIcons: boolean;
subsettings: boolean;
}>`
& > input {
${invisibleCss}
}
& > label {
align-items: center;
background-color: #fff;
border: 1px solid ${props => props.accentColor};
border-left-width: 2px;
border-right-width: 2px;
color: ${props => props.accentColor};
display: grid;
gap: 20px;
grid-template-columns: 40px auto 40px;
height: 51px;
justify-items: center;
padding: 0 10px;
}

& > input:checked + label {
background-color: ${props => props.accentColor};
color: #fff;
border-bottom-left-radius: ${props => props.subsettings && 0} !important;
border-bottom-right-radius: ${props => props.subsettings && 0} !important;
}

span {
justify-self: flex-start;
}

svg {
height: 26px;
width: 26px;
fill: ${props =>
props.fillModeIcons === false ? "inherit" : "currentcolor"};
}

&:hover {
cursor: pointer;
}
`;

const StyledSettingsContainer = styled.div`
border: 1px solid ${grey[300]};
border-top: 0;
padding: 1em;
`;

interface Props {
accentColor?: string;
fillModeIcons: boolean;
id: string;
modeButton: ModeButtonDefinition;
onSettingsUpdate: (event: QueryParamChangeEvent) => void;
onToggle: () => void;
}

const AdvancedModeSettingsButton = ({
accentColor = blue[700],
fillModeIcons,
id,
modeButton,
onSettingsUpdate,
onToggle
}: Props): JSX.Element => {
const intl = useIntl();
const label = generateModeButtonLabel(modeButton.key, intl, modeButton.label);
const checkboxId = `metro-submode-selector-mode-${id}`;
return (
<SettingsContainer className="advanced-submode-container">
<StyledModeSettingsButton
accentColor={accentColor}
fillModeIcons={fillModeIcons}
id={modeButton.key}
subsettings={modeButton.modeSettings.length > 0}
>
<input
aria-label={label}
checked={modeButton.enabled ?? undefined}
id={checkboxId}
onChange={onToggle}
type="checkbox"
/>
<label htmlFor={checkboxId}>
<modeButton.Icon />
<span>{modeButton?.label}</span>
{modeButton.enabled && <Check2 />}
</label>
</StyledModeSettingsButton>
{modeButton.modeSettings.length > 0 && (
<AnimateHeight duration={500} height={modeButton.enabled ? "auto" : 0}>
<StyledSettingsContainer>
<SubSettingsPane
onSettingUpdate={onSettingsUpdate}
modeButton={modeButton}
/>
</StyledSettingsContainer>
</AnimateHeight>
)}
</SettingsContainer>
);
};

export default AdvancedModeSettingsButton;
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import styled from "styled-components";
import React, { useCallback } from "react";
import { ModeButtonDefinition } from "@opentripplanner/types";
import AdvancedModeSettingsButton from "./AdvancedModeSettingsButton";
import { invisibleCss } from ".";
import { QueryParamChangeEvent } from "../types";

const SubsettingsContainer = styled.fieldset`
border: none;
margin: 0;
width: 90%;

legend {
${invisibleCss}
}

display: flex;
flex-direction: column;

div:first-of-type div label {
border-top-width: 2px;
border-radius: 8px 8px 0 0;
}

div:last-of-type div label {
border-bottom-width: 2px;
border-radius: 0 0 8px 8px;
}

div.advanced-submode-container:last-of-type div:last-child {
border-radius: 0 0 8px 8px;
}
`;

interface Props {
fillModeIcons: boolean | undefined;
label: string;
modeButtons: ModeButtonDefinition[];
onSettingsUpdate: (event: QueryParamChangeEvent) => void;
onToggleModeButton: (key: string, newState: boolean) => void;
}

const AdvancedModeSubsettingsContainer = ({
fillModeIcons,
modeButtons,
label,
onSettingsUpdate,
onToggleModeButton
}: Props): JSX.Element => {
return (
<SubsettingsContainer>
<legend>{label}</legend>
{modeButtons.map(button => {
return (
<AdvancedModeSettingsButton
fillModeIcons={fillModeIcons}
key={button.label}
modeButton={button}
onSettingsUpdate={onSettingsUpdate}
onToggle={useCallback(() => {
onToggleModeButton(button.key, !button.enabled);
}, [button, onToggleModeButton])}
id={button.key}
/>
);
})}
</SubsettingsContainer>
);
};

export default AdvancedModeSubsettingsContainer;
Loading
Loading