ghost-portal/packages/ui-app/src/Password.tsx
Luke Schoen 189e486ed8 feat: Fixes Issue #260. Allow use Enter to submit extrinsics (#265)
* WIP: Fixes #260. Autofocus password input field. Press tab after enter password to switch focus to submit

* feat: Clicks Submit Transaction when user presses Enter after entering password

* fix: Remove console logs and no need to focus

* test: Add test setup. setState not changing currentItem from undefined

* fix: Change tabIndex values to number instead of string

* fix: Use Refs instead of JS DOM access

* fix: Add missing newline

* test: Test modal show after setState. Skipped pending resolution of Enzyme issue

* fix: Remove string from tabIndex prop type

* fix: Remove use of SUIEvent. Use React refs not HTML DOM queries

* test: Update tests but skipping since awaiting resolution of enzyme issue

Reference: https://github.com/airbnb/enzyme/issues/1794

* WIP

* fix: Simply call onSend instead of long-winded approach with Refs

* test: Fix tests to use promises instead of callbacks

* fix: Fix tslint error to handle promises correctly

* fix: Remove return since broke functionality. Use await to handle promise. Use key instead of keyCode
2018-09-13 11:36:52 +02:00

82 lines
1.8 KiB
TypeScript

// Copyright 2017-2018 @polkadot/ui-app authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { BareProps } from './types';
import React from 'react';
import Button from './Button';
import Input from './Input';
type Props = BareProps & {
autoFocus?: boolean,
defaultValue?: any,
children?: React.ReactNode,
isDisabled?: boolean,
isError?: boolean,
label?: string,
name?: string,
onChange: (value: string) => void,
onKeyDown?: (event: React.KeyboardEvent<Element>) => void,
tabIndex?: number,
value: any,
withLabel?: boolean
};
type State = {
isVisible: boolean
};
export default class Password extends React.PureComponent<Props, State> {
state: State = {
isVisible: false
};
render () {
const { autoFocus, children, className, defaultValue, isDisabled, isError, label, name, onChange, onKeyDown, style, tabIndex, value, withLabel } = this.props;
const { isVisible } = this.state;
return (
<Input
autoFocus={autoFocus}
className={className}
defaultValue={defaultValue}
isAction
isDisabled={isDisabled}
isError={isError}
label={label}
name={name}
onChange={onChange}
onKeyDown={onKeyDown}
style={style}
tabIndex={tabIndex}
type={
isVisible
? 'text'
: 'password'
}
value={value}
withLabel={withLabel}
>
<Button
icon={
isVisible
? 'hide'
: 'unhide'
}
isPrimary
onClick={this.onToggleVisible}
/>
{children}
</Input>
);
}
onToggleVisible = (): void => {
this.setState({
isVisible: !this.state.isVisible
});
}
}