All files / presentation/ui/src DeparturePicker.tsx

100% Statements 5/5
100% Branches 11/11
100% Functions 3/3
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                                                                            65x 65x                       81x 81x             4x                                                      
import { Accordion, ListGroup, ListItem } from "@wintermuted/ui-react";
import React from "react";
import FontAwesome from "react-fontawesome";
import { useTimeDisplay } from "./TimeDisplay";
import TimeDiffBadge from "./TimeDiffBadge";
import "./DeparturePicker.scss";
 
export interface DepartureOption {
  id: string;
  estimated: number;
  scheduled: number;
}
 
export interface DeparturePickerProps<
  T extends DepartureOption = DepartureOption,
> {
  departures: readonly T[];
  selectedId?: string;
  onSelect: (departure: T) => void;
  title?: React.ReactNode;
  emptyContent?: React.ReactNode;
  status?: React.ReactNode;
  disabled?: boolean;
}
 
export function DeparturePicker<T extends DepartureOption>({
  departures,
  selectedId,
  onSelect,
  title = (
    <>
      <FontAwesome name="clock-o" aria-hidden="true" /> Departures
    </>
  ),
  emptyContent = <p role="status">No departures available.</p>,
  status,
  disabled = false,
}: DeparturePickerProps<T>) {
  const { formatTime } = useTimeDisplay();
  return (
    <Accordion
      open
      className="transit-stop-panel ow-departure-picker"
      title={title}
    >
      {status}
      {!departures.length ? (
        emptyContent
      ) : (
        <ListGroup>
          {departures.map((departure) => {
            const selected = departure.id === selectedId;
            return (
              <ListItem key={departure.id} as="li" style={{ padding: 0 }}>
                <button
                  type="button"
                  className="transit-departure-option"
                  aria-pressed={selected}
                  disabled={disabled}
                  onClick={() => onSelect(departure)}
                >
                  <span className="transit-departure-label">
                    <span
                      className="transit-departure-caret"
                      aria-hidden="true"
                    >
                      {selected && <FontAwesome name="caret-right" />}
                    </span>
                    <TimeDiffBadge
                      estimatedArrivalTime={departure.estimated}
                      scheduledArrivalTime={departure.scheduled}
                    />
                  </span>
                  <small>
                    {departure.estimated ? "Estimated" : "Scheduled"}:{" "}
                    {formatTime(departure.estimated || departure.scheduled)}
                  </small>
                </button>
              </ListItem>
            );
          })}
        </ListGroup>
      )}
    </Accordion>
  );
}