如何从另一个组件自定义输入组件?

发布于 2025-02-13 05:31:20 字数 1509 浏览 0 评论 0原文

对于此输入组件,我想将其作为其他组件的基本组件。

import React, { InputHTMLAttributes } from "react";
import styled, { css } from "styled-components";

export const TextField: React.FC<InputHTMLAttributes<HTMLInputElement>> = (
  props
) => {
  return (
    <Input type="text" {...props} />
  );
};

const Input = styled.input`
  ${({ theme }) => css`
    width: 100%;
    color: ${theme.color.text};
  `}
`;

这是一种定制的样式,并添加了一些样式:

import React from "react";
import styled, { css } from "styled-components";
import { TextField } from "~/components";

export type ProductFieldProps = {
  name: string;
  value?: string;
};

type ProductFieldTypeProps = {
  textAlign: string;
};

export const ProductField: React.FC<ProductFieldProps> = ({
  name,
  value,
}) => {
  return (
    <StyledTextField textAlign="center">
      <TextField name={name} value={value} />
    </StyledTextField>
  );
};

const StyledTextField = styled(TextField)`
  ${({ theme }) => css<ProductFieldTypeProps>`
    &:hover {
      border-color: ${theme.color.hover};
    }
  `}
`;

使用新的组件productfield时,

# pages/index.tsx
import { ProductField } from "~/components";

...
return {
  <>
    <ProductField name="aaa" value="bbb" />
  </>
}

我遇到了一个服务器错误:

错误:输入是一个void元素标签,不得有richy也不使用dangesslySetInnerhtml

如何正确设置?

For this input component, I want to make it as a basic component for other components.

import React, { InputHTMLAttributes } from "react";
import styled, { css } from "styled-components";

export const TextField: React.FC<InputHTMLAttributes<HTMLInputElement>> = (
  props
) => {
  return (
    <Input type="text" {...props} />
  );
};

const Input = styled.input`
  ${({ theme }) => css`
    width: 100%;
    color: ${theme.color.text};
  `}
`;

This is a customized one and add some styles:

import React from "react";
import styled, { css } from "styled-components";
import { TextField } from "~/components";

export type ProductFieldProps = {
  name: string;
  value?: string;
};

type ProductFieldTypeProps = {
  textAlign: string;
};

export const ProductField: React.FC<ProductFieldProps> = ({
  name,
  value,
}) => {
  return (
    <StyledTextField textAlign="center">
      <TextField name={name} value={value} />
    </StyledTextField>
  );
};

const StyledTextField = styled(TextField)`
  ${({ theme }) => css<ProductFieldTypeProps>`
    &:hover {
      border-color: ${theme.color.hover};
    }
  `}
`;

When use the new component ProductField,

# pages/index.tsx
import { ProductField } from "~/components";

...
return {
  <>
    <ProductField name="aaa" value="bbb" />
  </>
}

I got a server error:

Error: input is a void element tag and must neither have children nor use dangerouslySetInnerHTML.

How to set correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青丝拂面 2025-02-20 05:31:20

您正在使用样式组件为输入组件设计样式。

您可以在这条线上看到您正在将样式应用于textfield component:

const StyledTextField = styled(TextField)

而在新的productfield component 您导入的组件,样式版本和原始版本和原始版本和nest他们彼此之间:

    <StyledTextField textAlign="center">
      <TextField name={name} value={value} />
    </StyledTextField>

这本质上意味着您将两个输入字段彼此嵌套,这是不允许的,因此错误:

错误:输入是一个无效元素标签,不得有孩子也不使用dangesslysetinnerhtml。

一种方法是简单地删除嵌套textfield这样的方法,然后将所有道具应用于样式的一个:

<StyledTextField textAlign="center" name={name} value={value} />

另一个解决方案是直接导出并使用样式版本,因为附加包装器似乎多余。

更多信息有关如何使用样式组件的定制组件样式

You are using styled-components to style your Input component.

You can see on this line that you are applying styles to the TextField component:

const StyledTextField = styled(TextField)

And yet in the new ProductField component you import both, the styled version and the original one and nest them in each other:

    <StyledTextField textAlign="center">
      <TextField name={name} value={value} />
    </StyledTextField>

This essentially means you are nesting two input field into each other, which is not allowed and hence the error:

Error: input is a void element tag and must neither have children nor use dangerouslySetInnerHTML.

One way to approach this is to simply remove the nested TextField like so, and apply all the props to the styled one instead:

<StyledTextField textAlign="center" name={name} value={value} />

Another solution would be to just export and use the styled version directly because the additional wrapper seems redundant.

More info on how to style custom components with styled-components

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文