2017-05-19 14:42:43 -06:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-06-02 07:19:39 +09:00
|
|
|
import FormControl from 'react-bootstrap/lib/FormControl';
|
2017-03-23 16:27:59 +09:00
|
|
|
import { registerComponent } from 'meteor/vulcan:core';
|
2017-03-15 10:36:02 +08:00
|
|
|
|
2018-01-11 13:50:16 +09:00
|
|
|
const autocompleteValues = {
|
|
|
|
'username': 'username',
|
|
|
|
'usernameOrEmail': 'email',
|
|
|
|
'email': 'email',
|
|
|
|
'password': 'current-password'
|
|
|
|
}
|
|
|
|
|
2017-05-19 14:42:43 -06:00
|
|
|
export class AccountsField extends PureComponent {
|
2017-03-15 10:36:02 +08:00
|
|
|
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,
|
2018-01-25 15:03:03 -06:00
|
|
|
// required = false,
|
2017-03-15 10:36:02 +08:00
|
|
|
className = "field",
|
|
|
|
defaultValue = "",
|
|
|
|
message,
|
|
|
|
} = this.props;
|
|
|
|
const { mount = true } = this.state;
|
|
|
|
if (type == 'notice') {
|
|
|
|
return <div className={ className }>{ label }</div>;
|
|
|
|
}
|
2018-01-11 13:50:16 +09:00
|
|
|
|
|
|
|
const autoComplete = autocompleteValues[id];
|
|
|
|
|
2017-03-15 10:36:02 +08:00
|
|
|
return mount ? (
|
2017-03-23 12:50:49 +09:00
|
|
|
<div className={ className } style={{marginBottom: '10px'}}>
|
2018-01-11 13:50:16 +09:00
|
|
|
<FormControl id={ id } type={ type } inputRef={ref => { this.input = ref; }} onChange={ onChange } placeholder={ hint } defaultValue={ defaultValue } autoComplete={autoComplete }/>
|
2017-03-15 10:36:02 +08:00
|
|
|
{message && (
|
|
|
|
<span className={['message', message.type].join(' ').trim()}>
|
|
|
|
{message.message}</span>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
) : null;
|
|
|
|
}
|
|
|
|
}
|
2017-03-23 12:50:49 +09:00
|
|
|
AccountsField.propTypes = {
|
2017-05-19 14:42:43 -06:00
|
|
|
onChange: PropTypes.func
|
2017-03-15 10:36:02 +08:00
|
|
|
};
|
|
|
|
|
2017-03-23 12:50:49 +09:00
|
|
|
registerComponent('AccountsField', AccountsField)
|