Skip to content

Commit

Permalink
refactor(eslint): add no-object-in-class-names (#5649)
Browse files Browse the repository at this point in the history
  • Loading branch information
SevereCloud authored Aug 22, 2023
1 parent ba603f8 commit edcc8bb
Show file tree
Hide file tree
Showing 30 changed files with 283 additions and 252 deletions.
4 changes: 4 additions & 0 deletions packages/vkui/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ module.exports = {
],
'no-restricted-syntax': [
'error',
{
selector: 'CallExpression[callee.name="classNames"] ObjectExpression',
message: 'Необходимо вынести объект на уровень модуля как константу',
},
{
selector: 'ImportDeclaration[source.value=/^\\W+(index(\\.ts)?)?$/i]',
message: 'Do not import index',
Expand Down
17 changes: 6 additions & 11 deletions packages/vkui/src/components/Badge/Badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import * as React from 'react';
import { classNames } from '@vkontakte/vkjs';
import styles from './Badge.module.css';

const stylesMode = {
new: styles['Badge--mode-new'],
prominent: styles['Badge--mode-prominent'],
};

export interface BadgeProps extends React.HTMLAttributes<HTMLElement> {
mode: 'new' | 'prominent';
}
Expand All @@ -10,15 +15,5 @@ export interface BadgeProps extends React.HTMLAttributes<HTMLElement> {
* @see https://vkcom.github.io/VKUI/#/Badge
*/
export const Badge = ({ mode = 'new', className, ...restProps }: BadgeProps) => (
<span
className={classNames(
styles['Badge'],
{
new: styles['Badge--mode-new'],
prominent: styles['Badge--mode-prominent'],
}[mode],
className,
)}
{...restProps}
/>
<span className={classNames(styles['Badge'], stylesMode[mode], className)} {...restProps} />
);
10 changes: 6 additions & 4 deletions packages/vkui/src/components/Banner/Banner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import { Text } from '../Typography/Text/Text';
import { Title } from '../Typography/Title/Title';
import styles from './Banner.module.css';

const stylesSize = {
s: styles['Banner--size-s'],
m: styles['Banner--size-m'],
};

export interface BannerProps extends React.HTMLAttributes<HTMLDivElement>, HasRootRef<HTMLElement> {
/**
* Тип баннера.
Expand Down Expand Up @@ -153,10 +158,7 @@ export const Banner = ({
!noPadding && styles['Banner--withPadding'],
platform === Platform.IOS && styles['Banner--ios'],
mode === 'image' && styles['Banner--mode-image'],
{
s: styles['Banner--size-s'],
m: styles['Banner--size-m'],
}[size],
stylesSize[size],
mode === 'image' && imageTheme === 'dark' && styles['Banner--inverted'],
className,
)}
Expand Down
12 changes: 5 additions & 7 deletions packages/vkui/src/components/BaseGallery/BaseGallery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const SHIFT_DEFAULT_STATE = {
indent: 0,
};

const stylesBullets = {
dark: styles['BaseGallery__bullets--dark'],
light: styles['BaseGallery__bullets--light'],
};
export const BaseGallery = ({
bullets = false,
getRootRef,
Expand Down Expand Up @@ -343,13 +347,7 @@ export const BaseGallery = ({
{bullets && (
<div
aria-hidden
className={classNames(
styles['BaseGallery__bullets'],
{
dark: styles['BaseGallery__bullets--dark'],
light: styles['BaseGallery__bullets--light'],
}[bullets],
)}
className={classNames(styles['BaseGallery__bullets'], stylesBullets[bullets])}
>
{React.Children.map(children, (_item: React.ReactNode, index: number) => (
<div
Expand Down
58 changes: 33 additions & 25 deletions packages/vkui/src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,35 @@ import { Tappable, TappableProps } from '../Tappable/Tappable';
import '../Spinner/Spinner.module.css';
import styles from './Button.module.css';

const stylesSize = {
s: styles['Button--size-s'],
m: styles['Button--size-m'],
l: styles['Button--size-l'],
};

const stylesMode = {
primary: styles['Button--mode-primary'],
secondary: styles['Button--mode-secondary'],
tertiary: styles['Button--mode-tertiary'],
outline: styles['Button--mode-outline'],
link: styles['Button--mode-link'],
};

const stylesAppearance = {
'accent': styles['Button--appearance-accent'],
'positive': styles['Button--appearance-positive'],
'negative': styles['Button--appearance-negative'],
'neutral': styles['Button--appearance-neutral'],
'overlay': styles['Button--appearance-overlay'],
'accent-invariable': styles['Button--appearance-accent-invariable'],
};

const stylesAlign = {
left: styles['Button--align-left'],
center: styles['Button--align-center'],
right: styles['Button--align-right'],
};

const sizeYClassNames = {
none: styles['Button--sizeY-none'],
[SizeType.REGULAR]: styles['Button--sizeY-regular'],
Expand Down Expand Up @@ -65,31 +94,10 @@ export const Button = ({
className={classNames(
className,
styles.Button,
{
s: styles['Button--size-s'],
m: styles['Button--size-m'],
l: styles['Button--size-l'],
}[size],
{
primary: styles['Button--mode-primary'],
secondary: styles['Button--mode-secondary'],
tertiary: styles['Button--mode-tertiary'],
outline: styles['Button--mode-outline'],
link: styles['Button--mode-link'],
}[mode],
{
'accent': styles['Button--appearance-accent'],
'positive': styles['Button--appearance-positive'],
'negative': styles['Button--appearance-negative'],
'neutral': styles['Button--appearance-neutral'],
'overlay': styles['Button--appearance-overlay'],
'accent-invariable': styles['Button--appearance-accent-invariable'],
}[appearance],
{
left: styles['Button--align-left'],
center: styles['Button--align-center'],
right: styles['Button--align-right'],
}[align],
stylesSize[size],
stylesMode[mode],
stylesAppearance[appearance],
stylesAlign[align],
sizeY !== SizeType.COMPACT && sizeYClassNames[sizeY],
platform === Platform.IOS && styles['Button--ios'],
stretched && styles['Button--stretched'],
Expand Down
35 changes: 20 additions & 15 deletions packages/vkui/src/components/ButtonGroup/ButtonGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ import { classNames } from '@vkontakte/vkjs';
import type { AlignType, HasRootRef } from '../../types';
import styles from './ButtonGroup.module.css';

const stylesMode = {
vertical: styles['ButtonGroup--mode-vertical'],
horizontal: styles['ButtonGroup--mode-horizontal'],
};

const stylesGap = {
space: styles['ButtonGroup--gap-space'],
s: styles['ButtonGroup--gap-s'],
m: styles['ButtonGroup--gap-m'],
};

const stylesAlign = {
left: styles['ButtonGroup--align-left'],
center: styles['ButtonGroup--align-center'],
right: styles['ButtonGroup--align-right'],
};

export interface ButtonGroupProps
extends React.HTMLAttributes<HTMLDivElement>,
HasRootRef<HTMLDivElement> {
Expand Down Expand Up @@ -44,22 +61,10 @@ export const ButtonGroup = ({
className={classNames(
className,
styles.ButtonGroup,
{
vertical: styles['ButtonGroup--mode-vertical'],
horizontal: styles['ButtonGroup--mode-horizontal'],
}[mode],
gap !== 'none' &&
{
space: styles['ButtonGroup--gap-space'],
s: styles['ButtonGroup--gap-s'],
m: styles['ButtonGroup--gap-m'],
}[gap],
stylesMode[mode],
gap !== 'none' && stylesGap[gap],
stretched && styles['ButtonGroup--stretched'],
{
left: styles['ButtonGroup--align-left'],
center: styles['ButtonGroup--align-center'],
right: styles['ButtonGroup--align-right'],
}[align],
stylesAlign[align],
)}
role="group"
ref={getRootRef}
Expand Down
12 changes: 7 additions & 5 deletions packages/vkui/src/components/CardGrid/CardGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ const sizeXClassNames = {
[SizeType.COMPACT]: styles['CardGrid--sizeX-compact'],
};

const stylesSize = {
s: 'vkuiInternalCardGrid--size-s',
m: 'vkuiInternalCardGrid--size-m',
l: 'vkuiInternalCardGrid--size-l',
};

export interface CardGridProps extends React.HTMLAttributes<HTMLDivElement> {
size?: 's' | 'm' | 'l';
/**
Expand Down Expand Up @@ -36,11 +42,7 @@ export const CardGrid = ({
styles['CardGrid'],
'vkuiInternalCardGrid',
spaced && styles['CardGrid--spaced'],
{
s: 'vkuiInternalCardGrid--size-s',
m: 'vkuiInternalCardGrid--size-m',
l: 'vkuiInternalCardGrid--size-l',
}[size],
stylesSize[size],
sizeX !== SizeType.REGULAR && sizeXClassNames[sizeX],
className,
)}
Expand Down
13 changes: 7 additions & 6 deletions packages/vkui/src/components/CardScroll/CardScroll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { useDOM } from '../../lib/dom';
import { HorizontalScroll, HorizontalScrollProps } from '../HorizontalScroll/HorizontalScroll';
import styles from './CardScroll.module.css';

const stylesSize = {
s: 'vkuiInternalCardScroll--size-s',
m: 'vkuiInternalCardScroll--size-m',
l: 'vkuiInternalCardScroll--size-l',
};

export interface CardScrollProps extends React.HTMLAttributes<HTMLDivElement> {
/**
* При `size=false` ширина `Card` будет регулироваться контентом внутри. В остальных случаях — будет явно задана в процентах.
Expand Down Expand Up @@ -87,12 +93,7 @@ export const CardScroll = ({
className={classNames(
styles['CardScroll'],
'vkuiInternalCardScroll',
size !== false &&
{
s: 'vkuiInternalCardScroll--size-s',
m: 'vkuiInternalCardScroll--size-m',
l: 'vkuiInternalCardScroll--size-l',
}[size],
size !== false && stylesSize[size],
withSpaces && styles['CardScroll--withSpaces'],
className,
)}
Expand Down
17 changes: 9 additions & 8 deletions packages/vkui/src/components/FixedLayout/FixedLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import { SplitColContext } from '../SplitCol/SplitColContext';
import { TooltipContainer } from '../Tooltip/TooltipContainer';
import styles from './FixedLayout.module.css';

const stylesVertical = {
top: styles['FixedLayout--vertical-top'],
bottom: classNames(
styles['FixedLayout--vertical-bottom'],
'vkuiInternalFixedLayout--vertical-bottom',
),
};

export interface FixedLayoutProps
extends React.HTMLAttributes<HTMLDivElement>,
HasRootRef<HTMLDivElement>,
Expand Down Expand Up @@ -84,14 +92,7 @@ export const FixedLayout = ({
styles['FixedLayout'],
platform === Platform.IOS && 'vkuiInternalFixedLayout--ios',
filled && styles['FixedLayout--filled'],
vertical &&
{
top: styles['FixedLayout--vertical-top'],
bottom: classNames(
styles['FixedLayout--vertical-bottom'],
'vkuiInternalFixedLayout--vertical-bottom',
),
}[vertical],
vertical && stylesVertical[vertical],
className,
)}
style={{ ...style, width }}
Expand Down
12 changes: 7 additions & 5 deletions packages/vkui/src/components/FocusVisible/FocusVisible.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import * as React from 'react';
import { classNames } from '@vkontakte/vkjs';
import styles from './FocusVisible.module.css';

const stylesMode = {
inside: styles['FocusVisible--mode-inside'],
outside: styles['FocusVisible--mode-outside'],
outline: styles['FocusVisible--mode-outline'],
};

export type FocusVisibleMode = 'inside' | 'outside' | 'outline';

export interface FocusVisibleProps {
Expand All @@ -21,11 +27,7 @@ export const FocusVisible = ({ visible, mode, thin, ...restProps }: FocusVisible
styles['FocusVisible'],
visible && styles['FocusVisible--visible'],
thin && styles['FocusVisible--thin'],
{
inside: styles['FocusVisible--mode-inside'],
outside: styles['FocusVisible--mode-outside'],
outline: styles['FocusVisible--mode-outline'],
}[mode],
stylesMode[mode],
)}
/>
);
11 changes: 6 additions & 5 deletions packages/vkui/src/components/FormField/FormField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ const sizeYClassNames = {
[SizeType.COMPACT]: styles['FormField--sizeY-compact'],
};

const stylesStatus = {
error: styles['FormField--status-error'],
valid: styles['FormField--status-valid'],
};

export interface FormFieldProps {
status?: 'default' | 'error' | 'valid';
/**
Expand Down Expand Up @@ -83,11 +88,7 @@ export const FormField = ({
className={classNames(
styles['FormField'],
mode === 'default' && styles['FormField--mode-default'],
status !== 'default' &&
{
error: styles['FormField--status-error'],
valid: styles['FormField--status-valid'],
}[status],
status !== 'default' && stylesStatus[status],
sizeY !== SizeType.REGULAR && sizeYClassNames[sizeY],
disabled && styles['FormField--disabled'],
!disabled && hover && styles['FormField--hover'],
Expand Down
17 changes: 6 additions & 11 deletions packages/vkui/src/components/FormItem/FormItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ const sizeYClassNames = {
),
};

const stylesStatus = {
error: classNames(styles['FormItem--status-error'], 'vkuiInternalFormItem--status-error'),
valid: classNames(styles['FormItem--status-valid'], 'vkuiInternalFormItem--status-valid'),
};

export interface FormItemProps
extends React.AllHTMLAttributes<HTMLElement>,
HasRootRef<HTMLElement>,
Expand Down Expand Up @@ -98,17 +103,7 @@ export const FormItem = ({
styles['FormItem'],
!noPadding && styles['FormItem--withPadding'],
'vkuiInternalFormItem',
status !== 'default' &&
{
error: classNames(
styles['FormItem--status-error'],
'vkuiInternalFormItem--status-error',
),
valid: classNames(
styles['FormItem--status-valid'],
'vkuiInternalFormItem--status-valid',
),
}[status],
status !== 'default' && stylesStatus[status],
sizeY !== SizeType.REGULAR && sizeYClassNames[sizeY],
hasReactNode(top) &&
classNames(styles['FormItem--withTop'], 'vkuiInternalFormItem--withTop'),
Expand Down
Loading

0 comments on commit edcc8bb

Please sign in to comment.