【问题标题】:underline the clicked text element下划线单击的文本元素
【发布时间】:2021-01-22 15:07:10
【问题描述】:

我有一个文本元素列表,我想在单击时添加下划线。如果我将文本装饰添加到tabText,那么显然它会应用于所有项目。如何确保当我单击另一个选项卡时,前一个选项卡的下划线被删除?

有什么方法可以在单击项目时从样式元素中添加或删除项目?

//onPress={() => {}}>
const tabs = [
  {
    id: 1,
    text: 'Alle List',
  },
  {
    id: 2,
    text: 'Second',
  },
];
export const Tab: React.FunctionComponent = () => {
  return (
    <View style={styles.tabView}>
      {tabs.map((item: any) => (
        <View>
          <Text style={styles.tabText}>{item.text}</Text>
        </View>
      ))}
    </View>
  );
};

const styles = StyleSheet.create({
  tabView: {
    paddingTop: moderateScale(15),
    paddingLeft: moderateScale(20),
    paddingRight: moderateScale(20),
    flexDirection: 'row',
    justifyContent: 'space-between',
  },
  tabText: {
    color: 'white',
    paddingBottom: moderateScale(10),
    //textDecorationLine: 'underline'
  },
});

Codesandbox(也将 tabText 项作为数组):

https://snack.expo.io/@nhammad/shallow-watermelon

【问题讨论】:

    标签: javascript css reactjs typescript react-native


    【解决方案1】:

    您可以使用 useState 保存选定的索引,然后根据选定的索引应用另一种样式。这是对您的脚本的快速修改,小吃链接在最后。

    import * as React from 'react';
    import { Text, View, StyleSheet, TouchableOpacity } from 'react-native';
    
    const tabs = [
      {
        id: 1,
        text: 'All Friends',
      },
      {
        id: 2,
        text: 'Second',
      },
      {
        id: 3,
        text: 'Third',
      },
    ];
    export default function App() {
      const [selected, setSelected] = React.useState(null)
      return (
        <View style={styles.container}>
          <View style={styles.tabView}>
            {tabs.map((item: any, index) => (
              <TouchableOpacity onPress={()=>setSelected(index)}>
              <View>
                <Text style={[styles.tabText,selected===index?styles.selected:null]}>{item.text}</Text>
              </View>
              </TouchableOpacity>
            ))}
          </View>
        </View>
      );
    }
    
    const styles = StyleSheet.create({
      tabView: {
        paddingTop: 15,
        paddingLeft: 20,
        paddingRight: 20,
        flexDirection: 'row',
        justifyContent: 'space-between',
      },
      tabText: {
        color: 'black',
        paddingBottom: 10,
      },
      selected: {
        textDecorationLine: 'underline'
      },
    });
    

    https://snack.expo.io/@saachitech/da6280

    【讨论】:

    • 是否也可以设置下划线的样式?例如,我为它添加了一种颜色,但我想增加它的高度,或者可能增加下划线和文本之间的距离。 textDecorationColor: 'green,
    • 也许使用textUnderlinePosition: 'under',这里有更多信息css-tricks.com/almanac/properties/t/text-underline-position
    • 更新了带有边框底部样式的小吃
    【解决方案2】:

    您可以根据活动路线为标签添加不同的样式

    <Text style={[(this.props.activeRouteName == route.routeName) ? styles.tabActiveText: styles.tabText]}>{item.text}</Text>    
    
    And then in the styles
    
    tabActiveText: {
        color: 'white',
        paddingBottom: moderateScale(10),
        textDecorationLine: 'underline'
    }
    

    【讨论】:

    • 感谢您的意见。现在没有“activeRoute”。这只是纯文本。而且没有这个。属性在这里要么因为我不使用类
    猜你喜欢
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    相关资源
    最近更新 更多