Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add star-ds library and FocusableWrapper component #11

Merged
merged 6 commits into from
Sep 26, 2024
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
16 changes: 11 additions & 5 deletions core/core-components/src/components/Button/Button.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getWidthSize, getRoundnessMatrix, Pin } from './utils';

export interface Style {
root?: ViewStyle;
container?: ViewStyle;
wrapper?: ViewStyle;
loader?: ViewStyle;
text?: TextStyle;
Expand All @@ -18,6 +19,7 @@ export const getStyle = (
disabledOpacity?: number,
viewStyle?: ButtonConfig['variations']['view'][string],
sizeStyle?: ButtonConfig['variations']['size'][string],
pressed?: boolean,
stretching?: Stretching,
pin?: Pin,
isLoading?: boolean,
Expand All @@ -26,6 +28,7 @@ export const getStyle = (
if (!viewStyle || !sizeStyle || !pin || !theme) {
return {
root: {},
container: {},
wrapper: {},
loader: {},
text: {},
Expand All @@ -37,19 +40,22 @@ export const getStyle = (
const fontFace = getFontFace(sizeStyle.fontFamilyRef, sizeStyle.fontStyle, sizeStyle.fontWeight, theme);

return StyleSheet.create({
root: (({ pressed }: { pressed: boolean }) => ({
root: {
opacity: disabledOpacity,
...externalStyle?.root,
},
container: {
alignItems: 'center',
justifyContent: 'center',
height: sizeStyle.height,
backgroundColor: pressed ? viewStyle.backgroundColorActive : viewStyle.backgroundColor,
borderRadius: sizeStyle.radius,
paddingLeft: sizeStyle.padding,
paddingRight: sizeStyle.padding,
backgroundColor: pressed ? viewStyle.backgroundColorActive : viewStyle.backgroundColor,
height: sizeStyle.height,
...widthSize,
...buttonBorderRadius,
...externalStyle?.root,
})) as ViewStyle,
...externalStyle?.container,
},
wrapper: {
width: '100%',
gap: sizeStyle.contentGap,
Expand Down
115 changes: 93 additions & 22 deletions core/core-components/src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Pressable, Text, View } from 'react-native';
import { useMemo } from 'react';
import { NativeSyntheticEvent, Platform, TargetedEvent, Text, View } from 'react-native';
import { useMemo, useState } from 'react';

import { spinnerComponent } from '../Spinner';
import { Theme, withTheme } from '../ThemeProvider';
import { PropsType } from '../../types';
import { FocusableWrapper } from '../FocusableWrapper/FocusableWrapper';

import { ButtonConfig, ButtonProps } from './Button.types';
import { getStyle } from './Button.styles';
Expand All @@ -12,6 +13,7 @@ const Spinner = spinnerComponent();

export const buttonCore = <T extends ButtonConfig>(config?: T, theme?: Theme) => (
props: ButtonProps & PropsType<T['variations']>,
externalRef: React.ForwardedRef<View>,
) => {
const {
view = '',
Expand All @@ -25,40 +27,109 @@ export const buttonCore = <T extends ButtonConfig>(config?: T, theme?: Theme) =>
isLoading,
loader,
disabled,
// focused, TODO: Придумать общую реализацию фокусной рамки,
style: externalStyle,
onBlur,
onFocus,
//
focusable,
hasTVPreferredFocus,
nextFocusDown,
nextFocusForward,
nextFocusLeft,
nextFocusRight,
nextFocusUp,
...rest
} = props;

const [focused, setFocused] = useState(false);
const [pressed, setPressed] = useState(false);

const txt = typeof children === 'string' ? children : text;

const viewStyle = config?.variations.view[view];
const sizeStyle = config?.variations.size[size];
const disabledOpacity = disabled ? config?.variations.disabled.true.disabledOpacity : 1;

const style = useMemo(
() => getStyle(theme, disabledOpacity, viewStyle, sizeStyle, stretching, pin, isLoading, externalStyle),
[view, size, disabled, stretching, pin, theme?.mode],
() =>
getStyle(theme, disabledOpacity, viewStyle, sizeStyle, pressed, stretching, pin, isLoading, externalStyle),
[view, size, pressed, disabled, stretching, pin, theme?.mode],
);

const navigationProps = {
focusable,
hasTVPreferredFocus,
nextFocusDown,
nextFocusForward,
nextFocusLeft,
nextFocusRight,
nextFocusUp,
};

const onWrapperFocus = (event: NativeSyntheticEvent<TargetedEvent>) => {
if (onFocus) {
onFocus(event);
}

setFocused(true);
};

const onWrapperBlur = (event: NativeSyntheticEvent<TargetedEvent>) => {
if (onBlur) {
onBlur(event);
}

setFocused(false);
};

const onPressIn = () => {
setPressed(true);
};

const onPressOut = () => {
setPressed(false);
};

return (
<Pressable style={style.root} disabled={disabled} {...rest}>
<View style={style.wrapper}>
{contentLeft}
{txt ? <Text style={style.text}>{txt}</Text> : children}
{contentRight}
</View>
{isLoading && (
<View style={style.loader}>
{loader || (
<Spinner
color={viewStyle?.color}
height={sizeStyle?.spinnerSize}
width={sizeStyle?.spinnerSize}
/>
)}
<FocusableWrapper
style={{
root: style.root,
focus: {
borderColor: theme?.data.color[theme?.mode].textPrimary,
borderRadius: sizeStyle?.radius,
borderWidth: 2,
},
}}
hasFocus={Platform.isTV}
focused={focused}
disabled={disabled}
ref={externalRef}
onFocus={onWrapperFocus}
onBlur={onWrapperBlur}
onPressIn={onPressIn}
onPressOut={onPressOut}
{...navigationProps}
{...rest}
>
<View style={style.container}>
<View style={style.wrapper}>
{contentLeft}
{txt ? <Text style={style.text}>{txt}</Text> : children}
{contentRight}
</View>
)}
</Pressable>
{isLoading && (
<View style={style.loader}>
{loader || (
<Spinner
color={viewStyle?.color}
height={sizeStyle?.spinnerSize}
width={sizeStyle?.spinnerSize}
/>
)}
</View>
)}
</View>
</FocusableWrapper>
);
};

Expand Down
14 changes: 3 additions & 11 deletions core/core-components/src/components/Button/Button.types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { ButtonProps as ButtonPropsNative } from 'react-native';
import { ReactNode } from 'react';

import { Focusable } from '../FocusableWrapper';

import { Pin } from './utils';
import { Style } from './Button.styles';

export type Stretching = 'fixed' | 'filled' | 'auto';

interface CustomButtonProps extends Omit<ButtonPropsNative, 'title'> {
interface CustomButtonProps extends Omit<ButtonPropsNative, 'onPress' | 'title'>, Focusable {
/**
* Объект для стилизации компонента
*/
Expand Down Expand Up @@ -44,10 +46,6 @@ interface CustomButtonProps extends Omit<ButtonPropsNative, 'title'> {
* auto - кнопка растягивается в зависимости от контента
*/
stretching?: Stretching;
/**
* Может ли фокусироваться кнопка
*/
focused?: boolean;
/**
* Кнопка неактивна
*/
Expand All @@ -72,7 +70,6 @@ export interface ButtonConfig {
defaults: {
view: string;
size: string;
focused: string;
};
variations: {
view: {
Expand Down Expand Up @@ -105,10 +102,5 @@ export interface ButtonConfig {
disabledOpacity: number;
};
};
focused: {
[x: string]: {
focusColor: string;
};
};
};
}
74 changes: 68 additions & 6 deletions core/core-components/src/components/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { Pressable, Text, View } from 'react-native';
import { useMemo } from 'react';
import { GestureResponderEvent, NativeSyntheticEvent, Platform, TargetedEvent, Text, View } from 'react-native';
import { useMemo, useState } from 'react';

import { Theme, withTheme } from '../ThemeProvider';
import { PropsType } from '../../types';
import { FocusableWrapper } from '../FocusableWrapper';

import { CheckboxConfig, CheckboxProps } from './Checkbox.types';
import { getStyle } from './Checkbox.styles';
import { Done, Indeterminate } from './IconsSvg';

export const checkboxCore = <T extends CheckboxConfig>(config?: T, theme?: Theme) => (
props: CheckboxProps & PropsType<T['variations']>,
externalRef: React.ForwardedRef<View>,
) => {
const {
view = '',
Expand All @@ -22,10 +24,22 @@ export const checkboxCore = <T extends CheckboxConfig>(config?: T, theme?: Theme
checked = false,
style: externalStyle,
onValueChange,
// focused, TODO: Придумать общую реализацию фокусной рамки
onPress,
onBlur,
onFocus,
//
focusable,
hasTVPreferredFocus,
nextFocusDown,
nextFocusForward,
nextFocusLeft,
nextFocusRight,
nextFocusUp,
...rest
} = props;

const [focused, setFocused] = useState(false);

const viewStyle = config?.variations.view[view];
const sizeStyle = config?.variations.size[size];
const disabledOpacity = disabled ? config?.variations.disabled.true.disabledOpacity : 1;
Expand All @@ -49,14 +63,62 @@ export const checkboxCore = <T extends CheckboxConfig>(config?: T, theme?: Theme
[view, size, label, checked, indeterminate, description, disabled, theme?.mode],
);

const onPress = () => {
const navigationProps = {
focusable,
hasTVPreferredFocus,
nextFocusDown,
nextFocusForward,
nextFocusLeft,
nextFocusRight,
nextFocusUp,
};

const onWrapperPress = (event: GestureResponderEvent) => {
if (onValueChange) {
onValueChange(checked);
}

if (onPress) {
onPress(event);
}
};

const onWrapperFocus = (event: NativeSyntheticEvent<TargetedEvent>) => {
if (onFocus) {
onFocus(event);
}

setFocused(true);
};

const onWrapperBlur = (event: NativeSyntheticEvent<TargetedEvent>) => {
if (onBlur) {
onBlur(event);
}

setFocused(false);
};

return (
<Pressable style={style.root} disabled={disabled} onPress={onPress} {...rest}>
<FocusableWrapper
style={{
root: style.root,
focus: {
borderColor: theme?.data.color[theme?.mode].textPrimary,
borderRadius: sizeStyle?.triggerBorderRadius,
borderWidth: 2,
},
}}
hasFocus={Platform.isTV}
focused={focused}
disabled={disabled}
ref={externalRef}
onFocus={onWrapperFocus}
onBlur={onWrapperBlur}
onPress={onWrapperPress}
{...navigationProps}
{...rest}
>
<View style={style.wrapper}>
<View style={style.trigger}>
{indeterminate ? (
Expand All @@ -80,7 +142,7 @@ export const checkboxCore = <T extends CheckboxConfig>(config?: T, theme?: Theme
</View>
)}
</View>
</Pressable>
</FocusableWrapper>
);
};

Expand Down
Loading