xref: /MusicFree/src/components/base/iconButton.tsx (revision 26f326364ffffc501faab6b3f1563421273758ae)
119dc08ecS猫头猫import React from 'react';
25589cdf3S猫头猫import {ColorKey, colorMap, iconSizeConst} from '@/constants/uiConst';
3aa1d5f71S猫头猫import {TapGestureHandler} from 'react-native-gesture-handler';
4*26f32636S猫头猫import {StyleSheet, View} from 'react-native';
5e650bfb3S猫头猫import useColors from '@/hooks/useColors';
65589cdf3S猫头猫import {SvgProps} from 'react-native-svg';
75589cdf3S猫头猫import Icon, {IIconName} from '@/components/base/icon.tsx';
819dc08ecS猫头猫
95589cdf3S猫头猫interface IIconButtonProps extends SvgProps {
105589cdf3S猫头猫    name: IIconName;
115589cdf3S猫头猫    style?: SvgProps['style'];
12e650bfb3S猫头猫    sizeType?: keyof typeof iconSizeConst;
1319dc08ecS猫头猫    fontColor?: ColorKey;
14e650bfb3S猫头猫    color?: string;
1519dc08ecS猫头猫    onPress?: () => void;
169dc4d3f3S猫头猫    accessibilityLabel?: string;
1719dc08ecS猫头猫}
18caaa811bS猫头猫export function IconButtonWithGesture(props: IIconButtonProps) {
199dc4d3f3S猫头猫    const {
209dc4d3f3S猫头猫        name,
21e650bfb3S猫头猫        sizeType: size = 'normal',
229dc4d3f3S猫头猫        fontColor = 'normal',
239dc4d3f3S猫头猫        onPress,
249dc4d3f3S猫头猫        style,
259dc4d3f3S猫头猫        accessibilityLabel,
269dc4d3f3S猫头猫    } = props;
27e650bfb3S猫头猫    const colors = useColors();
2819dc08ecS猫头猫    const textSize = iconSizeConst[size];
29e650bfb3S猫头猫    const color = colors[colorMap[fontColor]];
3019dc08ecS猫头猫    return (
31da0ff60bS猫头猫        <TapGestureHandler onActivated={onPress}>
32*26f32636S猫头猫            <View>
3319dc08ecS猫头猫                <Icon
349dc4d3f3S猫头猫                    accessible
359dc4d3f3S猫头猫                    accessibilityLabel={accessibilityLabel}
3619dc08ecS猫头猫                    name={name}
3719dc08ecS猫头猫                    color={color}
384060c00aS猫头猫                    style={[{minWidth: textSize}, styles.textCenter, style]}
394060c00aS猫头猫                    size={textSize}
404060c00aS猫头猫                />
41*26f32636S猫头猫            </View>
42da0ff60bS猫头猫        </TapGestureHandler>
4319dc08ecS猫头猫    );
4419dc08ecS猫头猫}
45caaa811bS猫头猫
46caaa811bS猫头猫export default function IconButton(props: IIconButtonProps) {
47e650bfb3S猫头猫    const {sizeType = 'normal', fontColor = 'normal', style, color} = props;
48e650bfb3S猫头猫    const colors = useColors();
49e650bfb3S猫头猫    const size = iconSizeConst[sizeType];
50e650bfb3S猫头猫
51caaa811bS猫头猫    return (
52caaa811bS猫头猫        <Icon
53e650bfb3S猫头猫            {...props}
54e650bfb3S猫头猫            color={color ?? colors[colorMap[fontColor]]}
55e650bfb3S猫头猫            style={[{minWidth: size}, styles.textCenter, style]}
56e650bfb3S猫头猫            size={size}
574060c00aS猫头猫        />
58caaa811bS猫头猫    );
59caaa811bS猫头猫}
604060c00aS猫头猫
614060c00aS猫头猫const styles = StyleSheet.create({
624060c00aS猫头猫    textCenter: {
634060c00aS猫头猫        height: '100%',
644060c00aS猫头猫        textAlignVertical: 'center',
654060c00aS猫头猫    },
664060c00aS猫头猫});
67