Skip to content

Commit 2e88449

Browse files
committed
remove Inputs state
1 parent 9da43b6 commit 2e88449

File tree

8 files changed

+157
-157
lines changed

8 files changed

+157
-157
lines changed

src/Datepicker/Datetime.js

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ import { ANGLE_LEFT, ANGLE_RIGHT, ANGLE_LEFT_DOUBLE, ANGLE_RIGHT_DOUBLE } from '
1313
import Transition from '../Transition';
1414
import PropTypes from '../utils/proptypes';
1515

16-
import Styles from '../styles/_datepicker.scss';
17-
import InputStyles from '../styles/_input.scss';
18-
import FormStyles from '../styles/_form.scss';
16+
import _datepickers from '../styles/_datepicker.scss';
17+
import _inputs from '../styles/_input.scss';
1918

2019
import { getLang, setLang } from '../lang';
2120
setLang('datetime');
@@ -201,7 +200,7 @@ class Datetime extends Component {
201200
const { current, timeStage } = this.state;
202201

203202
return (
204-
<div className={Styles.timeContainer}>
203+
<div className={_datepickers.timeContainer}>
205204
<Clock current={current} active={!!timeStage}
206205
stage={timeStage}
207206
onClose={this.props.type === TIME ? undefined : this.timeStageChange}
@@ -268,7 +267,7 @@ class Datetime extends Component {
268267
}
269268

270269
let buttons = [];
271-
const className = Styles.year;
270+
const className = _datepickers.year;
272271

273272
buttons.push(
274273
<a className={className} key={'prev'}
@@ -299,7 +298,7 @@ class Datetime extends Component {
299298
return (
300299
<a type="button"
301300
onClick={this.monthChange.bind(this, i)}
302-
key={i} className={Styles.month}>{m}</a>
301+
key={i} className={_datepickers.month}>{m}</a>
303302
);
304303
}, this);
305304
}
@@ -339,11 +338,11 @@ class Datetime extends Component {
339338
}
340339

341340
let className = classnames(
342-
Styles.day,
343-
d.getMonth() !== month && Styles.gray,
344-
disabled && Styles.disabled,
345-
(isToday && today.getDate() === d.getDate() && today.getMonth() === d.getMonth()) && Styles.today,
346-
(isCurrent && value.getDate() === d.getDate() && value.getMonth() === d.getMonth()) && Styles.active
341+
_datepickers.day,
342+
d.getMonth() !== month && _datepickers.gray,
343+
disabled && _datepickers.disabled,
344+
(isToday && today.getDate() === d.getDate() && today.getMonth() === d.getMonth()) && _datepickers.today,
345+
(isCurrent && value.getDate() === d.getDate() && value.getMonth() === d.getMonth()) && _datepickers.active
347346
);
348347

349348
return (
@@ -372,7 +371,7 @@ class Datetime extends Component {
372371
let display = stage === 'day' ? 'inline-block' : 'none';
373372

374373
return (
375-
<div style={this.props.style} className={Styles.header}>
374+
<div style={this.props.style} className={_datepickers.header}>
376375
<i style={{float: 'left', display}} onClick={this.pre.bind(this, 'month')}>
377376
{ANGLE_LEFT_DOUBLE}
378377
</i>
@@ -399,15 +398,15 @@ class Datetime extends Component {
399398
switch (this.state.stage) {
400399
case 'day':
401400
let weeks = getLang('datetime.weekday').map(function (w, i) {
402-
return <div key={i} className={Styles.week}>{w}</div>;
401+
return <div key={i} className={_datepickers.week}>{w}</div>;
403402
});
404-
return <div className={Styles.dayInner}>{weeks}{this.renderDays()}</div>;
403+
return <div className={_datepickers.dayInner}>{weeks}{this.renderDays()}</div>;
405404
case 'month':
406-
return <div className={Styles.monthInner}>{this.renderMonths()}</div>;
405+
return <div className={_datepickers.monthInner}>{this.renderMonths()}</div>;
407406
case 'year':
408-
return <div className={Styles.yearInner}>{this.renderYears()}</div>;
407+
return <div className={_datepickers.yearInner}>{this.renderYears()}</div>;
409408
case 'clock':
410-
return <div className={Styles.inner}></div>;
409+
return <div className={_datepickers.inner}></div>;
411410
}
412411
return null;
413412
}
@@ -418,25 +417,25 @@ class Datetime extends Component {
418417

419418
let className = classnames(
420419
this.props.className,
421-
Styles.datepicker,
422-
type !== DATETIME && Styles.short,
423-
readOnly ? InputStyles.disabled : (open && Styles.open),
424-
type === TIME && Styles.timepicker,
425-
dropup && Styles.dropup
420+
_datepickers.datepicker,
421+
type !== DATETIME && _datepickers.short,
422+
readOnly ? _inputs.disabled : (open && _datepickers.open),
423+
type === TIME && _datepickers.timepicker,
424+
dropup && _datepickers.dropup
426425
);
427426

428427
let text = value ? this.formatValue(value) : '';
429428

430429
return (
431430
<div ref="datetime" onClick={this.handleOpen} className={className}>
432-
<div className={classnames(Styles.control, InputStyles.input, hasError && FormStyles.dangerInput)}>
431+
<div className={classnames(_datepickers.control, _inputs.input, hasError && _inputs.dangerInput)}>
433432
{
434433
text ? <span>{text}</span>
435-
: <span className={InputStyles.placeholder}>{placeholder}&nbsp;</span>
434+
: <span className={_inputs.placeholder}>{placeholder}&nbsp;</span>
436435
}
437436
</div>
438437
<Transition ref="datepicker" duration={333} tf="ease-out" act={open ? 'enter' : 'leave'}>
439-
<div className={Styles.picker}>
438+
<div className={_datepickers.picker}>
440439
{this.renderHeader()}
441440
{this.renderInner()}
442441
{(stage === 'day' || stage === 'clock') && type !== DATE && this.renderTime()}

src/Input.js

Lines changed: 16 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,90 +5,45 @@ import classnames from 'classnames';
55
import Regs from './utils/regs';
66
import { getGrid } from './utils/grids';
77
import { register } from './higherOrders/FormItem';
8+
import { triggerAble } from './higherOrders/Trigger';
89
import PropTypes from './utils/proptypes';
10+
import { compose } from './utils/compose';
911

10-
import Styles from './styles/_input.scss';
12+
import _inputs from './styles/_input.scss';
1113

1214
class Input extends Component {
1315
constructor (props) {
1416
super(props);
15-
this.state = {
16-
value: props.value
17-
};
18-
19-
this.imeLock = false;
2017

2118
this.handleChange = this.handleChange.bind(this);
22-
this.handleTrigger = this.handleTrigger.bind(this);
23-
this.handleCompositionStart = this.handleCompositionStart.bind(this);
24-
this.handleCompositionEnd = this.handleCompositionEnd.bind(this);
25-
}
26-
27-
componentWillReceiveProps (nextProps) {
28-
let value = nextProps.value;
29-
if (value !== this.props.value && value !== this.state.value) {
30-
this.setState({ value });
31-
}
3219
}
3320

3421
handleChange (event) {
35-
const { readOnly, type, trigger } = this.props;
36-
37-
if (readOnly) {
38-
return;
39-
}
22+
const { type } = this.props;
4023

4124
let value = event.target.value;
42-
4325
if (value && (type === 'integer' || type === 'number')) {
44-
if (!Regs[type].test(value)) {
45-
value = this.state.value || '';
46-
}
26+
if (!Regs[type].test(value)) return;
4727
}
4828

49-
this.setState({ value });
50-
51-
if (trigger === 'change' && !this.imeLock) {
52-
this.props.onChange(value);
53-
}
54-
}
55-
56-
handleTrigger (event) {
57-
let value = event.target.value;
58-
this.props.onChange(value, event);
59-
}
60-
61-
handleCompositionStart () {
62-
this.imeLock = true;
63-
}
64-
65-
handleCompositionEnd () {
66-
this.imeLock = false;
29+
this.props.onChange(value);
6730
}
6831

6932
render () {
70-
const { className, grid, type, size, readOnly, trigger, ...other } = this.props;
33+
const { className, grid, type, size, readOnly, ...other } = this.props;
7134
const props = {
7235
className: classnames(
7336
className,
74-
Styles.input,
75-
Styles[size],
76-
readOnly && Styles.disabled,
37+
_inputs.input,
38+
_inputs[size],
39+
readOnly && _inputs.disabled,
7740
getGrid(grid)
7841
),
7942
readOnly,
80-
onChange: this.handleChange,
81-
onCompositionStart: this.handleCompositionStart,
82-
onCompositionEnd: this.handleCompositionEnd,
83-
type: type === 'password' ? 'password' : 'text',
84-
value: this.state.value
43+
onChange: readOnly ? undefined : this.handleChange,
44+
type: type === 'password' ? 'password' : 'text'
8545
};
8646

87-
if (trigger !== 'change') {
88-
let handle = 'on' + trigger.charAt(0).toUpperCase() + trigger.slice(1);
89-
props[handle] = this.handleTrigger;
90-
}
91-
9247
return (<input {...other} {...props} />);
9348
}
9449
}
@@ -98,15 +53,12 @@ Input.propTypes = {
9853
defaultValue: PropTypes.string,
9954
grid: PropTypes.grid,
10055
id: PropTypes.string,
101-
onBlur: PropTypes.func,
10256
onChange: PropTypes.func,
103-
onFocus: PropTypes.func,
10457
placeholder: PropTypes.string,
10558
readOnly: PropTypes.bool,
10659
rows: PropTypes.number,
10760
size: PropTypes.oneOf(['small', 'large', 'middle']),
10861
style: PropTypes.object,
109-
trigger: PropTypes.string,
11062
type: PropTypes.string,
11163
value: PropTypes.any
11264
};
@@ -117,8 +69,7 @@ Input.defaultProps = {
11769
value: ''
11870
};
11971

120-
export default register(
121-
['text', 'email', 'alpha', 'alphanum', 'password', 'url', 'integer', 'number'],
122-
{},
123-
Input
124-
);
72+
export default compose(
73+
register(['text', 'email', 'alpha', 'alphanum', 'password', 'url', 'integer', 'number'], {}),
74+
triggerAble
75+
)(Input);

src/Textarea.js

Lines changed: 29 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,98 +5,67 @@ import classnames from 'classnames';
55
import { objectAssign } from './utils/objects';
66
import { getGrid } from './utils/grids';
77
import { register } from './higherOrders/FormItem';
8-
import { computedStyle, getLineHeight } from './utils/dom';
8+
import { triggerAble } from './higherOrders/Trigger';
9+
import { cloneShadow } from './utils/dom';
910
import PropTypes from './utils/proptypes';
11+
import { compose } from './utils/compose';
1012

11-
import Styles from './styles/_input.scss';
13+
import _inputs from './styles/_input.scss';
1214

1315
class Textarea extends Component {
1416
constructor (props) {
1517
super(props);
16-
this.state = {
17-
rows: props.rows
18-
};
19-
20-
this.imeLock = false;
21-
2218
this.handleChange = this.handleChange.bind(this);
23-
this.bindElement = this.bindElement.bind(this);
24-
this.handleCompositionStart = this.handleCompositionStart.bind(this);
25-
this.handleCompositionEnd = this.handleCompositionEnd.bind(this);
2619
}
2720

2821
componentDidMount () {
29-
let el = this.element;
30-
3122
if (this.props.autoHeight) {
23+
let el = this.refs.element;
24+
25+
// wait css
3226
setTimeout(() => {
33-
this.lineHeight = getLineHeight(el);
34-
this.paddingHeight = parseInt(computedStyle(el, 'paddingTop')) +
35-
parseInt(computedStyle(el, 'paddingBottom'));
36-
}, 0);
27+
this.minHeight = el.clientHeight;
28+
this.shadow = cloneShadow(el);
29+
});
3730
}
3831
}
3932

40-
bindElement (ref) {
41-
this.element = ref;
33+
componentWillUnmount () {
34+
if (this.shadow) {
35+
this.refs.element.parentNode.removeChild(this.shadow);
36+
}
4237
}
4338

4439
handleChange (event) {
45-
this.props.autoHeight && this.autoHeight();
46-
this.props.onChange(event.target.value);
47-
}
48-
49-
handleCompositionStart () {
50-
this.imeLock = true;
51-
}
52-
53-
handleCompositionEnd () {
54-
this.imeLock = false;
55-
}
56-
57-
autoHeight () {
58-
let el = this.element;
59-
let scrH;
60-
let rows;
61-
62-
scrH = el.scrollHeight - this.paddingHeight;
63-
rows = Math.floor(scrH / this.lineHeight);
64-
65-
if (rows >= this.props.rows) {
66-
this.setState({
67-
rows
68-
});
69-
}
70-
71-
el.style.height = 'auto';
40+
let value = event.target.value;
41+
this.shadow.value = value;
42+
this.refs.element.style.height = Math.max(this.minHeight, this.shadow.scrollHeight) + 'px';
43+
this.props.onChange(event.target.value, event);
7244
}
7345

7446
render () {
7547
let { className, grid, autoHeight, readOnly, ...other } = this.props;
76-
const { rows } = this.state;
7748

78-
let style = { minHeight: 'auto' };
49+
let style = {};
7950
if (autoHeight) {
8051
style.resize = 'none';
52+
style.overflowY = 'hidden';
8153
}
8254

8355
const props = {
8456
className: classnames(
8557
className,
8658
getGrid(grid),
87-
readOnly && Styles.disabled,
88-
Styles.input
59+
readOnly && _inputs.disabled,
60+
_inputs.input
8961
),
90-
onChange: this.handleChange,
91-
onCompositionStart: this.handleCompositionStart,
92-
onCompositionEnd: this.handleCompositionEnd,
62+
onChange: readOnly ? undefined : this.handleChange,
9363
style: objectAssign({}, this.props.style, style),
94-
readOnly,
95-
rows
64+
readOnly
9665
};
9766

9867
return (
99-
<textarea ref={this.bindElement} { ...other } { ...props } />
68+
<textarea ref="element" { ...other } { ...props } />
10069
);
10170
}
10271
}
@@ -120,5 +89,8 @@ Textarea.defaultProps = {
12089
value: ''
12190
};
12291

123-
export default register(['textarea'], {}, Textarea);
92+
export default compose(
93+
register(['textarea'], {}),
94+
triggerAble
95+
)(Textarea);
12496

0 commit comments

Comments
 (0)