accounts-ui/imports/ui/components/Field.jsx
2016-04-02 22:49:12 +02:00

63 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React from 'react';
import ReactDOM from 'react-dom';
import { Accounts } from 'meteor/accounts-base';
export class Field extends React.Component {
constructor(props) {
super(props);
this.state = {
mount: true
};
}
componentDidMount() {
// Trigger an onChange on inital load, to support browser prefilled values.
const { onChange } = this.props;
let node = ReactDOM.findDOMNode(this);
if (node) {
let value = node.getElementsByTagName('input')[0].value;
// Match the data format of a typical onChange event.
onChange({ target: { value: value } });
}
}
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.type !== this.props.type) {
this.setState({mount: false});
}
else if (!this.state.mount) {
this.setState({mount: true});
}
}
render() {
const {
id,
hint,
label,
type = 'text',
onChange,
required = false,
className = "field",
defaultValue = ""
} = this.props;
const { mount = true } = this.state;
return mount ? (
<div className={ className }>
<label htmlFor={ id }>{ label }</label>
<input id={ id } 
type={ type }
onChange={ onChange }
placeholder={ hint }
defaultValue={ defaultValue } />
</div>
) : null;
}
}
Field.propTypes = {
onChange: React.PropTypes.func
};
Accounts.ui.Field = Field;