Skip to content

Instantly share code, notes, and snippets.

@nrimsky
Created April 20, 2021 22:41
Show Gist options
  • Save nrimsky/ea981bb5acdca522344ba192906a7d82 to your computer and use it in GitHub Desktop.
Save nrimsky/ea981bb5acdca522344ba192906a7d82 to your computer and use it in GitHub Desktop.
GenericInput.tsx
import React from 'react';
export function GenericInput<T>(props: { value: T, dataToString: (value: T) => string, dataFromString: (input: string) => T, onChange: (value: T) => void }) {
const isNumberField = typeof (props.value) === "number";
return (<input
value={props.dataToString(props.value)}
onChange={e => props.onChange(props.dataFromString(e.target.value))}
type={isNumberField ? "number" : "text"}
/>);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment