Following is the Form component
import React from 'react';
import { Elements } from '@helpers/ImportProxy';
import { mockApi } from '@constants/api';
type Props = {
// formValues: any
};
type MapKindToComponentT = {
[key: string]: React.SFC
}
/** @component */
export const Form: React.SFC<Props> = __props => {
const renderQuestions = () =>
mockApi.map(
(question, index): React.ReactElement | undefined => {
const mapKindToComponent: MapKindToComponentT = {
radio: Elements.RadioElement,
text: Elements.InputElement,
email: Elements.InputElement,
url: Elements.InputElement,
checkbox: Elements.CheckBoxElement,
dropdown: Elements.SelectElement,
textarea: Elements.TextareaElement,
};
if(mapKindToComponent[question.kind]) {
const Element = mapKindToComponent[question.kind];
return <Element key={index} question={question} />;
}
}
);
return (
<form>
{renderQuestions()}
<div>
<button type="submit">Submit</button>
</div>
</form>
);
};
export default Form;
Value of each key of mapKindToComponent is React functional component.
Following is the error I get for it's currently defined type. Works fine with any.
Type error: Type 'FunctionComponent' is not assignable to type 'FunctionComponent<{}>'. Types of property 'propTypes' are incompatible. Type 'WeakValidationMap | undefined' is not assignable to type 'WeakValidationMap<{}> | undefined'. Type 'WeakValidationMap' is not assignable to type 'WeakValidationMap<{}>'. Type '{}' is not assignable to type 'Props'. TS2322