-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.tsx
More file actions
90 lines (71 loc) · 2.34 KB
/
input.tsx
File metadata and controls
90 lines (71 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import React, { ReactElement, ChangeEvent, InputHTMLAttributes } from "react";
import classNames from "classnames";
import { IconProp } from "@fortawesome/fontawesome-svg-core";
import Icon from "../Icon/icon";
type InputSize = "lg" | "sm";
export interface InputProps
extends Omit<InputHTMLAttributes<HTMLElement>, "size"> {
/**是否禁用 Inpnt */
disabled?: boolean;
/**设置input大小,支持的是lg或者是sm */
size?: InputSize;
/**添加图标,在右侧添加一个图标用于提示 */
icon?: IconProp;
/**标明图标位置在左边还是右边 */
iconLocal?: string;
/**添加前缀,用于配置一些固定的组合 */
prepend?: string | ReactElement;
/**添加后缀 用于配置一些固定组合 */
append?: string | ReactElement;
/**监听键盘输入,触发回调函数 */
onChange?: (e: ChangeEvent<HTMLInputElement>) => void;
}
/**
* Input 输入框 通过鼠标或键盘输入内容,是最基础的表单域的包装。
*
* ~~~js
* // 这样引用
* import { Input } from 'vikingship'
* ~~~
* 支持 HTMLInput 的所有基本属性
*/
const Input: React.FC<InputProps> = (props) => {
const { iconLocal,disabled, size, icon, prepend, append, style, ...restProps } = props;
const cname = classNames("viking-input-wrapper", {
[`input-size-${size}`]: size,
"is-disable": disabled,
"input-group": prepend || append,
"input-group-append": !!append,
"input-group-prepend": !!prepend,
});
const fixControlledValue = (value: any) => {
if (typeof value === "undefined" || value === null) {
return "";
}
return value;
};
if ("value" in props) {
delete restProps.defaultValue;
restProps.value = fixControlledValue(props.value);
}
return (
<div className={cname} style={style}>
{prepend && <div className="viking-input-group-prepend">{prepend}</div>}
{icon && (
<div className={`${"icon-wrapper"} ${iconLocal==="left"?"icon-left":''}`}>
<Icon icon={icon} title={`title-${icon}`} />
</div>
)}
<input
disabled={disabled}
className={`${"viking-input-inner"} ${iconLocal==="left"?"icon-left":''}`}
{...restProps}
/>
{append && <div className="viking-input-group-append">{append}</div>}
</div>
);
};
Input.defaultProps = {
disabled: false,
};
export default Input;