Skip to content

Commit c5e8cd6

Browse files
author
Nelson Menezes
committed
Add custom textarea component
1 parent d1a0ac2 commit c5e8cd6

File tree

3 files changed

+94
-69
lines changed

3 files changed

+94
-69
lines changed

lib/src/components/CustomInput/CustomInput.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import rules from './../../rules';
55
import Base from './../Base/Base';
66

77
export const makeCustomInput = function(WrappedComponent) {
8-
98
class CustomInput extends Base {
109
static propTypes = {
1110
validations: PropTypes.arrayOf(PropTypes.string).isRequired,
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import React, { PropTypes, createElement } from 'react';
2+
import hoistStatics from 'hoist-non-react-statics';
3+
import cx from 'classnames';
4+
import rules from './../../rules';
5+
import Base from './../Base/Base';
6+
7+
export function makeCustomTextarea(WrappedComponent) {
8+
class CustomTextarea extends Base {
9+
static propTypes = {
10+
validations: PropTypes.arrayOf(PropTypes.string).isRequired,
11+
errorClassName: PropTypes.string,
12+
containerClassName: PropTypes.string,
13+
errorContainerClassName: PropTypes.string
14+
};
15+
16+
static contextTypes = {
17+
register: PropTypes.func.isRequired,
18+
unregister: PropTypes.func.isRequired,
19+
validateState: PropTypes.func.isRequired,
20+
components: PropTypes.objectOf(PropTypes.any),
21+
errors: PropTypes.objectOf(PropTypes.array)
22+
};
23+
24+
constructor(props, context) {
25+
super(props, context);
26+
27+
this.state = {
28+
value: props.value,
29+
isChanged: !!props.value,
30+
isUsed: false,
31+
isChecked: true
32+
};
33+
34+
context.register(this);
35+
}
36+
37+
render() {
38+
const {
39+
/* eslint-disable */
40+
validations,
41+
/* eslint-enable */
42+
errorClassName,
43+
containerClassName,
44+
errorContainerClassName,
45+
className,
46+
value,
47+
onChange,
48+
onBlur,
49+
...rest } = this.props;
50+
// TODO: Refactor conditions
51+
const isInvalid = this.state.isUsed &&
52+
this.state.isChanged &&
53+
!!this.context.errors[this.props.name];
54+
const error = isInvalid && this.context.errors[this.props.name][0];
55+
let hint = null;
56+
57+
if (isInvalid) {
58+
hint = typeof error === 'function' ? error(this.state.value, this.context.components) : rules[error].hint(this.state.value, this.context.components);
59+
}
60+
61+
const wrappedProps = {
62+
containerClassName: cx({
63+
[containerClassName]: !!containerClassName,
64+
[errorContainerClassName]: !!error && errorContainerClassName
65+
}),
66+
className: cx({
67+
[className]: !!className,
68+
[errorClassName]: !!error && errorClassName
69+
}),
70+
onChange: this.onChange,
71+
onBlur: this.onBlur,
72+
value: this.state.value,
73+
hint,
74+
...rest
75+
};
76+
77+
return createElement(WrappedComponent, wrappedProps);
78+
}
79+
}
80+
81+
return hoistStatics(CustomTextarea, WrappedComponent);
82+
}
Lines changed: 12 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,21 @@
11
import React, { PropTypes } from 'react';
2-
import cx from 'classnames';
3-
import rules from './../../rules';
4-
import Base from './../Base/Base';
5-
6-
export default class Textarea extends Base {
7-
static propTypes = {
8-
validations: PropTypes.arrayOf(PropTypes.string).isRequired,
9-
errorClassName: PropTypes.string,
10-
containerClassName: PropTypes.string,
11-
errorContainerClassName: PropTypes.string
12-
};
13-
14-
static contextTypes = {
15-
register: PropTypes.func.isRequired,
16-
unregister: PropTypes.func.isRequired,
17-
validateState: PropTypes.func.isRequired,
18-
components: PropTypes.objectOf(PropTypes.any),
19-
errors: PropTypes.objectOf(PropTypes.array)
20-
};
21-
22-
constructor(props, context) {
23-
super(props, context);
24-
25-
this.state = {
26-
value: props.value,
27-
isChanged: !!props.value,
28-
isUsed: false,
29-
isChecked: true
30-
};
31-
32-
context.register(this);
33-
}
2+
import { makeCustomTextarea } from './../CustomTextarea/CustomTextarea';
343

4+
export default makeCustomTextarea(React.createClass({
355
render() {
36-
const {
37-
/* eslint-disable */
38-
validations,
39-
/* eslint-enable */
40-
errorClassName,
41-
containerClassName,
42-
errorContainerClassName,
43-
className,
44-
...rest } = this.props;
45-
// TODO: Refactor conditions
46-
const isInvalid = this.state.isUsed &&
47-
this.state.isChanged &&
48-
!!this.context.errors[this.props.name];
49-
const error = isInvalid && this.context.errors[this.props.name][0];
50-
let hint = null;
51-
52-
if (isInvalid) {
53-
hint = typeof error === 'function' ? error(this.state.value, this.context.components) : rules[error].hint(this.state.value, this.context.components);
54-
}
6+
const { containerClassName, hint, ...rest } = this.props;
557

568
return (
57-
<div
58-
className={cx({
59-
[containerClassName]: !!containerClassName,
60-
[errorContainerClassName]: !!error && errorContainerClassName
61-
})}
62-
>
9+
<div className={containerClassName}>
6310
<textarea
64-
{...rest}
65-
className={cx({
66-
[className]: !!className,
67-
[errorClassName]: !!error && errorClassName
68-
})}
69-
onChange={this.onChange}
70-
onBlur={this.onBlur}
71-
value={this.state.value}
72-
/>
11+
{...rest}
12+
className={this.props.className}
13+
onChange={this.props.onChange}
14+
onBlur={this.props.onBlur}
15+
value={this.props.value}
16+
/>
7317
{hint}
7418
</div>
75-
);
19+
)
7620
}
77-
}
21+
}));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy