Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/components/MenuItem/MenuItemAccessory.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IconLock } from '@tabler/icons-react-native'
import { memo, useMemo } from 'react'

import { View } from 'react-native'
import { View, type ColorValue } from 'react-native'

import { StyleSheet } from 'react-native-unistyles'

Expand All @@ -12,13 +12,15 @@ export interface MenuItemAccessoryProps {
readonly Icon: SvgSource
/** Неактивное состояние. Если true, заменяет Icon на иконку замка. */
readonly disabled?: boolean
/** Цвет аксессуара для текущего состояния родительского пункта меню. */
readonly color?: ColorValue
}

/**
* Аксессуар элемента меню. Выводится в крайней левой или крайней правой позиции пункта меню.
*/
export const MenuItemAccessory = memo<MenuItemAccessoryProps>(
({ Icon, disabled }) => {
({ Icon, disabled, color }) => {
const IconComponent = useMemo(
() => (disabled ? IconLock : Icon),
[Icon, disabled]
Expand All @@ -27,11 +29,12 @@ export const MenuItemAccessory = memo<MenuItemAccessoryProps>(
return (
<View style={styles.container}>
<SvgUniversal
{...styles.icon}
height={styles.icon.height}
source={IconComponent}
uniProps={({ theme }) => ({
color: theme.Menu.Item.menuitemIconColor,
color: color ?? theme.Menu.Item.menuitemIconColor,
})}
width={styles.icon.width}
/>
</View>
)
Expand Down
29 changes: 7 additions & 22 deletions src/components/MenuItem/Template/MenuItemTemplate.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ const meta: Meta<typeof MenuItemTemplate> = {
args: {
title: 'Menu Item',
Icon: undefined,
iconColor: 'rgba(0, 0, 0, 0.8000)',
badgeSeverity: undefined,
caption: 'Caption',
separator: false,
disabled: false,
selected: false,
extra: undefined,
},
argTypes: {
Expand All @@ -42,16 +42,20 @@ const meta: Meta<typeof MenuItemTemplate> = {
mapping: Icons,
},
Icon: { control: 'select', options: Object.keys(Icons), mapping: Icons },
iconColor: { control: { type: 'color' } },
badgeSeverity: {
control: 'radio',
options: ['undefined', 'basic', 'info', 'success', 'warning', 'danger'],
},
title: { control: 'text' },
separator: { control: 'boolean' },
selected: {
control: 'boolean',
description: 'Выбранный пункт меню, управляемый через selected',
},
extra: { control: 'select', options: Object.keys(Extras), mapping: Extras },
onPress: { action: 'pressed' },
},
parameters: { controls: { exclude: ['iconColor'] } },
decorators: [
(Story) => (
<View style={{ padding: 16 }}>
Expand All @@ -66,26 +70,7 @@ export default meta
type Story = StoryObj<typeof MenuItemTemplate>

const MenuItemTemplateStory: Story = {
args: { title: 'Menu item template' },
args: { title: 'MenuItemTemplate' },
argTypes: {},
parameters: {
notes: `
#### Элемент меню - полный набор свойств

<MenuItemTemplate
title="Menu item title"
caption="Menu item subtitle"
Icon={IconUser}
iconColor="#ff0000"
badgeSeverity="warning"
prefix="down"
suffix="right"
extra={IconDiamond}
separator={false}
disabled={false}
onPress={() => console.log("Menu item pressed)}
/>
`,
},
}
export { MenuItemTemplateStory as MenuItemTemplate }
187 changes: 133 additions & 54 deletions src/components/MenuItem/Template/MenuItemTemplate.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { memo, useCallback, useMemo, type ReactNode } from 'react'
import { memo, useCallback, useState, type ReactNode } from 'react'
import {
View,
Pressable,
Expand All @@ -9,7 +9,7 @@ import {
type ViewStyle,
} from 'react-native'

import { StyleSheet } from 'react-native-unistyles'
import { StyleSheet, useUnistyles } from 'react-native-unistyles'

import type { SvgSource } from '../../../utils/SvgUniversal'
import type { BadgeSeverity } from '../../Badge/Badge'
Expand All @@ -24,7 +24,10 @@ export interface MenuItemTemplateProps extends ViewProps {
caption?: string
/** SVG-иконка слева от заголовка */
Icon?: SvgSource
/** Цвет иконки. Если цвет не задан - применяется такой же цвет что и для аксессуаров (prefix, suffix) */
/**
* @deprecated Цвет основной иконки определяется состоянием пункта меню, как у аксессуаров.
* Переданное значение игнорируется.
*/
iconColor?: ColorValue
/** Цвет бейджа (точки) в правом верхнем углу иконки. Бейдж может выводиться только при наличии иконки. */
badgeSeverity?: BadgeSeverity
Expand All @@ -36,8 +39,17 @@ export interface MenuItemTemplateProps extends ViewProps {
* Дополнительный контент пункта меню, выводится справа от текста. Может быть любым react компонентом. Важно! Размеры доплолнительного контента не контролируются пунктом меню и могут его растягивать. Использовать с осторожностью.
*/
extra?: ReactNode
/** Неактивное состояние. В неактивном состоянии отключается чувствительность к нажатиям, компонент становится полупрозрачным, а аксессуары заменяются иконкой с замком*/
/**
* Отключает нажатия и визуальное выделение, применяет обычные цвета с непрозрачностью 60%.
* Аксессуары заменяются замками. Значение selected сохраняется в доступном состоянии.
*/
disabled?: boolean
/**
* Выбранное состояние пункта, по умолчанию false.
* Управляется потребителем и не меняется автоматически при нажатии.
* Имеет приоритет над accessibilityState.selected и aria-selected.
*/
selected?: boolean
/** Разделитель. Выводится как полоска сверху. Изменяет общую высоту элемента меню.*/
separator?: boolean
/** Обработчик нажатия */
Expand All @@ -51,101 +63,170 @@ export interface MenuItemTemplateProps extends ViewProps {
}

/**
* Шаблон элемента меню. Содержит максимальное количество компонентов внутри пункта меню и используется как основа для создания пунктов меню любой возможной конфигурации.
* Шаблон пункта меню с иконками, подписью и дополнительным содержимым.
* Цвета текста, основной иконки и аксессуаров задаются темой по текущему состоянию.
* Отслеживает нативный фокус и отображает выбранность из selected.
* Нажатие вызывает onPress; accessibilityState.selected формируется из selected.
*
* @link https://www.figma.com/design/4TYeki0MDLhfPGJstbIicf/UI-kit-PrimeFace-(DS)?node-id=937-6724&m=dev
* @see https://www.figma.com/design/HOLKdvQJ8jCLeX17s9d0Yf/UI-Kit--DS--v2.0?node-id=937-6724
*/
export const MenuItemTemplate = memo<MenuItemTemplateProps>(
({
title,
caption,
Icon,
iconColor,
// eslint-disable-next-line @typescript-eslint/no-deprecated -- исключаем устаревший проп из нативных props
iconColor: _iconColor,
badgeSeverity,
PrefixIcon,
SuffixIcon,
extra,
separator,
testID,
onPress,
onFocus,
onBlur,
disabled,
selected = false,
accessibilityState,
style,
...rest
}) => {
const iconStyle = useMemo(
() => ({
width: styles.icon.width,
height: styles.icon.height,
color: iconColor || styles.icon.color,
}),
[iconColor]
const { theme } = useUnistyles()
const [focused, setFocused] = useState(false)
const item = theme.theme.Menu.Item
const handleFocus = useCallback<NonNullable<ViewProps['onFocus']>>(
(event) => {
setFocused(true)
onFocus?.(event)
},
[onFocus]
)
const handleBlur = useCallback<NonNullable<ViewProps['onBlur']>>(
(event) => {
setFocused(false)
onBlur?.(event)
},
[onBlur]
)
const getState = useCallback(
(pressed: boolean): MenuItemState => {
if (disabled) return 'default'

if (selected) {
return pressed || focused ? 'selectedHighlighted' : 'selected'
}

if (pressed) return 'pressed'

return focused ? 'focused' : 'default'
},
[disabled, focused, selected]
)

const pressableStyle = useCallback(
({ pressed }: PressableStateCallbackType) => [
styles.container,
pressed && styles.containerPressed,
styles.container(getState(pressed)),
style,
disabled && styles.containerDisabled,
],
[disabled, style]
[disabled, getState, style]
)

return (
<View style={separator ? styles.separator : null}>
<Pressable
accessibilityLabel={title}
accessibilityRole='button'
accessibilityState={{
...accessibilityState,
selected,
disabled: Boolean(disabled),
}}
accessibilityValue={{ text: caption }}
disabled={disabled}
style={pressableStyle}
testID={testID || 'menuItemButton'}
onBlur={handleBlur}
onFocus={handleFocus}
onPress={onPress}
{...rest}
aria-selected={selected}
>
<View style={styles.contentContainer}>
{PrefixIcon ? (
<MenuItemAccessory Icon={PrefixIcon} disabled={disabled} />
) : null}
<View style={styles.templateContainer}>
{Icon ? (
<MenuItemIcon
Icon={Icon}
badgeSeverity={badgeSeverity}
style={iconStyle}
/>
) : null}
<View style={styles.textContainer}>
<Body size='base'>{title}</Body>
{caption ? (
<Caption color='secondary'>{caption}</Caption>
{({ pressed }) => {
const state = getState(pressed)
const stateIconColor =
item[`${tokensByState[state].color}IconColor`]
const iconStyle = {
width: styles.icon.width,
height: styles.icon.height,
color: stateIconColor,
}

return (
<View style={styles.contentContainer}>
{PrefixIcon ? (
<MenuItemAccessory
Icon={PrefixIcon}
color={stateIconColor}
disabled={disabled}
/>
) : null}
<View style={styles.templateContainer}>
{Icon ? (
<MenuItemIcon
Icon={Icon}
badgeSeverity={badgeSeverity}
style={iconStyle}
/>
) : null}
<View style={styles.textContainer}>
<Body size='base' style={styles.title(state)}>
{title}
</Body>
{caption ? (
<Caption style={styles.caption}>{caption}</Caption>
) : null}
</View>
</View>
{extra}
{SuffixIcon ? (
<MenuItemAccessory
Icon={SuffixIcon}
color={stateIconColor}
disabled={disabled}
/>
) : null}
</View>
</View>
{extra}
{SuffixIcon ? (
<MenuItemAccessory Icon={SuffixIcon} disabled={disabled} />
) : null}
</View>
)
}}
</Pressable>
</View>
)
}
)

const tokensByState = {
default: { color: 'menuitem', background: 'menuitemBg' },
pressed: { color: 'menuitemHover', background: 'menuitemHoverBg' },
focused: { color: 'menuitemFocus', background: 'menuitemFocusBg' },
selected: { color: 'menuitemActive', background: 'menuitemActiveBg' },
selectedHighlighted: {
color: 'menuitemActive',
background: 'menuitemActiveHoverBg',
},
} as const

type MenuItemState = keyof typeof tokensByState

const styles = StyleSheet.create(({ theme, spacing, typography, border }) => ({
container: {
container: (state: MenuItemState) => ({
borderColor: theme.Menu.Item.menuitemBorderColor,
borderWidth: border.Width.border,
borderRadius: theme.Menu.Item.menuitemBorderRadius,
backgroundColor: theme.Menu.Item.menuitemBg,
},
containerPressed: { backgroundColor: theme.Menu.Item.menuitemHoverBg },
containerDisabled: {
borderColor: theme.Button.Disabled.disabledButtonBorderColor,
backgroundColor: theme.Button.Disabled.disabledButtonBg,
opacity: 0.6,
},
backgroundColor: theme.Menu.Item[tokensByState[state].background],
}),
containerDisabled: { opacity: 0.6 },
separator: {
borderTopWidth: 1,
borderTopColor: theme.Menu.Overlay.overlayMenuBorderColor,
Expand All @@ -158,11 +239,10 @@ const styles = StyleSheet.create(({ theme, spacing, typography, border }) => ({
paddingHorizontal: theme.Menu.Item.menuitemPaddingLeftRight,
paddingVertical: theme.Menu.Item.menuitemPaddingTopBottom,
},
accessory: {
color: theme.Menu.Item.menuitemIconColor,
width: theme.Menu.Item.menuitemSubmenuIconFontSize,
height: theme.Menu.Item.menuitemSubmenuIconFontSize,
},
title: (state: MenuItemState) => ({
color: theme.Menu.Item[`${tokensByState[state].color}TextColor`],
}),
caption: { color: theme.Menu.Common.menuTextColor },
templateContainer: {
flexDirection: 'row',
gap: spacing.Gap['gap-2'],
Expand All @@ -171,7 +251,6 @@ const styles = StyleSheet.create(({ theme, spacing, typography, border }) => ({
icon: {
width: typography.Size['text-xl'],
height: typography.Size['text-xl'],
color: theme.Menu.Item.menuitemIconColor,
},
textContainer: { gap: spacing.Gap['gap-1'], flex: 1 },
}))
Loading
Loading