1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-28 11:18:16 +00:00

duplicate device unique name

This commit is contained in:
Szymon Lesisz 2018-05-15 19:47:24 +02:00
parent cefddd98b8
commit ed6450f3ef
2 changed files with 63 additions and 32 deletions

View File

@ -8,12 +8,14 @@ import type { Props } from './index';
type State = { type State = {
defaultName: string; defaultName: string;
instanceName: ?string; instanceName: ?string;
isUsed: boolean;
} }
export default class DuplicateDevice extends Component<Props, State> { export default class DuplicateDevice extends Component<Props, State> {
keyboardHandler: (event: KeyboardEvent) => void; keyboardHandler: (event: KeyboardEvent) => void;
state: State; state: State;
input: ?HTMLInputElement;
constructor(props: Props) { constructor(props: Props) {
super(props); super(props);
@ -25,18 +27,22 @@ export default class DuplicateDevice extends Component<Props, State> {
this.state = { this.state = {
defaultName: `${device.label} (${instance.toString()})`, defaultName: `${device.label} (${instance.toString()})`,
instanceName: null instanceName: null,
isUsed: false,
} }
} }
keyboardHandler(event: KeyboardEvent): void { keyboardHandler(event: KeyboardEvent): void {
if (event.keyCode === 13) { if (event.keyCode === 13 && !this.state.isUsed) {
event.preventDefault(); event.preventDefault();
this.submit(); this.submit();
} }
} }
componentDidMount(): void { componentDidMount(): void {
// one time autofocus
if (this.input)
this.input.focus();
this.keyboardHandler = this.keyboardHandler.bind(this); this.keyboardHandler = this.keyboardHandler.bind(this);
window.addEventListener('keydown', this.keyboardHandler, false); window.addEventListener('keydown', this.keyboardHandler, false);
} }
@ -46,16 +52,22 @@ export default class DuplicateDevice extends Component<Props, State> {
} }
onNameChange = (value: string): void => { onNameChange = (value: string): void => {
let isUsed: boolean = false;
if (value.length > 0) {
isUsed = ( this.props.connect.devices.find(d => d.instanceName === value) !== undefined );
}
this.setState({ this.setState({
instanceName: value.length > 0 ? value : null instanceName: value.length > 0 ? value : null,
isUsed
}); });
} }
submit() { submit() {
if (this.props.modal.opened) { if (!this.props.modal.opened) return;
this.props.modalActions.onDuplicateDevice( { ...this.props.modal.device, instanceName: this.state.instanceName } ); this.props.modalActions.onDuplicateDevice( { ...this.props.modal.device, instanceName: this.state.instanceName } );
} }
}
render() { render() {
@ -63,11 +75,18 @@ export default class DuplicateDevice extends Component<Props, State> {
const { device } = this.props.modal; const { device } = this.props.modal;
const { onCancel, onDuplicateDevice } = this.props.modalActions; const { onCancel, onDuplicateDevice } = this.props.modalActions;
const {
defaultName,
instanceName,
isUsed
} = this.state;
return ( return (
<div className="duplicate"> <div className="duplicate">
<button className="close-modal transparent" onClick={ onCancel }></button>
<h3>Clone { device.label }?</h3> <h3>Clone { device.label }?</h3>
<p>This will create new instance of device which can be used with different passphrase</p> <p>This will create new instance of device which can be used with different passphrase</p>
<div className="row">
<label>Instance name</label> <label>Instance name</label>
<input <input
type="text" type="text"
@ -75,10 +94,14 @@ export default class DuplicateDevice extends Component<Props, State> {
autoCorrect="off" autoCorrect="off"
autoCapitalize="off" autoCapitalize="off"
spellCheck="false" spellCheck="false"
placeholder={ this.state.defaultName } className={ isUsed ? 'not-valid' : null }
placeholder={ defaultName }
ref={ (element) => { this.input = element; } }
onChange={ event => this.onNameChange(event.currentTarget.value) } onChange={ event => this.onNameChange(event.currentTarget.value) }
defaultValue={ this.state.instanceName } /> defaultValue={ instanceName } />
<button onClick={ event => this.submit() }>Create new instance</button> { isUsed ? <span className="error">Instance name is already in use</span> : null }
</div>
<button disabled={ isUsed } onClick={ event => this.submit() }>Create new instance</button>
<button className="white" onClick={ onCancel }>Cancel</button> <button className="white" onClick={ onCancel }>Cancel</button>
</div> </div>
); );

View File

@ -126,6 +126,11 @@
padding: 14px 0px; padding: 14px 0px;
} }
.row {
position: relative;
text-align: left;
padding-bottom: 20px;
label { label {
display: block; display: block;
padding-bottom: 6px; padding-bottom: 6px;
@ -133,7 +138,16 @@
color: @color_text_secondary; color: @color_text_secondary;
} }
button { .error {
position: absolute;
left: 0px;
bottom: 0px;
font-size: 12px;
color: @color_error_primary;
}
}
button:not(.close-modal) {
width: 100%; width: 100%;
margin-top: 12px; margin-top: 12px;
span { span {
@ -256,20 +270,14 @@
color: @color_error_primary; color: @color_error_primary;
} }
} }
// input[type="text"] {
// font-family: 'fontello';
// font-size: 6px;
// line-height: 14px;
// }
} }
input[type="text"], input[type="text"],
input[type="password"] { input[type="password"] {
width: 260px; width: 260px;
box-shadow: none; // box-shadow: none;
border-radius: 0px; // border-radius: 0px;
border: 1px solid @color_divider; // border: 1px solid @color_divider;
height: auto; height: auto;
.placeholder({ .placeholder({