Skip to content

Commit a2db0ca

Browse files
committed
docs: finalize docs with answer list customizations
1 parent 1d90b13 commit a2db0ca

File tree

5 files changed

+245
-1
lines changed

5 files changed

+245
-1
lines changed
126 KB
Loading
101 KB
Loading
171 KB
Loading
231 KB
Loading

docusaurus/docs/reactnative/guides/custom-poll-flow.mdx

Lines changed: 245 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import CustomStep12 from '../assets/guides/custom-poll-flow/custom-step-1-2.png'
1313
import CustomCreation1 from '../assets/guides/custom-poll-flow/custom-creation-1.png';
1414
import CustomCreation2 from '../assets/guides/custom-poll-flow/custom-creation-2.png';
1515

16+
import CustomCommentsCreation1 from '../assets/guides/custom-poll-flow/custom-comments-creation-1.png';
17+
import CustomCommentsCreation2 from '../assets/guides/custom-poll-flow/custom-comments-creation-2.png';
18+
1619
In this cookbook we'll go over how we can create and customize a `Channel` screen containing the [`Poll` component](../ui-components/poll.mdx), along with adding navigation to all of its underlying screens rather than relying on modals.
1720

1821
### Prerequisites
@@ -402,7 +405,7 @@ giving us the final UI:
402405
<img src={CustomCreation2} alt='Step 1-2' style={{ width: '40%' }} />
403406
</div>
404407

405-
As one final step, let's change the background color of our `Poll`s. Since all `Poll` components are `theme` compatible, we can do this by overriding the default theme:
408+
Going forward with customizations, let's change the background color of our `Poll`s. Since all `Poll` components are `theme` compatible, we can do this by overriding the default theme:
406409

407410
```tsx
408411
import {
@@ -473,3 +476,244 @@ which gives us a changed `Poll` background:
473476
:::note
474477
Make sure that the `theme` is set above your `OverlayProvider` to make sure that the `Poll` customizations are also reflected on the message preview whenever it's long pressed.
475478
:::
479+
480+
As one final step, let's assume we want to add an `PollAnswersList`; however the default UI doesn't fit our requirements and we want something more custom.
481+
482+
To do this, we first need to add the `ShowAllCommentsButton` and `AddCommentButton` components to our custom `Buttons` UI:
483+
484+
```tsx
485+
import {
486+
// ...rest of the imports
487+
// highlight-start
488+
ShowAllCommentsButton,
489+
AddCommentButton
490+
// highlight-end
491+
} from 'stream-chat-react-native';
492+
import { createStackNavigator } from '@react-navigation/stack';
493+
494+
// ... rest of the components
495+
496+
const MyPollButtons = () => {
497+
return (
498+
<>
499+
<ShowAllOptionsButton />
500+
// highlight-start
501+
<ShowAllCommentsButton />
502+
<AddCommentButton />
503+
// highlight-end
504+
<ViewResultsButton
505+
onPress={({ message, poll }) =>
506+
navigation.navigate('PollResultsScreen', {
507+
message,
508+
poll,
509+
});
510+
}
511+
/>
512+
<EndVoteButton />
513+
</>
514+
)
515+
}
516+
517+
// ... the ChannelScreen component
518+
```
519+
520+
Next, to actually create comments we need a poll that accepts answers (comments) as well as adding a comment. We can do this by enabling the option when creating a poll and then clicking on the `Add Comment` button:
521+
522+
<div style={{ display: 'flex', gap: '1rem', justifyContent: 'space-between' }}>
523+
<img src={CustomCommentsCreation1} alt='Step 1-1' style={{ width: '40%' }} />
524+
<img src={CustomCommentsCreation2} alt='Step 1-2' style={{ width: '40%' }} />
525+
</div>
526+
527+
Now, similar to how we handled the other screens we would want to create a separate `Screen` in the navigation stack for our `AnswersList`. For now, we'll use the default UI:
528+
529+
```tsx
530+
import {
531+
// ...rest of the imports
532+
// highlight-next-line
533+
PollAnswersList,
534+
} from 'stream-chat-react-native';
535+
import { createStackNavigator } from '@react-navigation/stack';
536+
537+
// ... rest of the components
538+
539+
const MyPollButtons = () => {
540+
return (
541+
<>
542+
<ShowAllOptionsButton />
543+
// highlight-start
544+
<ShowAllCommentsButton
545+
onPress={({ message, poll }) => {
546+
navigation.navigate('PollAnswersScreen', {
547+
message,
548+
poll,
549+
});
550+
}}
551+
/>
552+
// highlight-end
553+
<AddCommentButton />
554+
<ViewResultsButton
555+
onPress={({ message, poll }) =>
556+
navigation.navigate('PollResultsScreen', {
557+
message,
558+
poll,
559+
});
560+
}
561+
/>
562+
<EndVoteButton />
563+
</>
564+
)
565+
}
566+
567+
// highlight-start
568+
const PollAnswersScreen = ({
569+
route: {
570+
params: { message, poll },
571+
},
572+
}) => {
573+
const navigation = useNavigation();
574+
return (
575+
<SafeAreaView style={{ flex: 1 }}>
576+
<PollModalHeader title={'All Poll Answers'} onPress={() => navigation.goBack()} />
577+
<PollAnswersList message={message} poll={poll} />
578+
</SafeAreaView>
579+
);
580+
};
581+
// highlight-end
582+
583+
const ChannelScreen = () => {
584+
const navigation = useNavigation();
585+
return (
586+
<ThemeProvider style={myTheme}>
587+
<OverlayProvider>
588+
<Chat client={client}>
589+
<Channel
590+
channel={channel}
591+
PollContent={MyPollContent}
592+
openPollCreationDialog={({ sendMessage }) => navigation.navigate('CreatePollScreen', { sendMessage })}
593+
>
594+
<ChannelStack.Navigator initialRouteName={'ChannelMessageList'}>
595+
<ChannelStack.Screen
596+
name={'ChannelMessageList'}
597+
options={{ headerShown: false }}
598+
component={ChannelMessageList}
599+
/>
600+
<ChannelStack.Screen
601+
name={'PollResultsScreen'}
602+
options={{ headerShown: false }}
603+
component={PollResultsScreen}
604+
/>
605+
// highlight-start
606+
<ChannelStack.Screen
607+
name={'PollAnswersScreen'}
608+
options={{ headerShown: false }}
609+
component={PollAnswersScreen}
610+
/>
611+
// highlight-end
612+
<ChannelStack.Group screenOptions={{ presentation: 'modal' }}>
613+
<ChannelStack.Screen
614+
name={'CreatePollScreen'}
615+
options={{ headerShown: false }}
616+
component={MyCreatePollContent}
617+
/>
618+
</ChannelStack.Group>
619+
</ChannelStack.Navigator>
620+
</Channel>
621+
</Chat>
622+
</OverlayProvider>
623+
</ThemeProvider>
624+
);
625+
};
626+
```
627+
628+
which will give allow us to navigate to the default `PollAnswersList` UI:
629+
630+
![Answer List Navigation](../assets/guides/custom-poll-flow/answer-list-navigation.png)
631+
632+
Now, let's finally customize the UI. To achieve this we can override the `PollAnswersListContent` of our `PollAnswersList`.
633+
634+
Since the list of answers can be very large and we want to be able to still display all answers, we will use the `usePollAnswersPagination` hook to get them:
635+
636+
```tsx
637+
import {
638+
// ...rest of the imports
639+
// highlight-next-line
640+
usePollAnswersPagination,
641+
} from 'stream-chat-react-native';
642+
import { createStackNavigator } from '@react-navigation/stack';
643+
644+
// ... rest of the components
645+
646+
// highlight-start
647+
const LoadingIndicator = () => { /* some LoadingIndicator logic here */ }
648+
649+
const MyItem = ({ item }) => {
650+
const { answer_text, user } = item;
651+
return (
652+
<Text>
653+
{user.name} commented: {answer_text}
654+
</Text>
655+
);
656+
};
657+
658+
const MyPollAnswersContent = () => {
659+
const { pollAnswers, loading, loadMore } = usePollAnswersPagination();
660+
return (
661+
<FlatList
662+
contentContainerStyle={{ flex: 1, padding: 16 }}
663+
data={pollAnswers}
664+
renderItem={MyItem}
665+
onEndReached={loadMore}
666+
ListFooterComponent={loading ? <LoadingIndicator /> : null}
667+
/>
668+
);
669+
};
670+
// highlight-end
671+
672+
const PollAnswersScreen = ({
673+
route: {
674+
params: { message, poll },
675+
},
676+
}) => {
677+
const navigation = useNavigation();
678+
return (
679+
<SafeAreaView style={{ flex: 1 }}>
680+
<PollModalHeader title={'All Poll Answers'} onPress={() => navigation.goBack()} />
681+
// highlight-start
682+
<PollAnswersList
683+
message={message}
684+
poll={poll}
685+
PollAnswersListContent={MyPollAnswersContent}
686+
/>
687+
// highlight-end
688+
</SafeAreaView>
689+
);
690+
};
691+
692+
// ... the Channel screen
693+
```
694+
695+
And we get the final content:
696+
697+
![Custom Answers List](../assets/guides/custom-poll-flow/custom-answers-list.png)
698+
699+
The list will be fully compatible with loading the pagination when scrolling to the bottom and displaying a loading indicator whenever that happens as well.
700+
701+
With that, we have finished the customizations we wanted to do for our polls.
702+
703+
As a last note; any components that you'd like to reuse from the default UI are free to be imported from within the SDK.
704+
705+
An extensive list of these includes:
706+
707+
- All buttons mentioned [here](../ui-components/poll-buttons.mdx)
708+
- `CreatePollContent`
709+
- `PollContent`
710+
- `PollButtons`
711+
- `PollHeader`
712+
- `PollModalHeader`
713+
- `PollInputDialog`
714+
- `CreatePollIcon`
715+
- `PollOption`
716+
- `PollResultsContent`
717+
- `PollResultsItem`
718+
- `PollVote`
719+
- `PollAllOptionsContent`

0 commit comments

Comments
 (0)