2023-06-22 19:20:21 +03:00
|
|
|
import { Accessor, Component, createEffect, Setter } from "solid-js";
|
2023-05-15 20:50:53 +03:00
|
|
|
import { cityList } from "../consts";
|
|
|
|
|
|
|
|
|
|
export const SelectCity: Component<{
|
|
|
|
|
getCities: Accessor<Set<string>>,
|
|
|
|
|
setCities: Setter<Set<string>>,
|
2023-06-22 19:20:21 +03:00
|
|
|
}> = ({ getCities, setCities }) => {
|
2023-05-15 20:50:53 +03:00
|
|
|
function handleSelect(e: MouseEvent) {
|
|
|
|
|
if (!e.target) return;
|
|
|
|
|
const target = e.target as HTMLInputElement;
|
|
|
|
|
const { checked: selected, value: city } = target;
|
2023-06-22 19:20:21 +03:00
|
|
|
const selectedCities = new Set([...getCities()]);
|
|
|
|
|
if (selected) selectedCities.add(city);
|
|
|
|
|
else selectedCities.delete(city);
|
2023-05-15 20:50:53 +03:00
|
|
|
|
2023-06-22 19:20:21 +03:00
|
|
|
setCities(selectedCities);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function selectAll(e: MouseEvent) {
|
|
|
|
|
if (!e.target) return;
|
|
|
|
|
const target = e.target as HTMLInputElement;
|
|
|
|
|
const selectedCities = target.checked
|
|
|
|
|
? new Set([...cityList])
|
|
|
|
|
: new Set([]);
|
|
|
|
|
|
|
|
|
|
setCities(selectedCities);
|
2023-05-15 20:50:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2023-06-22 19:20:21 +03:00
|
|
|
<div>
|
|
|
|
|
Select all <input
|
|
|
|
|
type="checkbox"
|
|
|
|
|
onClick={selectAll}
|
|
|
|
|
/>
|
|
|
|
|
<ul>{cityList.map(city =>
|
|
|
|
|
<li>
|
2023-12-25 12:09:58 +02:00
|
|
|
<label>
|
2023-06-22 19:20:21 +03:00
|
|
|
<input
|
|
|
|
|
type="checkbox"
|
|
|
|
|
name="city"
|
|
|
|
|
value={city}
|
|
|
|
|
checked={getCities().has(city)}
|
|
|
|
|
onClick={handleSelect}
|
|
|
|
|
/>
|
|
|
|
|
{city}
|
2023-12-25 12:09:58 +02:00
|
|
|
</label>
|
2023-06-22 19:20:21 +03:00
|
|
|
</li>
|
|
|
|
|
)}
|
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
2023-05-15 20:50:53 +03:00
|
|
|
);
|
|
|
|
|
};
|