Created
April 20, 2021 22:41
-
-
Save nrimsky/ea981bb5acdca522344ba192906a7d82 to your computer and use it in GitHub Desktop.
GenericInput.tsx
This file contains hidden or 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
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