Skip to content

Commit

Permalink
feat(core-components-form-control): change errors logic (#150)
Browse files Browse the repository at this point in the history
* feat(core-components-form-control): change errors logic

* fix(core-components-form-control): hideErrorIcon -> hasErrorIcon

* revert(core-components-input): revert add hasErrorIcon

* revert(core-components-form-control): revert add hasErrorIcon
  • Loading branch information
reme3d2y committed Jun 15, 2020
1 parent 2d82709 commit 1c0e494
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/form-control/src/Component.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { FormControl } from '@alfalab/core-components-form-control';
label={text('label', '')}
hint={text('hint', '')}
error={text('error', '')}
rightAddons={boolean('rightAddons', false) && !text('error', '') && <Icon />}
rightAddons={boolean('rightAddons', false) && !text('error') && <Icon />}
leftAddons={boolean('leftAddons', false) && <Icon />}
bottomAddons={boolean('bottomAddons', false) && <span>bottom text</span>}
/>
Expand Down
10 changes: 7 additions & 3 deletions packages/form-control/src/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export type FormControlProps = HTMLAttributes<HTMLDivElement> & {
focused?: boolean;

/**
* Текст ошибки
* Отображение ошибки
*/
error?: string;
error?: string | boolean;

/**
* Текст подсказки
Expand Down Expand Up @@ -115,6 +115,8 @@ export const FormControl = ({
[leftAddons],
);

const errorMessage = typeof error === 'string' ? error : '';

return (
<div
data-test-id={dataTestId}
Expand Down Expand Up @@ -151,7 +153,9 @@ export const FormControl = ({
{rightAddonsRenderer()}
</div>

{(error || hint) && <span className={styles.sub}>{error || hint}</span>}
{errorMessage && <span className={cn(styles.sub, styles.error)}>{errorMessage}</span>}

{hint && !errorMessage && <span className={styles.sub}>{hint}</span>}

{bottomAddons}
</div>
Expand Down
20 changes: 16 additions & 4 deletions packages/form-control/src/__snapshots__/Component.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Object {
/>
</div>
</div>
</div>
</div>
</body>,
Expand All @@ -37,6 +38,7 @@ Object {
/>
</div>
</div>
</div>
</div>,
"debug": [Function],
Expand Down Expand Up @@ -132,7 +134,7 @@ Object {
</div>
</div>
<span
class="sub"
class="sub error"
>
error
</span>
Expand Down Expand Up @@ -174,7 +176,7 @@ Object {
</div>
</div>
<span
class="sub"
class="sub error"
>
error
</span>
Expand Down Expand Up @@ -253,6 +255,7 @@ Object {
/>
</div>
</div>
<div>
Bottom addons
</div>
Expand All @@ -274,6 +277,7 @@ Object {
/>
</div>
</div>
<div>
Bottom addons
</div>
Expand Down Expand Up @@ -372,7 +376,7 @@ Object {
</div>
</div>
<span
class="sub"
class="sub error"
>
This is error
</span>
Expand Down Expand Up @@ -414,7 +418,7 @@ Object {
</div>
</div>
<span
class="sub"
class="sub error"
>
This is error
</span>
Expand Down Expand Up @@ -493,6 +497,7 @@ Object {
/>
</div>
</div>
<span
class="sub"
>
Expand All @@ -516,6 +521,7 @@ Object {
/>
</div>
</div>
<span
class="sub"
>
Expand Down Expand Up @@ -607,6 +613,7 @@ Object {
/>
</div>
</div>
</div>
</div>
</body>,
Expand Down Expand Up @@ -636,6 +643,7 @@ Object {
/>
</div>
</div>
</div>
</div>,
"debug": [Function],
Expand Down Expand Up @@ -718,6 +726,7 @@ Object {
/>
</div>
</div>
</div>
</div>
</body>,
Expand All @@ -743,6 +752,7 @@ Object {
/>
</div>
</div>
</div>
</div>,
"debug": [Function],
Expand Down Expand Up @@ -825,6 +835,7 @@ Object {
</div>
</div>
</div>
</div>
</div>
</body>,
Expand All @@ -850,6 +861,7 @@ Object {
</div>
</div>
</div>
</div>
</div>,
"debug": [Function],
Expand Down
2 changes: 1 addition & 1 deletion packages/form-control/src/index.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@

/* ERROR STATE */

.hasError .sub {
.error {
color: var(--form-control-error-color);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/input/src/Component.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { Input } from '@alfalab/core-components-input';
label={text('label', '')}
hint={text('hint', '')}
error={text('error', '')}
rightAddons={boolean('rightAddons', false) && !text('error', '') && <Icon />}
rightAddons={boolean('rightAddons', false) && !text('error') && <Icon />}
leftAddons={boolean('leftAddons', false) && <Icon />}
bottomAddons={boolean('bottomAddons', false) && <span>bottom text</span>}
/>
Expand Down
4 changes: 2 additions & 2 deletions packages/input/src/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export type InputProps = Omit<InputHTMLAttributes<HTMLInputElement>, 'size' | 't
size?: 's' | 'm' | 'l';

/**
* Текст ошибки
* Отображение ошибки
*/
error?: string;
error?: string | boolean;

/**
* Текст подсказки
Expand Down
2 changes: 2 additions & 0 deletions packages/input/src/__snapshots__/Component.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Object {
</div>
</div>
</div>
</div>
</div>
</body>,
Expand All @@ -49,6 +50,7 @@ Object {
</div>
</div>
</div>
</div>
</div>,
"debug": [Function],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Object {
</div>
</div>
</div>
</div>
</div>
</body>,
Expand All @@ -49,6 +50,7 @@ Object {
</div>
</div>
</div>
</div>
</div>,
"debug": [Function],
Expand Down
2 changes: 2 additions & 0 deletions packages/select/src/__snapshots__/Component.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Object {
</svg>
</div>
</div>
</div>
</div>
<div
Expand Down Expand Up @@ -128,6 +129,7 @@ Object {
</svg>
</div>
</div>
</div>
</div>
<div
Expand Down

0 comments on commit 1c0e494

Please sign in to comment.