// Library. import {useState} from 'react' import {__} from '@wordpress/i18n' // Ours. import ControlButtons from './location-selector/ControlButtons' import LocationsList from './location-selector/LocationsList' // Types. import {LocationSet, MapLocationProps} from '../types' type Props = { locations: MapLocationProps[] onChange: (values: LocationSet) => void values: LocationSet } export default function LocationSelector(props: Props) { const { locations: propLocations, onChange, values: propValues, } = props const [allLocations, setAllLocations] = useState(new Set()), [selectedLocations, setSelectedLocations] = useState(new Set()) return (
!propValues.has(location.id))} onChange={(values: number[]) => { setAllLocations(new Set(values)) }} selected={allLocations} /> { // Setup new values. const newValues = new Set(propValues) allLocations.forEach((id) => { if (!newValues.has(id)) { newValues.add(id) } }) onChange(newValues) // Reset. setAllLocations(new Set()) }} onClickRemove={() => { // Setup new values. const newValues = new Set(propValues) selectedLocations.forEach((id) => { if (newValues.has(id)) { newValues.delete(id) } }) onChange(newValues) // Reset. setSelectedLocations(new Set()) }} /> propValues.has(location.id))} onChange={(values: number[]) => { setSelectedLocations(new Set(values)) }} selected={selectedLocations} />
) }