Skip to content

Commit 1515348

Browse files
committed
chore: add docs for more screens and move vote button logic inside
1 parent 52b9138 commit 1515348

File tree

4 files changed

+80
-24
lines changed

4 files changed

+80
-24
lines changed

docusaurus/docs/reactnative/ui-components/poll-answers-list.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ A component used to render a list of answers that a `Poll` has. The results will
1010

1111
It will render its default `PollAnswersListContent`, which can be overridden for custom UI. Its children have access to the entire `poll` state through the `usePollState` hook.
1212

13+
To access the paginated list of answers, the `usePollAnswersPagination` hook can be used.
14+
1315
## Props
1416

1517
### <div class="label description">_forwarded from [MessageContext](../../contexts/message-context#message)_ props</div> message {#message}

docusaurus/docs/reactnative/ui-components/poll-option-full-results.mdx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,55 @@
22
id: poll-option-full-results
33
title: PollOptionFullResults
44
---
5+
6+
import MessageProp from '../common-content/contexts/message-context/message.mdx';
7+
import Poll from '../common-content/contexts/poll-context/poll.mdx';
8+
9+
A component all of the votes for a specific `Poll` option. The results will be paginated and updated as they change. Needs to be structured inside a [`Channel` component](../core-components/channel).
10+
11+
It will render its default `PollOptionFullResultsContent`, which can be overridden for custom UI. Its children have access to the entire `poll` state through the `usePollState` hook.
12+
13+
To access the paginated list of votes, the `usePollOptionVotesPagination` hook can be used.
14+
15+
## Props
16+
17+
### <div class="label description">_forwarded from [MessageContext](../../contexts/message-context#message)_ props</div> message {#message}
18+
19+
<MessageProp />
20+
21+
### `poll` *
22+
23+
<Poll />
24+
25+
### `option` *
26+
27+
The `poll` option that we want to display the results for.
28+
29+
| Type |
30+
| ------ |
31+
| object |
32+
33+
### `additionalFlatListProps`
34+
35+
A prop used to override the underlying [`FlatList`](https://reactnative.dev/docs/flatlist#props) props of the `PollOptionFullResults`.
36+
37+
```jsx
38+
const flatListProps = { bounces: true };
39+
40+
<PollOptionFullResults additionalFlatListProps={flatListProps} />;
41+
```
42+
43+
| Type |
44+
| ------ |
45+
| object |
46+
47+
### `PollOptionFullResultsContent`
48+
49+
A `Component` prop used to render the content of the `PollOptionFullResults` component.
50+
51+
The component has full access to the entire `Poll` reactive state through the `usePollState` hook.
52+
53+
| Type | Default |
54+
| ------------- | ---------------------------------------------- |
55+
| ComponentType | [`PollOptionFullResultsContent`](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/Poll/PollOptionFullResults.tsx) |
56+

package/src/components/Poll/components/Button.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ export type PollButtonProps<
3535
};
3636

3737
export type PollVoteButtonProps = {
38-
onPress?: () => void;
39-
};
38+
option: PollOption;
39+
} & Pick<PollButtonProps, 'onPress'>;
4040

4141
export const GenericPollButton = ({ onPress, title }: { onPress?: () => void; title?: string }) => {
4242
const {
@@ -326,7 +326,8 @@ export const ShowAllOptionsButton = (props: PollButtonProps) => {
326326
);
327327
};
328328

329-
export const VoteButton = ({ onPress, option }: PollVoteButtonProps & { option: PollOption }) => {
329+
export const VoteButton = ({ onPress, option }: PollVoteButtonProps) => {
330+
const { message, poll } = usePollContext();
330331
const { is_closed, ownVotesByOptionId } = usePollState();
331332
const ownCapabilities = useOwnCapabilitiesContext();
332333

@@ -341,9 +342,26 @@ export const VoteButton = ({ onPress, option }: PollVoteButtonProps & { option:
341342
},
342343
} = useTheme();
343344

345+
const toggleVote = useCallback(async () => {
346+
if (ownVotesByOptionId[option.id]) {
347+
await poll.removeVote(ownVotesByOptionId[option.id]?.id, message.id);
348+
} else {
349+
await poll.castVote(option.id, message.id);
350+
}
351+
}, [message.id, option.id, ownVotesByOptionId, poll]);
352+
353+
const onPressHandler = useCallback(() => {
354+
if (onPress) {
355+
onPress({ message, poll });
356+
return;
357+
}
358+
359+
toggleVote();
360+
}, [message, onPress, poll, toggleVote]);
361+
344362
return ownCapabilities.castPollVote && !is_closed ? (
345363
<TouchableOpacity
346-
onPress={onPress}
364+
onPress={onPressHandler}
347365
style={[
348366
styles.voteContainer,
349367
{

package/src/components/Poll/components/PollOption.tsx

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useMemo } from 'react';
1+
import React, { useMemo } from 'react';
22

33
import { ScrollViewProps, StyleSheet, Text, View } from 'react-native';
44

@@ -8,13 +8,7 @@ import { PollOption as PollOptionClass, PollState, PollVote } from 'stream-chat'
88

99
import { VoteButton } from './Button';
1010

11-
import {
12-
PollContextProvider,
13-
PollContextValue,
14-
useMessageContext,
15-
usePollContext,
16-
useTheme,
17-
} from '../../../contexts';
11+
import { PollContextProvider, PollContextValue, useTheme } from '../../../contexts';
1812

1913
import { DefaultStreamChatGenerics } from '../../../types/types';
2014
import { Avatar } from '../../Avatar/Avatar';
@@ -93,17 +87,7 @@ export const PollAllOptions = ({
9387
);
9488

9589
export const PollOption = ({ option, showProgressBar = true }: PollOptionProps) => {
96-
const { is_closed, ownVotesByOptionId, vote_counts_by_option } = usePollState();
97-
const { poll } = usePollContext();
98-
const { message } = useMessageContext();
99-
100-
const toggleVote = useCallback(async () => {
101-
if (ownVotesByOptionId[option.id]) {
102-
await poll.removeVote(ownVotesByOptionId[option.id]?.id, message.id);
103-
} else {
104-
await poll.castVote(option.id, message.id);
105-
}
106-
}, [message.id, option.id, ownVotesByOptionId, poll]);
90+
const { is_closed, vote_counts_by_option } = usePollState();
10791

10892
const { latest_votes_by_option, maxVotedOptionIds } = usePollStateStore(selector);
10993

@@ -161,7 +145,7 @@ export const PollOption = ({ option, showProgressBar = true }: PollOptionProps)
161145
return (
162146
<View style={[styles.wrapper, wrapper]}>
163147
<View style={[styles.container, container]}>
164-
<VoteButton onPress={toggleVote} option={option} />
148+
<VoteButton option={option} />
165149
<Text style={[styles.text, { color: black }, text]}>{option.text}</Text>
166150
<View style={{ flexDirection: 'row' }}>
167151
{relevantVotes.map((vote: PollVote) => (

0 commit comments

Comments
 (0)