react-native-calendars icon indicating copy to clipboard operation
react-native-calendars copied to clipboard

onDayPress does not work when overriding day component

Open ikotsov opened this issue 4 years ago • 5 comments

Description

Hello,

I am using CalendarList and a custom Day component. onDayPress prop works as expected without defining a custom day component. When I define one it does not work at all, does not respond. I tried that with the example code just to be 100% sure > https://github.com/wix/react-native-calendars#overriding-day-component

To make it work I wrapped my custom day component into a TouchableOpacity tag, but I know judging from the source code that this is not valid.

<CalendarList
style={[styles.calendar, { height: 300 }]}
dayComponent={({ date, state }) => {
  return (
    <TouchableOpacity onPress={myOnPress}>
      <View>
        <Text
          style={{
            textAlign: 'center',
            color: state === 'disabled' ? 'gray' : 'black'
          }}>
          {date.day}
        </Text>
      </View>
    <TouchableOpacity/>
  );
}}
/>

Is this a bug?

Expected Behavior

The onDayPress prop to respond and work as expected when defining a custom day component using dayComponent prop.

Environment

  • npm ls react-native-calendars: 1.265.0
  • npm ls react-native: 0.61.4

Also specified:

  1. Phone/emulator/simulator & version: Nexus_5_API_28

ikotsov avatar May 21 '20 16:05 ikotsov

Yes, you right. We need to rethink about the API of dayComponent.

chenei avatar Jun 04 '20 10:06 chenei

I found there are two more props coming from dayComponent you can use for that, so you can do:

dayComponent={({ date, state, onPress, onLongPress }) => 
        <TouchableOpacity onPress={onPress} onLongPress={onLongPress}>
           <Text
             style={{
               textAlign: 'center',
               color: state === 'disabled' ? 'gray' : 'black'
             }}>
             {date.day}
           </Text>
       <TouchableOpacity/>

MaiconGilton avatar Jul 16 '20 12:07 MaiconGilton

I found there are two more props coming from dayComponent you can use for that, so you can do:

dayComponent={({ date, state, onPress, onLongPress }) => 
        <TouchableOpacity onPress={onPress} onLongPress={onLongPress}>
           <Text
             style={{
               textAlign: 'center',
               color: state === 'disabled' ? 'gray' : 'black'
             }}>
             {date.day}
           </Text>
       <TouchableOpacity/>

Nice find, and if you need the same parameter used in onPress and onLongPress you can use the date

dayComponent={({ date, state, onPress, onLongPress }) => {         
    return (
        <TouchableOpacity onPress={() => onPress(date)} onLongPress={() => onLongPress(date)}>
            <Text>
                ...
            </Text>    
        </TouchableOpacity>
    );
}}

carloswbarros avatar Jul 30 '20 16:07 carloswbarros

I found there are two more props coming from dayComponent you can use for that, so you can do:

dayComponent={({ date, state, onPress, onLongPress }) => 
        <TouchableOpacity onPress={onPress} onLongPress={onLongPress}>
           <Text
             style={{
               textAlign: 'center',
               color: state === 'disabled' ? 'gray' : 'black'
             }}>
             {date.day}
           </Text>
       <TouchableOpacity/>

Nice find, and if you need the same parameter used in onPress and onLongPress you can use the date

dayComponent={({ date, state, onPress, onLongPress }) => {         
    return (
        <TouchableOpacity onPress={() => onPress(date)} onLongPress={() => onLongPress(date)}>
            <Text>
                ...
            </Text>    
        </TouchableOpacity>
    );
}}

Does this affect the performance of the calendar?

Karthik-B-06 avatar Nov 23 '20 08:11 Karthik-B-06

Same issue. Do I still use dayComponent with <Pressable> or <TouchableOpacity > to use onDayPress ?

siiiido avatar Apr 15 '24 07:04 siiiido