Skip to content

Commit a680aa4

Browse files
committed
feat: finish input interface and UI
1 parent 4a9e3a5 commit a680aa4

File tree

6 files changed

+80
-54
lines changed

6 files changed

+80
-54
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"immutability-helper": "^3.1.1",
3232
"loadsh": "^0.0.4",
3333
"monaco-editor": "^0.21.2",
34+
"omit.js": "^2.0.2",
3435
"rc-collapse": "^2.0.1",
3536
"rc-textarea": "^0.3.1",
3637
"rc-tree": "^3.10.0",

src/components/input/TextArea.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22
import RcTextArea, { TextAreaProps as RcTextAreaProps } from 'rc-textarea';
33
import { useEffect, useRef } from 'react';
4+
import omit from 'omit.js';
45

56
import { classNames, getBEMElement, getBEMModifier } from 'mo/common/className';
67
import useMergedState from 'rc-util/lib/hooks/useMergedState';
@@ -48,10 +49,12 @@ const TextArea = ({
4849

4950
const textAreaClassName = getBEMElement(inputClassName, 'textarea');
5051
const showCountClassName = getBEMModifier(textAreaClassName, 'show-count');
52+
const otherProps = omit(props, ['value']);
5153

5254
const textArea = (
5355
<RcTextArea
54-
{...props}
56+
{...otherProps}
57+
value={value}
5558
maxLength={maxLength}
5659
className={classNames(className && !showCount ? [className!] : '')}
5760
style={showCount ? {} : style}

src/components/input/input.tsx

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import './style.scss';
22
import * as React from 'react';
3-
import { classNames, prefixClaName } from 'mo/common/className';
4-
import { KeyCodes } from 'mo/common/keyCodes';
53

4+
import { classNames, prefixClaName, getBEMModifier } from 'mo/common/className';
5+
import { KeyCodes } from 'mo/common/keyCodes';
66
import TextArea from './TextArea';
77

88
type SizeType = 'normal' | 'large';
@@ -14,8 +14,9 @@ export interface InputProps {
1414
string
1515
>;
1616
placeholder?: string;
17-
value?: string;
18-
readonly defaultValue?: string;
17+
value?: any;
18+
style?: React.CSSProperties;
19+
readonly defaultValue?: any;
1920
readonly className?: string;
2021
onFocus?: (e: React.FocusEvent<HTMLInputElement>) => void;
2122
onBlur?: (e: React.FocusEvent<HTMLInputElement>) => void;
@@ -56,9 +57,9 @@ export function getInputClassName(
5657
) {
5758
return classNames(
5859
prefixCls,
59-
size === 'normal' ? [`${prefixCls}--normal`] : '',
60-
size === 'large' ? [`${prefixCls}--lg`] : '',
61-
disabled ? [`${prefixCls}--disabled`] : ''
60+
{[getBEMModifier(prefixCls, 'normal')]: size === 'normal'},
61+
{[getBEMModifier(prefixCls, 'lg')]: size === 'large'},
62+
{[getBEMModifier(prefixCls, 'disabled')]: disabled}
6263
);
6364
}
6465

@@ -124,16 +125,20 @@ class Input extends React.Component<InputProps, InputState> {
124125
};
125126

126127
render() {
128+
const { value } = this.state;
127129
const {
128130
className,
129131
size = 'normal',
130132
disabled = false,
131133
placeholder,
132134
onFocus,
133135
onBlur,
136+
style
134137
} = this.props;
135138
return (
136139
<input
140+
value={value}
141+
style={style}
137142
placeholder={placeholder}
138143
onChange={this.handleChange}
139144
onFocus={(e) => onFocus?.(e)}

src/components/input/style.scss

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ $Input: 'input';
1010
transition: all 0.3s;
1111
width: calc(100% - 16px);
1212

13+
&--lg {
14+
font-size: 16px;
15+
height: 40px;
16+
padding: 6px 11px;
17+
}
18+
1319
&__textarea {
1420
&--show-count::after {
1521
color: rgba(0, 0, 0, 0.45);

stories/components/15-Input.stories.tsx

+56-45
Original file line numberDiff line numberDiff line change
@@ -62,65 +62,76 @@ const propDefinitions = [
6262
defaultValue: '--',
6363
},
6464
];
65+
66+
const renderMultipeTextArea = () => {
67+
const [value, setValue] = useState(10);
68+
const [size, setSize] = useState({ width: 0, height: 0 });
69+
const onChange = useCallback((e) => setValue(e.target.value), [
70+
value,
71+
]);
72+
const onPressEnter = (e) => console.log(`enter key is pressed`);
73+
const onResize = useCallback(
74+
({ width, height }) => {
75+
console.log(
76+
`size is changed, width:${width} height:${height}`
77+
);
78+
setSize((resize) => ({ ...size, width, height }));
79+
},
80+
[size.width, size.height]
81+
);
82+
83+
return (
84+
<>
85+
<TextArea
86+
placeholder="Autosize height based on content lines"
87+
autoSize
88+
/>
89+
<div style={{ margin: '10px 0' }} />
90+
<TextArea
91+
placeholder="Autosize height with minimum and maximum number of lines"
92+
autoSize={{ minRows: 2, maxRows: 6 }}
93+
/>
94+
<div style={{ margin: '10px 0' }} />
95+
<TextArea
96+
value={value}
97+
onChange={onChange}
98+
onResize={onResize}
99+
onPressEnter={onPressEnter}
100+
placeholder="Controlled autosize"
101+
autoSize={{ minRows: 3, maxRows: 5 }}
102+
/>
103+
</>
104+
);
105+
};
106+
65107
stories.add(
66108
'Basic Usage',
67109
() => {
68-
const renderMultipeTextArea = () => {
69-
const [value, setValue] = useState(10);
70-
const [size, setSize] = useState({ width: 0, height: 0 });
71-
const onChange = useCallback((e) => setValue(e.target.value), [
72-
value,
73-
]);
74-
const onPressEnter = (e) => console.log(`enter key is pressed`);
75-
const onResize = useCallback(
76-
({ width, height }) => {
77-
console.log(
78-
`size is changed, width:${width} height:${height}`
79-
);
80-
setSize((resize) => ({ ...size, width, height }));
81-
},
82-
[size.width, size.height]
83-
);
84-
return (
85-
<>
86-
<TextArea
87-
placeholder="Autosize height based on content lines"
88-
autoSize
89-
/>
90-
<div style={{ margin: '10px 0' }} />
91-
<TextArea
92-
placeholder="Autosize height with minimum and maximum number of lines"
93-
autoSize={{ minRows: 2, maxRows: 6 }}
94-
/>
95-
<div style={{ margin: '10px 0' }} />
96-
<TextArea
97-
value={value}
98-
onChange={onChange}
99-
onResize={onResize}
100-
onPressEnter={onPressEnter}
101-
placeholder="Controlled autosize"
102-
autoSize={{ minRows: 3, maxRows: 5 }}
103-
/>
104-
</>
105-
);
106-
};
110+
const [inputValue, setInputValue] = useState("")
111+
const handleInputChange = useCallback((e) => setInputValue(e.target.value), [inputValue])
112+
107113
return (
108114
<>
109115
<h2>简述</h2>
110116
<p>通过鼠标或键盘输入内容,是最基础的表单域的包装。</p>
111-
<h3>使用示例 1 - Input</h3>
112-
<Input placeholder="search" />
113-
<h3>使用示例 2 - 带字数提示的文本域</h3>
117+
<h3>使用示例 1 - Input基本使用</h3>
118+
<Input placeholder="basic usage" />
119+
<h3>使用示例 2 - Input默认值</h3>
120+
<Input placeholder="input default value" defaultValue="default value"/>
121+
<h3>使用示例 3 - 输入框定义了三种尺寸(大、默认),高度分别为 40px、32px</h3>
122+
<Input size="large" placeholder="please input large size" />
123+
<Input placeholder="input default size" value={inputValue} style={{ marginTop: 10 }} onChange={handleInputChange} onPressEnter={e => console.log('enter key is pressed')}/>
124+
<h3>使用示例 4 - 带字数提示的文本域</h3>
114125
<TextArea
115126
placeholder="replace"
116127
maxLength={100}
117128
showCount={true}
118129
style={{ marginTop: 10 }}
119130
/>
120-
<h3>使用示例 3 - 用于多行输入</h3>
121-
<TextArea rows={4} placeholder="multipe line" />
131+
<h3>使用示例 5 - 用于多行输入</h3>
132+
<TextArea rows={4} placeholder="input multipe line" defaultValue="hi textarea"/>
122133
<h3>
123-
使用示例 4 - autoSize 属性适用于 textarea
134+
使用示例 6 - autoSize 属性适用于 textarea
124135
节点,并且只有高度会自动变化。另外 autoSize
125136
可以设定为一个对象,指定最小行数和最大行数。
126137
</h3>

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -9889,7 +9889,7 @@ obuf@^1.0.0, obuf@^1.1.2:
98899889
resolved "http://registry.npm.dtstack.com/obuf/-/obuf-1.1.2.tgz#09bea3343d41859ebd446292d11c9d4db619084e"
98909890
integrity sha1-Cb6jND1BhZ69RGKS0RydTbYZCE4=
98919891

9892-
omit.js@^2.0.0:
9892+
omit.js@^2.0.0, omit.js@^2.0.2:
98939893
version "2.0.2"
98949894
resolved "https://registry.npm.taobao.org/omit.js/download/omit.js-2.0.2.tgz#dd9b8436fab947a5f3ff214cb2538631e313ec2f"
98959895
integrity sha1-3ZuENvq5R6Xz/yFMslOGMeMT7C8=

0 commit comments

Comments
 (0)