Last active
December 8, 2017 15:54
-
-
Save joelharkes/bc5e708c43a549be9b15fbc8021bcb08 to your computer and use it in GitHub Desktop.
Map function for the Map Class
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
// generic map function for the Map class for typescript | |
function MapMap<TKey, TValue, TReturn>(map: Map<TKey, TValue>, mapper: (key: TKey, value: TValue) => TReturn): TReturn[] { | |
var t = [] as TReturn[]; | |
map.forEach((val, key) => t.push(mapper(key, val))); | |
return t; | |
} | |
// USAGE EXAMPLE | |
interface SimpleSelectProps extends BaseFieldProps<string> { | |
options: Map<string, string>; | |
} | |
export class SimpleSelect extends React.Component<SimpleSelectProps> { | |
render() { | |
var { options } = this.props; | |
return ( | |
<select className="form-control" value={this.props.value} onChange={this.onchange}> | |
{MapMap(options, (key, value) => <option value="key">{value}</option>)} | |
</select> | |
); | |
} | |
onchange = (ev: React.ChangeEvent<HTMLSelectElement>) => { | |
this.props.onChange(ev.target.value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment