【发布时间】:2018-03-18 03:25:23
【问题描述】:
我正在尝试让 redux-form 与 TypeScript 和 styled-components 一起使用。在下面的代码示例中,为什么它不适用于样式化输入组件?输入呈现,但每次按键都失去焦点。还需要单击两次才能聚焦元素。似乎 redux-form 试图控制从 styled-components 返回的包装元素? redux-form 和 styled-components 都使用 HOC - 高阶组件将 props 传递给底层元素。
export interface ITxtProps extends WrappedFieldProps {
label?: string;
}
export const FieldHack = class extends Field<any> {};
const renderTxt = (props: ITxtProps & GenericFieldHTMLAttributes) => {
const StyledInput = styled.input`
background-color: deeppink;
`;
const notWorking = <StyledInput {...props.input} />;
const worksPerfectly = <input {...props.input} />;
return (
<div>
<div>{props.label}</div>
{notWorking}
</div>
);
}
const NormalLoginComponent = (props: {
handleSubmit?: SubmitHandler<{}, {}>;
}) => {
const { handleSubmit } = props;
return (
<form onSubmit={handleSubmit}>
{/* this does not work when using styled components: */}
<Field name={'email'} component={renderTxt} />
{/* this gives an error, property label does not exist on type... */}
{/*<Field name={'email'} component={renderTxt} label="email" />*/}
{/* this works, but no intellisense/static types */}
<FieldHack name={'email2'} component={renderTxt} label="email" />
<Field name={'password'} component={'input'} type="password" />
<Button text={'log in'} onClick={handleSubmit} />
</form>
);
};
export interface ILoginForm {
email: string;
password: string;
}
const LoginForm = reduxForm<Readonly<ILoginForm>, {}>({
form: 'loginNormal',
})(NormalLoginComponent);
【问题讨论】:
-
我也有同样的问题。 @Tomas 你有什么结果吗?
标签: typescript redux-form styled-components