Last active
March 6, 2019 15:50
-
-
Save niekvandepas/fefddd978451926c4239d7ea44d9ae46 to your computer and use it in GitHub Desktop.
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 * as React from 'react'; | |
type InputType = 'text' | 'tel' | 'email' | 'password' | 'number'; | |
type Input = 'textarea' | 'button' | InputType; | |
const button = () => <button />; | |
const textarea = () => <textarea />; | |
const other = (type: InputType) => React.createElement('input', { type }); | |
const matchInput = (inputString: Input) => { | |
switch (inputString) { | |
case 'textarea': | |
return textarea(); | |
case 'button': | |
return button(); | |
default: | |
return other(inputString); | |
} | |
}; | |
export interface InputSpec { | |
type: Input; | |
className?: string; | |
defaultValue?: string; | |
placeholder?: string; | |
} | |
interface IProps { | |
elements: InputSpec[]; | |
} | |
const GenericForm = ({ elements }: IProps) => ( | |
<form className="scheduleitem-form"> | |
{ | |
elements.map((element, i) => { | |
return ( | |
<div key={i} className="input-wrapper"> | |
{ | |
React.createElement(element.type, {value: element.defaultValue || '', className: element.className || '', placeholder: element.placeholder || ''}) | |
} | |
</div> | |
) | |
}) | |
} | |
</form> | |
); | |
export default GenericForm; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment