0

I have data in FlatList which is working fine.

The data is hidden by default but when I click on the arrow, I want it to appear below the field.

Problem is that, when I click on the icon, it appears without problem but items are not clickable on it.

But if I set that state data true by default, the clickable to list items works fine because it gets rendered on screen.

My requirements are as when this list appears with state change, the list should appear and be clickable without a problem. Currently, it does appear but when I try to click items, it disappears.

How we can do it? Because I also need to add a search on it later on. Please provide a long-term solution.

Code:

{getD ? (
              <View style={{ flex: 1 }}>
                <FlatList
                  style={{
                    flexGrow: 1,
                    flexShrink: 1,
                    flexDirection: 'column',
                    overflow: 'scroll',
                    maxHeight: 150,
                    position: 'absolute',
                    width: '100%',
                    zIndex: 10,
                    elevation: 10,
                    backgroundColor: '#ffffff',
                    borderRadius: 5,
                    borderWidth: 1,
                    borderStyle: 'solid',
                    borderColor: '#D7D7D7',
                    shadowOffset: { height: 3, width: 0 },
                    shadowRadius: 10,
                    shadowColor: '#00000010',
                    shadowOpacity: 1,
                    flex: 1,
                  }}
                  data={recSet}
                  renderItem={({ item }) => (
                    <TouchableOpacity onPress={() => alert('xxxxx')}>
                      <View style={{}} />
                      <Text style={{}}>{item.title}</Text>
                    </TouchableOpacity>
                  )}
                  keyExtractor={(item) => item.id}
                  contentContainerStyle={{ flexGrow: 1 }}
                  extraData={getD}
                />
              </View>
            ) : null}
LearningROR
  • 211
  • 1
  • 13
  • Anyone, please? I am stuck at one at the moment and cant find a way out. :) – LearningROR Aug 11 '22 at 13:22
  • 1
    Take a look at this [link](https://stackoverflow.com/questions/44593024/stacked-touchableopacity-inside-another-touchableopacity-is-not-clickable) make sure you are using react-native `TouchableOpacity` – PhantomSpooks Aug 11 '22 at 16:25

1 Answers1

1

I had no problem getting touches to register properly so I asssuming that you ended auto importing react-native-gesture-handler TouchableOpacity somewhere. Heres a demo:

/*create ToggleFlatlist component*/
import React, { useState } from 'react';
import { View, TouchableOpacity, FlatList, Text } from 'react-native';

export default function ToggableFlatList({
  pressableStyle,
  initialVisibility,
  flatlistWrapperStyle,
  onToggleChange,
  ...flatlistProps
}) {
  const [isRendered, setIsRendered] = useState(initialVisibility);
  const onToggle = () => {
    const newValue = !isRendered;
    setIsRendered(newValue);
    onToggleChange?.(newValue);
  };
  return (
    <TouchableOpacity
      style={[{ flex: 1 }, pressableStyle]}
      onPress={onToggle}>
      {isRendered ? (
        <View style={[{ flex: 1 }, flatlistWrapperStyle]}>
          <FlatList {...flatlistProps} />
        </View>
      ) : null}
    </TouchableOpacity>
  );
}
export default function App() {
  const [isVisible, setIsVisible] = useState(false)
  return (
    <View style={styles.container}>
      <Text style={styles.textInstructions}>Press anywhere to {isVisible ? 'hide':'reveal'} list</Text>
      <ToggableFlatList
        flatlistWrapperStyle={styles.flatlistWrapper}
        initialVisibility={isVisible}
        onToggleChange={setIsVisible}
        style={styles.flatlist}
        data={recSet}
        renderItem={({ item }) => (
          <TouchableOpacity onPress={() => alert('xxxxx')}>
            <View style={{}} />
            <Text style={{}}>{item.title}</Text>
          </TouchableOpacity>
        )}
        keyExtractor={(item) => item.id}
        contentContainerStyle={{ flexGrow: 1 }}
      />
    </View>
  );
}
PhantomSpooks
  • 2,877
  • 2
  • 8
  • 13
  • I will try that one for sure. Thanks for it. Also, I just created a snack for the issue if you want to see the complete code. :) I will accept your answer anyways coz I am going to try that too. :) Link: https://snack.expo.dev/@javiwondr/humiliated-bananas?platform=ios Here you can the issue when dropdown open on icon clicked on right. – LearningROR Aug 11 '22 at 17:17
  • I tried implementing your ones in my code but for some reason now items are not opening up at all. Can you please review it? Link: https://snack.expo.dev/y2SpICqs_?platform=web – LearningROR Aug 11 '22 at 18:48
  • 1
    I just looked at your code and if you remove `onBlur={() => setgetD(false)}` from the TextInput you get your presses in the flatlist – PhantomSpooks Aug 11 '22 at 18:50
  • 1
    Ah the code I wrote turns the entire flatlist into a pressable so im sure some expected stuff would happen. I hadnt known that you were toggling the list with a button – PhantomSpooks Aug 11 '22 at 18:54
  • That's a perfect catch!!! I never knew this silly mistake I have done in the code :) Thanks for it. Also, on android on few items that are on text field, they get clicked into textfield rather than selecting the value. can we fix that using css? :) – LearningROR Aug 11 '22 at 18:56
  • Yup. Agreed. I am sorry for not making that clear properly. :) – LearningROR Aug 11 '22 at 18:57
  • I removed the onblur but now its not closing at all. – LearningROR Aug 11 '22 at 18:59
  • I accepted your answer for helping me out of the way :) – LearningROR Aug 11 '22 at 19:18
  • I added some [stuff](https://snack.expo.dev/ZrBD64KsM?platform=ios). First I set the down arrow to toggle getD instead of just setting it to true so that it can hide and reveal the flatlist. Then I removed the absolute position from the flatlist and gave its containing view a higher zIndex/elevation to avoid interacting with the textinputs rendered under it. I also thought the gradient was cool so I turned the textinput background to near transparent when the gradient is visible – PhantomSpooks Aug 11 '22 at 19:24
  • https://snack.expo.dev/zv-eTq7pb?platform=ios added rotates arrow when list is toggled – PhantomSpooks Aug 11 '22 at 19:32
  • First of all, thanks so much for the changes. :) Also, I am seeing now first name and last name got pushed below with the fields. I want them to have the list on first name and last name so it does not push the fields below. Can we change it to that like the list would always stay above the fields? – LearningROR Aug 11 '22 at 19:34
  • Also can we minimize the thickness of the gradient on the top? Its very nice on left, right and bottom but why its appearing thick on the top? can we fix that? Sorry for asking too much as I just realized I am :) – LearningROR Aug 11 '22 at 19:42
  • 1
    Lemme give it a shot. I pushed the inputs below to avoid the textinputs intercepting the flatlist presses. But if I think if you add `editable={!getD}` to the textinputs then it fixes that – PhantomSpooks Aug 11 '22 at 19:42
  • Yeah that's right but based on UX and UI provided I am keen to do it this way :) – LearningROR Aug 11 '22 at 19:47
  • I could not be able to remove the thickness on top of gradient. I tried every best possible option but I could not do that. on left, right and bottom it look very nice but on top it is thick which is what it looks bad. – LearningROR Aug 11 '22 at 19:53
  • 1
    https://snack.expo.dev/NMzTluBOh?platform=ios I cant get rid of the thickness either – PhantomSpooks Aug 11 '22 at 19:53
  • Thank you so much. All looking cool. For thickness I will check something else but yeah its a mess that it looks bad. :D – LearningROR Aug 11 '22 at 19:58
  • [This](https://snack.expo.dev/Pf5uBuW39?platform=ios) works although presses turns the borders white for some – PhantomSpooks Aug 11 '22 at 20:11
  • Yeah thanks for it. Looks better but I have to make it like this. Link: https://postimg.cc/wtbLMdqW – LearningROR Aug 11 '22 at 20:35
  • I posted this time ago but i wanted to have that for textfield. link: https://stackoverflow.com/questions/73158843/how-to-add-gradient-on-the-textinput-using-react-native – LearningROR Aug 11 '22 at 20:37
  • Oopssss for some reasons with latest changes items are no longer scrollable in Android but fine in iOS :( :( – LearningROR Aug 11 '22 at 20:43
  • 1
    It seems like MaskedView is the way to go to get that look – PhantomSpooks Aug 11 '22 at 21:41
  • Gave it a shot but did not work as expected on textinput. :) – LearningROR Aug 11 '22 at 22:03
  • 1
    So I went on a quite a journey to get something similar to [that picture](https://postimg.cc/wtbLMdqW) and I got pretty [close](https://snack.expo.dev/@phantom-factotum/gradients?platform=android) – PhantomSpooks Aug 12 '22 at 22:50
  • Yeah!!!! that really helped :D it's so close even. thanks so much, brother. – LearningROR Aug 16 '22 at 06:55
  • 1
    Ah the last I checked the buttons werent clickable – PhantomSpooks Aug 16 '22 at 17:00
  • Just checked now and it has been improved from your side again :D Lots of love for you..... <:3 – LearningROR Aug 16 '22 at 18:08