-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert us-state-choices.json to TSX. (#1265)
This uses the commondatabuilder (introduced in #501) to convert our raw JSON state choices into a TypeScript file, which will allow us to do more dynamic things with them, such as using the module's exported `getUSStateChoiceLabels()` function to easily look up a state's full name from its two-letter abbreviation.
- Loading branch information
Showing
3 changed files
with
145 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// This file was auto-generated by commondatabuilder. | ||
// Please don't edit it. | ||
|
||
export type USStateChoice = "AK"|"AL"|"AS"|"AZ"|"AR"|"CA"|"CO"|"CT"|"DE"|"DC"|"FL"|"GA"|"GU"|"HI"|"ID"|"IL"|"IN"|"IA"|"KS"|"KY"|"LA"|"ME"|"MD"|"MA"|"MI"|"MN"|"MP"|"MS"|"MO"|"MT"|"NE"|"NV"|"NH"|"NJ"|"NM"|"NY"|"NC"|"ND"|"OH"|"OK"|"OR"|"PA"|"PR"|"RI"|"SC"|"SD"|"TN"|"TX"|"UT"|"VT"|"VA"|"VI"|"WA"|"WV"|"WI"|"WY"; | ||
|
||
export const USStateChoices: USStateChoice[] = [ | ||
"AK", | ||
"AL", | ||
"AS", | ||
"AZ", | ||
"AR", | ||
"CA", | ||
"CO", | ||
"CT", | ||
"DE", | ||
"DC", | ||
"FL", | ||
"GA", | ||
"GU", | ||
"HI", | ||
"ID", | ||
"IL", | ||
"IN", | ||
"IA", | ||
"KS", | ||
"KY", | ||
"LA", | ||
"ME", | ||
"MD", | ||
"MA", | ||
"MI", | ||
"MN", | ||
"MP", | ||
"MS", | ||
"MO", | ||
"MT", | ||
"NE", | ||
"NV", | ||
"NH", | ||
"NJ", | ||
"NM", | ||
"NY", | ||
"NC", | ||
"ND", | ||
"OH", | ||
"OK", | ||
"OR", | ||
"PA", | ||
"PR", | ||
"RI", | ||
"SC", | ||
"SD", | ||
"TN", | ||
"TX", | ||
"UT", | ||
"VT", | ||
"VA", | ||
"VI", | ||
"WA", | ||
"WV", | ||
"WI", | ||
"WY" | ||
]; | ||
|
||
const USStateChoiceSet: Set<String> = new Set(USStateChoices); | ||
|
||
export function isUSStateChoice(choice: string): choice is USStateChoice { | ||
return USStateChoiceSet.has(choice); | ||
} | ||
|
||
export type USStateChoiceLabels = { | ||
[k in USStateChoice]: string; | ||
}; | ||
|
||
export function getUSStateChoiceLabels(): USStateChoiceLabels { | ||
return { | ||
AK: "Alaska", | ||
AL: "Alabama", | ||
AS: "American Samoa", | ||
AZ: "Arizona", | ||
AR: "Arkansas", | ||
CA: "California", | ||
CO: "Colorado", | ||
CT: "Connecticut", | ||
DE: "Delaware", | ||
DC: "District of Columbia", | ||
FL: "Florida", | ||
GA: "Georgia", | ||
GU: "Guam", | ||
HI: "Hawaii", | ||
ID: "Idaho", | ||
IL: "Illinois", | ||
IN: "Indiana", | ||
IA: "Iowa", | ||
KS: "Kansas", | ||
KY: "Kentucky", | ||
LA: "Louisiana", | ||
ME: "Maine", | ||
MD: "Maryland", | ||
MA: "Massachusetts", | ||
MI: "Michigan", | ||
MN: "Minnesota", | ||
MP: "Northern Mariana Islands", | ||
MS: "Mississippi", | ||
MO: "Missouri", | ||
MT: "Montana", | ||
NE: "Nebraska", | ||
NV: "Nevada", | ||
NH: "New Hampshire", | ||
NJ: "New Jersey", | ||
NM: "New Mexico", | ||
NY: "New York", | ||
NC: "North Carolina", | ||
ND: "North Dakota", | ||
OH: "Ohio", | ||
OK: "Oklahoma", | ||
OR: "Oregon", | ||
PA: "Pennsylvania", | ||
PR: "Puerto Rico", | ||
RI: "Rhode Island", | ||
SC: "South Carolina", | ||
SD: "South Dakota", | ||
TN: "Tennessee", | ||
TX: "Texas", | ||
UT: "Utah", | ||
VT: "Vermont", | ||
VA: "Virginia", | ||
VI: "Virgin Islands", | ||
WA: "Washington", | ||
WV: "West Virginia", | ||
WI: "Wisconsin", | ||
WY: "Wyoming", | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
import React from "react"; | ||
import { SelectFormField, ChoiceFormFieldProps } from "./form-fields"; | ||
import US_STATE_CHOICES from "../../../common-data/us-state-choices.json"; | ||
import { DjangoChoices } from "../common-data"; | ||
import { toDjangoChoices } from "../common-data"; | ||
import { | ||
USStateChoices, | ||
getUSStateChoiceLabels, | ||
} from "../../../common-data/us-state-choices"; | ||
|
||
export const USStateFormField: React.FC<Omit< | ||
ChoiceFormFieldProps, | ||
"label" | "choices" | ||
>> = (props) => ( | ||
<SelectFormField | ||
{...props} | ||
choices={US_STATE_CHOICES as DjangoChoices} | ||
choices={toDjangoChoices(USStateChoices, getUSStateChoiceLabels())} | ||
label="State" | ||
/> | ||
); |