UNPKG

@navinc/base-react-components

Version:
44 lines 2.15 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { jsx as _jsx } from "react/jsx-runtime"; import { useMemo } from 'react'; import styled from 'styled-components'; import { useField } from 'formik'; import Select from '../select.js'; import { states } from '../data/us-states-and-territories.js'; const getStates = (format, withTerritories) => { let list = states; if (!withTerritories) { list = states.filter((state) => !state.isTerritory); } const options = list.map((state) => ({ label: state[format], value: state.short })); return options.sort((curr, next) => { if (curr.label < next.label) { return -1; } if (curr.label > next.label) { return 1; } return 0; }); }; const StateSelect = (_a) => { var { name = 'state', label = 'State', onChange, invalidOnTouched, format = 'long', withTerritories = true } = _a, props = __rest(_a, ["name", "label", "onChange", "invalidOnTouched", "format", "withTerritories"]); const [field, meta, { setValue }] = useField({ name }); const getOptions = useMemo(() => getStates(format, withTerritories), [format, withTerritories]); return (_jsx(Select, Object.assign({ label: label, name: name, type: "text", options: getOptions, value: field.value, onChange: (e) => { setValue(e.target.value); onChange && onChange(e); }, errors: (!invalidOnTouched || meta.touched) && meta.error, isInvalid: (!invalidOnTouched || meta.touched) && !!meta.error, "data-testid": "state-input" }, props))); }; export default styled(StateSelect).withConfig({ componentId: "brc-sc-1hvtfnb" }) ``; //# sourceMappingURL=state-select.js.map