ghost-keychain/packages/extension-ui/src/components/InputWithLabel.tsx
Thibaut Sardan 34e16c2a04
Refactor password error in request pop up and remember checkbox (#503)
* error and remember checkbox refactoring

* make it build

* remove margin

Co-authored-by: Github Actions <action@github.com>
2020-10-20 16:34:48 +02:00

74 lines
1.8 KiB
TypeScript

// Copyright 2019-2020 @polkadot/extension-ui authors & contributors
// SPDX-License-Identifier: Apache-2.0
import React, { useCallback } from 'react';
import styled from 'styled-components';
import Label from './Label';
import { Input } from './TextInputs';
interface Props {
className?: string;
defaultValue?: string | null;
disabled?: boolean;
isError?: boolean;
isFocused?: boolean;
isReadOnly?: boolean;
label: string;
onBlur?: () => void;
onChange?: (value: string) => void;
onEnter?: () => void;
placeholder?: string;
type?: 'text' | 'password';
value?: string;
withoutMargin?: boolean;
}
function InputWithLabel ({ className, defaultValue, disabled, isError, isFocused, isReadOnly, label = '', onBlur, onChange, onEnter, placeholder, type = 'text', value, withoutMargin }: Props): React.ReactElement<Props> {
const _checkEnter = useCallback(
(event: React.KeyboardEvent<HTMLInputElement>): void => {
onEnter && event.key === 'Enter' && onEnter();
},
[onEnter]
);
const _onChange = useCallback(
({ target: { value } }: React.ChangeEvent<HTMLInputElement>): void => {
onChange && onChange(value);
},
[onChange]
);
return (
<Label
className={`${className || ''} ${withoutMargin ? 'withoutMargin' : ''}`}
label={label}
>
<Input
autoCapitalize='off'
autoCorrect='off'
autoFocus={isFocused}
defaultValue={defaultValue || undefined}
disabled={disabled}
onBlur={onBlur}
onChange={_onChange}
onKeyPress={_checkEnter}
placeholder={placeholder}
readOnly={isReadOnly}
spellCheck={false}
type={type}
value={value}
withError={isError}
/>
</Label>
);
}
export default styled(InputWithLabel)`
margin-bottom: 16px;
&.withoutMargin {
margin-bottom: 0px;
}
`;