UNPKG

remix-validated-form

Version:

Form component and utils for easy form validation in remix

44 lines (43 loc) 1.32 kB
import { useCallback, useRef } from "react"; export class MultiValueMap { constructor() { this.dict = new Map(); this.add = (key, value) => { if (this.dict.has(key)) { this.dict.get(key).push(value); } else { this.dict.set(key, [value]); } }; this.delete = (key) => { this.dict.delete(key); }; this.remove = (key, value) => { if (!this.dict.has(key)) return; const array = this.dict.get(key); const index = array.indexOf(value); if (index !== -1) array.splice(index, 1); if (array.length === 0) this.dict.delete(key); }; this.getAll = (key) => { var _a; return (_a = this.dict.get(key)) !== null && _a !== void 0 ? _a : []; }; this.entries = () => this.dict.entries(); this.values = () => this.dict.values(); this.has = (key) => this.dict.has(key); } } export const useMultiValueMap = () => { const ref = useRef(null); return useCallback(() => { if (ref.current) return ref.current; ref.current = new MultiValueMap(); return ref.current; }, []); };