0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

React Native Paperで困ったところ ①List.Accordionをカスタムしたい

Posted at

やりたき事

  • 三角形を押下した時は開閉
  • それ以外の部分を押下した時は別画面に遷移

元々のコード

import { List } from "react-native-paper";
export const Accordion = () => {
    return (
        <List.Accordion
            title="Summary"
        >
            <List.Item title="Detail 1" />
            <List.Item title="Detail 2" />
        </List.Accordion>
    );
}

修正方針

List.Accordionを使わない。開閉アイコンだけを使うことにする。

修正後コード

import { List } from "react-native-paper";
import { TouchableWithoutFeedback } from "react-native";
export const Accordion = () => {
    const [expanded, setExpanded] = useState(false);
    const handlePress = () =>{/* タイトルクリック時のアクション */}
    return (
        // Summary
         <View style={{ flexDirection: "row", alignItems: "center" }}>
          <TouchableWithoutFeedback onPress={handlePress}>
              <Text>Summary</Text>
          </TouchableWithoutFeedback>

          // 開閉アイコン
          <TouchableWithoutFeedback onPress={() => setExpanded(!expanded)}>
            <View>
              <List.Icon icon={expanded ? "chevron-up" : "chevron-down"} />
            </View>
          </TouchableWithoutFeedback>
        </View>

        // Details
        {expanded && (
            <List.Item title="Detail 1" />
            <List.Item title="Detail 2" />
        )}
    );
}

メモ

  • タッチ時に視覚的な変化をつけたいならTouchableWithourFeedbackじゃないのを使う

その他

僕がもっとReactに詳しければ、最高なんですが
image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?