import React, { Component } from "react"; import Form from '@cloudscape-design/components/form'; import SpaceBetween from '@cloudscape-design/components/space-between'; import Button from '@cloudscape-design/components/button'; import FormField from '@cloudscape-design/components/form-field'; import Autosuggest from '@cloudscape-design/components/autosuggest'; import DatePicker from '@cloudscape-design/components/date-picker'; import Checkbox from '@cloudscape-design/components/checkbox'; import { SearchApi } from "../services/search-api.ts"; export interface SearchProps { Origin: string; Destination: string; OutboundDate: string; InboundDate?: string; WheelchairAvailability?: boolean; StationStatus?: string; } export class SearchForm extends Component { private readonly searchApi: SearchApi; private readonly successHandler: Function; public readonly state: SearchProps = { Origin: '', Destination: '', OutboundDate: '', StationStatus: 'finished' } public stations: any[] = []; constructor (props: any) { super(props); this.successHandler = props.onsubmit; this.stations = props.stations; } async setValue (prop: string, value: any) { this.setState({ [prop]: value }) } submitSearchRequest (event: any) { this.successHandler({ ...this.state }); event.preventDefault(); } render () { return (
this.submitSearchRequest(e)}> } > {/* Origin */} { parseInt(this.state.Origin) == this.state.Origin as any ? (
{/* An origin has been selected */} From {this.stations.find(s => s.stop_id === this.state.Origin)!.stop_name} ({this.stations.find(s => s.stop_id === this.state.Origin)!.stop_id})
) : (
{/* No origin selected */} this.setValue('Origin', detail.value)} value={this.state.Origin} options={this.stations.filter(station => station.stop_name.match(new RegExp(this.state.Origin), 'i')).map(station => ({ label: station.stop_name, value: station.stop_id }))} enteredTextLabel={value => `Filter: "${value}"`} placeholder="Filter stations" empty={!this.state.Origin.length ? 'Start typing to filter stations' : 'No station found'} statusType={this.state.StationStatus as any} />
) } {/* Destination */} { parseInt(this.state.Destination) == this.state.Destination as any ? (
{/* A destination has been selected */} To {this.stations.find(s => s.stop_id === this.state.Destination)!.stop_name} ({this.stations.find(s => s.stop_id === this.state.Destination)!.stop_id})
) : (
this.setValue('Destination', detail.value)} value={this.state.Destination} options={this.stations.filter(station => station.stop_name.match(new RegExp(this.state.Destination), 'i')).map(station => ({ label: station.stop_name, value: station.stop_id }))} enteredTextLabel={value => `Filter: "${value}"`} placeholder="Filter stations" empty={!this.state.Destination.length ? 'Start typing to filter stations' : 'No station found'} statusType={this.state.StationStatus as any} />
) } {/* Outbound date */} this.setValue('OutboundDate', detail.value)} value={this.state.OutboundDate} openCalendarAriaLabel={selectedDate => "Choose Date" + (selectedDate ? `, selected date is ${selectedDate}` : "") } nextMonthAriaLabel="Next month" placeholder="YYYY/MM/DD" previousMonthAriaLabel="Previous month" todayAriaLabel="Today" /> {/* Inbound date */} this.setValue('InboundDate', detail.value)} value={this.state.InboundDate || ''} openCalendarAriaLabel={selectedDate => "Choose Date" + (selectedDate ? `, selected date is ${selectedDate}` : "") } nextMonthAriaLabel="Next month" placeholder="YYYY/MM/DD" previousMonthAriaLabel="Previous month" todayAriaLabel="Today" /> {/* Wheelchair availability */} this.setValue('WheelchairAvailability', detail.checked) } checked={!!this.state.WheelchairAvailability} > Wheelchair seating available
) } }