Skip to content

Commit

Permalink
feat(core-components-form-control): allow to hide error icon in theme
Browse files Browse the repository at this point in the history
  • Loading branch information
reme3d2y committed Jun 17, 2020
1 parent 3b134dc commit c19a73c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 34 deletions.
32 changes: 13 additions & 19 deletions packages/form-control/src/Component.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ReactNode, useCallback, HTMLAttributes } from 'react';
import React, { ReactNode, HTMLAttributes } from 'react';
import cn from 'classnames';
import ErrorIcon from '@alfalab/icons-classic/ErrorMColorIcon';

Expand Down Expand Up @@ -99,22 +99,6 @@ export const FormControl = ({
dataTestId,
...restProps
}: FormControlProps) => {
const rightAddonsRenderer = useCallback(
() =>
(error || rightAddons) && (
<div className={cn(styles.addons)}>
{error && <ErrorIcon />}
{rightAddons}
</div>
),
[error, rightAddons],
);

const leftAddonsRenderer = useCallback(
() => leftAddons && <div className={styles.addons}>{leftAddons}</div>,
[leftAddons],
);

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

return (
Expand All @@ -138,7 +122,9 @@ export const FormControl = ({
{...restProps}
>
<div className={styles.inner}>
{leftAddonsRenderer()}
{leftAddons && (
<div className={cn(styles.addons, styles.leftAddons)}>{leftAddons}</div>
)}

<div className={styles.inputWrapper}>
{label && (
Expand All @@ -150,7 +136,15 @@ export const FormControl = ({
<div className={styles.input}>{children}</div>
</div>

{rightAddonsRenderer()}
{rightAddons && (
<div className={cn(styles.addons, styles.rightAddons)}>{rightAddons}</div>
)}

{error && (
<div className={styles.errorIcon}>
<ErrorIcon />
</div>
)}
</div>

{errorMessage && <span className={cn(styles.sub, styles.error)}>{errorMessage}</span>}
Expand Down
16 changes: 8 additions & 8 deletions packages/form-control/src/__snapshots__/Component.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Object {
/>
</div>
<div
class="addons"
class="errorIcon"
>
<svg
focusable="false"
Expand Down Expand Up @@ -156,7 +156,7 @@ Object {
/>
</div>
<div
class="addons"
class="errorIcon"
>
<svg
focusable="false"
Expand Down Expand Up @@ -356,7 +356,7 @@ Object {
/>
</div>
<div
class="addons"
class="errorIcon"
>
<svg
focusable="false"
Expand Down Expand Up @@ -398,7 +398,7 @@ Object {
/>
</div>
<div
class="addons"
class="errorIcon"
>
<svg
focusable="false"
Expand Down Expand Up @@ -712,7 +712,7 @@ Object {
class="inner"
>
<div
class="addons"
class="addons leftAddons"
>
<div>
Left addons
Expand All @@ -738,7 +738,7 @@ Object {
class="inner"
>
<div
class="addons"
class="addons leftAddons"
>
<div>
Left addons
Expand Down Expand Up @@ -828,7 +828,7 @@ Object {
/>
</div>
<div
class="addons"
class="addons rightAddons"
>
<div>
Right addons
Expand All @@ -854,7 +854,7 @@ Object {
/>
</div>
<div
class="addons"
class="addons rightAddons"
>
<div>
Right addons
Expand Down
18 changes: 13 additions & 5 deletions packages/form-control/src/index.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
--form-control-error-color: var(--color-red-brand-85-flat);
--form-control-error-shadow: inset 0 -1px var(--form-control-error-color);
--form-control-error-border-bottom: 1px solid var(--form-control-error-color);
--form-control-error-icon-display: flex;
}

.component {
Expand Down Expand Up @@ -89,17 +90,19 @@
white-space: nowrap;
}

.addons {
.addons,
.errorIcon {
display: flex;
flex-shrink: 0;
align-items: center;
}

.addons:first-child {
.leftAddons {
padding-left: var(--gap-s);
}

.addons:last-child {
.rightAddons,
.errorIcon {
padding-right: var(--gap-s);
}

Expand Down Expand Up @@ -149,11 +152,12 @@
padding: var(--form-control-labeled-l-paddings);
}

.l .addons:first-child {
.l .leftAddons {
padding-left: var(--gap-m);
}

.l .addons:last-child {
.l .rightAddons,
.l .errorIcon {
padding-right: var(--gap-m);
}

Expand Down Expand Up @@ -213,3 +217,7 @@
box-shadow: var(--form-control-error-shadow);
border-bottom: var(--form-control-error-border-bottom);
}

.errorIcon {
display: var(--form-control-error-icon-display);
}
4 changes: 2 additions & 2 deletions packages/select/src/__snapshots__/Component.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Object {
</div>
</div>
<div
class="addons"
class="addons rightAddons"
>
<svg
class="arrow"
Expand Down Expand Up @@ -104,7 +104,7 @@ Object {
</div>
</div>
<div
class="addons"
class="addons rightAddons"
>
<svg
class="arrow"
Expand Down
1 change: 1 addition & 0 deletions packages/themes/src/mixins/form-control/click.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@
/* error */
--form-control-error-shadow: inset 0 0 0 1px var(--form-control-error-color);
--form-control-error-border-bottom: 0;
--form-control-error-icon-display: none;
}

0 comments on commit c19a73c

Please sign in to comment.