【问题标题】:Input types in Typescript interfaceTypescript 接口中的输入类型
【发布时间】:2020-04-17 15:50:33
【问题描述】:

我正在尝试编写一个要重用的组件,其中一个变量是要呈现的输入类型。

目前,我有这个:

type InputBaseProps = {
  children?: ReactChild;
  className?: string;
  id: string;
  label?: string;
  name?: string;
  onBlur: (event: ChangeEvent<HTMLInputElement>) => void;
  onChange: (event: ChangeEvent<HTMLInputElement>) => void;
  placeholder?: string;
  style?: CSSProperties;
  type: ????;
  value?: string;
};

我只想允许有效的 HTML types,就像这里定义的整个列表 https://www.w3schools.com/tags/att_input_type.asp

有什么简单的方法吗?

【问题讨论】:

    标签: javascript html typescript


    【解决方案1】:

    是的,这很简单,您只需输入 |在你想要的所有类型之间。例如它会是这样的

    “按钮”| “复选框” | “颜色” | “日期” | “本地日期时间” | “电子邮件” | “文件” | “隐藏” | “形象” | “月” | "数字" | “密码” | “收音机” | “范围” | “重置” | "搜索" | “提交” | “电话” | “文字” | “时间” | “网址” | “周”

    这些都是从您列出的 w3schools 页面中获取的

    如果这符合您的需要,请接受它

    【讨论】:

    • 谢谢!它按我的预期工作。我自己找到了这个解决方案,只是感觉有点冗长。我认为有一种更简单的方法。
    • 您自己的解决方案是什么?我很想知道
    • 刚刚使用了像您这样的解决方案。我认为这应该是一个更好的解决方案,而不是手动编写每个选项或复制粘贴。
    • 我刚刚写了一个快速的 javascript 程序来做这件事,可能比复制粘贴要花更长的时间,但是¯_(ツ)_/¯
    • @herohamp intellisense 是否适用于您的方法?
    【解决方案2】:
    import { HTMLInputTypeAttribute } from "react";
    
    type InputBaseProps = {
        type?: HTMLInputTypeAttribute 
    }
    

    HTMLInputTypeAttribute 解析为 ff:

    type HTMLInputTypeAttribute = "number" | "search" | "button" | "time" | "image" | "text" | "checkbox" | "color" | "date" | "datetime-local" | "email" | "file" | "hidden" | "month" | "password" | "radio" | "range" | ... 5 more ... | (string & {})
    

    智能感知工作正常。

    【讨论】:

      【解决方案3】:

      创建一个可导出的枚举...

      export enum InputType {
          button = "button",
          checkbox = "checkbox",
          color = "color",
          date = "date",
          datetimelocal = "datetime-local",
          email = "email",
          file = "file",
          hidden = "hidden",
          image = "image",
          month = "month",
          number = "number",
          password = "password",
          radio = "radio",
          range = "range",
         reset = "reset",
          search = "search",
          submit = "submit",
          tel = "tel",
          text = "text",
          time = "time",
          url = "url",
          week = "week"
      };
      

      那你可以做...

      type InputBaseProps = {
        children?: ReactChild;
        className?: string;
        id: string;
        label?: string;
        name?: string;
        onBlur: (event: ChangeEvent<HTMLInputElement>) => void;
        onChange: (event: ChangeEvent<HTMLInputElement>) => void;
        placeholder?: string;
        style?: CSSProperties;
        type: String as () => InputType;
        value?: string;
      };
      

      然后使用它...

      import {InputType} from './file-your-enum-is-in';
      const el: InputBaseProps  = {
          ...
          type: InputType.text,
          ...
      };
      

      【讨论】:

        猜你喜欢
        • 2020-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-06
        • 2021-07-31
        • 2017-11-16
        • 2018-09-15
        • 2019-04-27
        相关资源
        最近更新 更多