-
Notifications
You must be signed in to change notification settings - Fork 80
/
Field.jsx
77 lines (70 loc) · 1.79 KB
/
Field.jsx
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
import React from 'react';
import PropTypes from 'prop-types';
import { Accounts } from 'meteor/accounts-base';
export class Field extends React.Component {
constructor(props) {
super(props);
this.state = {
mount: true
};
}
triggerUpdate() {
// Trigger an onChange on inital load, to support browser prefilled values.
const { onChange } = this.props;
if (this.input && onChange) {
onChange({ target: { value: this.input.value } });
}
}
componentDidMount() {
this.triggerUpdate();
}
componentDidUpdate(prevProps) {
// Re-mount component so that we don't expose browser prefilled passwords if the component was
// a password before and now something else.
if (prevProps.id !== this.props.id) {
this.setState({mount: false});
}
else if (!this.state.mount) {
this.setState({mount: true});
this.triggerUpdate();
}
}
render() {
const {
id,
hint,
label,
type = 'text',
onChange,
required = false,
className = "field",
defaultValue = "",
message,
} = this.props;
const { mount = true } = this.state;
if (type == 'notice') {
return <div className={ className }>{ label }</div>;
}
return mount ? (
<div className={ className }>
<label htmlFor={ id }>{ label }</label>
<input
id={ id }
ref={ (ref) => this.input = ref }
type={ type }
onChange={ onChange }
placeholder={ hint }
defaultValue={ defaultValue }
/>
{message && (
<span className={['message', message.type].join(' ').trim()}>
{message.message}</span>
)}
</div>
) : null;
}
}
Field.propTypes = {
onChange: PropTypes.func
};
Accounts.ui.Field = Field;