ghost-portal/packages/ui-app/src/Input.tsx
Luke Schoen 9a33e05d54 fix: Show errors. Add Password maxLength. Fix classes lib. Fix styles. (#335)
* Account change password

* Small cleanups

* Props, State to Component

* fix: Allow change account immediately after create account without refresh page

* fix: Add missing label prop to allow overriding default label

* feat: Add maxLength of 32 to Password component and pass prop to Input

* fix: Restricted spacebar key in Password using onKeyDown

* fix: Add missing classes library to Password component Input

* fix: Add className to ChangePass modal component using classes library

* fix: Reorder props alphabetically

* fix: Add missing  namespace to className of AddressSummary in ChangePass

* fix: Show submission errors in ChangePass modal using Semantic Message component

* fix: Add side padding to error component in ChangePass modal

* fix: Removed unused variable to fix TS lint error

* revert showing error messages

* fix: Revert restriction so allow spacebar key in Password using onKeyDown

* fix: Add className to outer Modal component
2018-09-30 13:29:08 +02:00

167 lines
4.1 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 SUIInput from 'semantic-ui-react/dist/commonjs/elements/Input/Input';
import isUndefined from '@polkadot/util/is/undefined';
import Labelled from './Labelled';
type Input$Type = 'number' | 'password' | 'text';
type Props = BareProps & {
autoFocus?: boolean,
children?: React.ReactNode,
defaultValue?: any,
icon?: any, // node?
isAction?: boolean,
isDisabled?: boolean,
isEditable?: boolean,
isError?: boolean,
isHidden?: boolean,
label?: any, // node?
max?: any,
maxLength?: number,
min?: any,
name?: string,
onChange: (value: string) => void,
onKeyDown?: (event: React.KeyboardEvent<Element>) => void,
onKeyUp?: (event: React.KeyboardEvent<Element>) => void,
placeholder?: string,
tabIndex?: number,
type?: Input$Type,
value?: any,
withLabel?: boolean
};
type State = {
name: string;
};
// note: KeyboardEvent.keyCode and KeyboardEvent.which are deprecated
const KEYS = {
A: 'a',
ALT: 'Alt',
ARROW_LEFT: 'ArrowLeft',
ARROW_RIGHT: 'ArrowRight',
BACKSPACE: 'Backspace',
C: 'c',
CMD: 'Meta',
CTRL: 'Control',
ENTER: 'Enter',
ESCAPE: 'Escape',
TAB: 'Tab',
V: 'v',
X: 'x',
ZERO: '0'
};
const KEYS_PRE: Array<any> = [KEYS.ALT, KEYS.CMD, KEYS.CTRL];
// reference: degrade key to keyCode for cross-browser compatibility https://www.w3schools.com/jsref/event_key_keycode.asp
const isCopy = (key: string, isPreKeyDown: boolean): boolean =>
isPreKeyDown && key === KEYS.C;
const isCut = (key: string, isPreKeyDown: boolean): boolean =>
isPreKeyDown && key === KEYS.X;
const isPaste = (key: string, isPreKeyDown: boolean): boolean =>
isPreKeyDown && key === KEYS.V;
const isSelectAll = (key: string, isPreKeyDown: boolean): boolean =>
isPreKeyDown && key === KEYS.A;
let counter = 0;
export default class Input extends React.PureComponent<Props, State> {
state: State = {
name: `in_${counter++}_at_${Date.now()}`
};
render () {
const { autoFocus = false, children, className, defaultValue, icon, isEditable = false, isAction = false, isDisabled = false, isError = false, isHidden = false, label, max, maxLength, min, name, placeholder, style, tabIndex, type = 'text', value, withLabel } = this.props;
return (
<Labelled
className={className}
label={label}
style={style}
withLabel={withLabel}
>
<SUIInput
autoFocus={autoFocus}
action={isAction}
className={isEditable ? 'edit icon' : ''}
defaultValue={defaultValue}
disabled={isDisabled}
id={name}
iconPosition={
isUndefined(icon)
? void 0
: 'left'
}
error={isError}
hidden={isHidden}
max={max}
maxLength={maxLength}
min={min}
name={name || this.state.name}
onChange={this.onChange}
onKeyDown={this.onKeyDown}
onKeyUp={this.onKeyUp}
placeholder={placeholder}
tabIndex={tabIndex}
type={type}
value={value}
>
<input
autoComplete={
type === 'password'
? 'new-password'
: 'off'
}
/>
{isEditable ? <i className='edit icon' /> : null}
{icon}
{children}
</SUIInput>
</Labelled>
);
}
onChange = (event: React.SyntheticEvent<Element>): void => {
const { onChange } = this.props;
const { value } = event.target as HTMLInputElement;
onChange(value);
}
onKeyDown = (event: React.KeyboardEvent<Element>): void => {
const { onKeyDown } = this.props;
if (onKeyDown) {
onKeyDown(event);
}
}
onKeyUp = (event: React.KeyboardEvent<Element>): void => {
const { onKeyUp } = this.props;
if (onKeyUp) {
onKeyUp(event);
}
}
}
export {
isCopy,
isCut,
isPaste,
isSelectAll,
KEYS,
KEYS_PRE
};