Skip to content

Commit

Permalink
feat(core-components-confirmation): separate error and error text (#329)
Browse files Browse the repository at this point in the history
* feat(core-components-confirmation): separate error and error text

separate error and error text, fix centering

BREAKING CHANGE: separate error and error text

* feat(core-components-confirmation): fix margins
  • Loading branch information
dmitrsavk committed Oct 27, 2020
1 parent 0f4d547 commit 03fc4fb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 20 deletions.
6 changes: 4 additions & 2 deletions packages/confirmation/src/component.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ import { Select } from '../../select/src/Component';
onSmsRetryClick={() => handleSmsRetryClick('error')}
codeSending={codeSending['error']}
codeChecking={codeChecking['error']}
error={error['error']}
error={!!error['error']}
errorText={error['error']}
countdownDuration={countdownDuration}
code={value['error']}
onInputChange={({ code }) => setCode('error', code)}
Expand All @@ -117,7 +118,8 @@ import { Select } from '../../select/src/Component';
onSmsRetryClick={() => handleSmsRetryClick('fatal')}
codeSending={codeSending['fatal']}
codeChecking={codeChecking['fatal']}
error={error['fatal']}
error={!!error['fatal']}
errorText={error['fatal']}
errorIsFatal={true}
countdownDuration={countdownDuration}
code={value['fatal']}
Expand Down
19 changes: 13 additions & 6 deletions packages/confirmation/src/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ export type ConfirmationProps = {
*/
codeSending?: boolean;

/**
* Состояние ошибки подписания
*/
error?: boolean;

/**
* Текст ошибки подписания
*/
error?: string;
errorText?: string;

/**
* Дополнительный контент
Expand Down Expand Up @@ -154,7 +159,8 @@ export const Confirmation = forwardRef<HTMLDivElement, ConfirmationProps>(
dataTestId,
errorIsFatal,
errorTitle = 'Превышено количество попыток ввода кода',
error,
error = false,
errorText,
hasPhoneMask = true,
hasSmsCountdown = true,
phone,
Expand Down Expand Up @@ -183,13 +189,13 @@ export const Confirmation = forwardRef<HTMLDivElement, ConfirmationProps>(

const [countdownFinished, setCountdownFinished] = useState(false);

const shouldShowError = errorIsFatal && !!error;
const shouldShowError = errorIsFatal && Boolean(errorText);

const shouldShowSignComponent = !showHint && !shouldShowError;

const shouldShowHint = showHint && !shouldShowError;

const nonFatalError = errorIsFatal ? '' : error;
const nonFatalError = errorIsFatal ? '' : errorText;

const shouldShowHintLink = countdownFinished && !codeChecking && retries > 0;

Expand Down Expand Up @@ -256,7 +262,8 @@ export const Confirmation = forwardRef<HTMLDivElement, ConfirmationProps>(
phone={phone}
code={code}
hasPhoneMask={hasPhoneMask}
error={nonFatalError || ''}
errorText={nonFatalError || ''}
error={error}
title={signTitle}
inputRef={inputRef}
codeCheckingText={codeCheckingText}
Expand All @@ -274,7 +281,7 @@ export const Confirmation = forwardRef<HTMLDivElement, ConfirmationProps>(
<div className={styles.error}>
<span className={styles.errorHeader}>{errorTitle}</span>

<span className={styles.errorText}>{error}</span>
<span className={styles.errorText}>{errorText}</span>

<Button
size='s'
Expand Down
22 changes: 18 additions & 4 deletions packages/confirmation/src/components/code-input/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import cn from 'classnames';

import { usePrevious } from '@alfalab/hooks';

import { ContentAlign } from '../../component';
import { mergeArrays } from './utils';

import styles from './index.module.css';
Expand All @@ -19,8 +20,9 @@ type CodeInputProps = {
processing: boolean;
value: string;
slotsCount: number;
error?: string;
error: boolean;
className?: string;
alignContent: ContentAlign;
handleChange: (code: string) => void;
handleInputKeyDown: (event: KeyboardEvent) => void;
};
Expand All @@ -31,8 +33,9 @@ type InputProps = {
index: number;
value: string;
slotsCount: number;
error?: string;
error: boolean;
processing: boolean;
alignContent: ContentAlign;
focus: (inputIndex: number) => void;
handleInputKeyDown: (event: KeyboardEvent) => void;
setRef: SetInputRef;
Expand All @@ -47,6 +50,7 @@ const Input = ({
error,
processing,
value,
alignContent,
handleChange,
handleInputKeyDown,
setRef,
Expand Down Expand Up @@ -122,7 +126,7 @@ const Input = ({

return (
<input
className={cn(styles.input, { [styles.hasError]: Boolean(error) })}
className={cn(styles.input, styles[alignContent], { [styles.hasError]: error })}
disabled={processing}
value={splittedValue[index] || ''}
autoComplete={index === 0 ? 'one-time-code' : ''}
Expand All @@ -136,7 +140,16 @@ const Input = ({

export const CodeInput = forwardRef<HTMLInputElement, CodeInputProps>(
(
{ processing, value = '', slotsCount, error, handleInputKeyDown, handleChange, className },
{
processing,
value = '',
slotsCount,
error,
handleInputKeyDown,
handleChange,
className,
alignContent,
},
ref,
) => {
const inputs = useRef<HTMLInputElement[]>([]);
Expand Down Expand Up @@ -204,6 +217,7 @@ export const CodeInput = forwardRef<HTMLInputElement, CodeInputProps>(
error={error}
processing={processing}
slotsCount={slotsCount}
alignContent={alignContent}
handleChange={handleChange}
handleInputKeyDown={handleInputKeyDown}
setRef={setRef}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
font-weight: var(--confirmation-input-font-weight);
font-family: var(--confirmation-input-font-family);

&:last-child {
margin-right: var(--gap-xs);
}

&.hasError {
color: var(--confirmation-error-color);
caret-color: var(--confirmation-input-text-color);
background-color: var(--confirmation-input-error-bg-color);
}

&.center {
margin: 0 var(--gap-2xs);
}
}

@keyframes shake {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export type SignConfirmationProps = {
hasPhoneMask: boolean;
phone?: string;
code: string;
error: string;
errorText: string;
error: boolean;
title: string;
codeCheckingText: string;
codeSendingText: string;
Expand All @@ -44,7 +45,8 @@ export const SignConfirmation: FC<SignConfirmationProps> = ({
hasPhoneMask,
phone,
code: inputValue,
error = '',
error,
errorText,
title,
hasSmsCountdown,
inputRef,
Expand All @@ -59,7 +61,7 @@ export const SignConfirmation: FC<SignConfirmationProps> = ({
}) => {
const processing = codeChecking || codeSending;

const displayedError = processing ? '' : error;
const displayedError = processing ? '' : errorText;

const handleInputKeyDown = useCallback(
(event: KeyboardEvent) => {
Expand Down Expand Up @@ -99,11 +101,12 @@ export const SignConfirmation: FC<SignConfirmationProps> = ({
<div className={styles.inputContainer}>
<CodeInput
processing={processing}
error={displayedError}
error={error}
value={inputValue}
ref={inputRef}
slotsCount={requiredCharAmount}
className={styles.codeInput}
alignContent={alignContent}
handleChange={handleInputChange}
handleInputKeyDown={handleInputKeyDown}
/>
Expand Down

0 comments on commit 03fc4fb

Please sign in to comment.