Skip to content

Instantly share code, notes, and snippets.

@niekvandepas
Last active March 6, 2019 15:50
Show Gist options
  • Save niekvandepas/fefddd978451926c4239d7ea44d9ae46 to your computer and use it in GitHub Desktop.
Save niekvandepas/fefddd978451926c4239d7ea44d9ae46 to your computer and use it in GitHub Desktop.
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