Skip to content

Instantly share code, notes, and snippets.

@joelharkes
Last active December 8, 2017 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelharkes/bc5e708c43a549be9b15fbc8021bcb08 to your computer and use it in GitHub Desktop.
Save joelharkes/bc5e708c43a549be9b15fbc8021bcb08 to your computer and use it in GitHub Desktop.
Map function for the Map Class
// 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