tooot/src/screens/Shared/Compose/Poll.tsx

196 lines
5.8 KiB
TypeScript
Raw Normal View History

2020-12-03 22:03:06 +01:00
import React, { Dispatch, useEffect, useState } from 'react'
2020-12-06 22:32:36 +01:00
import { ActionSheetIOS, StyleSheet, TextInput, View } from 'react-native'
2020-12-03 22:03:06 +01:00
import { Feather } from '@expo/vector-icons'
2020-12-07 12:31:40 +01:00
import { PostAction, ComposeState } from '../Compose'
2020-12-03 22:03:06 +01:00
import { useTheme } from 'src/utils/styles/ThemeManager'
import { StyleConstants } from 'src/utils/styles/constants'
2020-12-06 21:42:19 +01:00
import { ButtonRow } from 'src/components/Button'
2020-12-03 22:03:06 +01:00
import { MenuContainer, MenuRow } from 'src/components/Menu'
export interface Props {
2020-12-07 12:31:40 +01:00
composeState: ComposeState
composeDispatch: Dispatch<PostAction>
2020-12-03 22:03:06 +01:00
}
2020-12-07 12:31:40 +01:00
const ComposePoll: React.FC<Props> = ({ composeState, composeDispatch }) => {
2020-12-03 22:03:06 +01:00
const { theme } = useTheme()
const expireMapping: { [key: string]: string } = {
'300': '5分钟',
'1800': '30分钟',
'3600': '1小时',
'21600': '6小时',
'86400': '1天',
'259200': '3天',
'604800': '7天'
}
const [firstRender, setFirstRender] = useState(true)
useEffect(() => {
setFirstRender(false)
}, [])
return (
<View style={[styles.base, { borderColor: theme.border }]}>
<View style={styles.options}>
2020-12-07 12:31:40 +01:00
{[...Array(composeState.poll.total)].map((e, i) => {
const restOptions = Object.keys(composeState.poll.options).filter(
o => parseInt(o) !== i && parseInt(o) < composeState.poll.total
2020-12-06 22:32:36 +01:00
)
let hasConflict = false
restOptions.forEach(o => {
// @ts-ignore
2020-12-07 12:31:40 +01:00
if (composeState.poll.options[o] === composeState.poll.options[i]) {
2020-12-06 22:32:36 +01:00
hasConflict = true
}
})
return (
<View key={i} style={styles.option}>
<Feather
2020-12-07 12:31:40 +01:00
name={composeState.poll.multiple ? 'square' : 'circle'}
2020-12-06 22:32:36 +01:00
size={StyleConstants.Font.Size.L}
color={theme.secondary}
/>
<TextInput
{...(i === 0 && firstRender && { autoFocus: true })}
style={[
styles.textInput,
{
borderColor: theme.border,
color: hasConflict ? theme.error : theme.primary
2020-12-03 22:03:06 +01:00
}
2020-12-06 22:32:36 +01:00
]}
placeholder={`选项`}
placeholderTextColor={theme.secondary}
maxLength={50}
// @ts-ignore
2020-12-07 12:31:40 +01:00
value={composeState.poll.options[i]}
2020-12-06 22:32:36 +01:00
onChangeText={e =>
2020-12-07 12:31:40 +01:00
composeDispatch({
2020-12-06 22:32:36 +01:00
type: 'poll',
payload: {
2020-12-07 12:31:40 +01:00
...composeState.poll,
options: { ...composeState.poll.options, [i]: e }
2020-12-06 22:32:36 +01:00
}
})
}
/>
</View>
)
})}
2020-12-03 22:03:06 +01:00
</View>
<View style={styles.controlAmount}>
<View style={styles.firstButton}>
2020-12-06 21:42:19 +01:00
<ButtonRow
2020-12-03 22:03:06 +01:00
onPress={() =>
2020-12-07 12:31:40 +01:00
composeState.poll.total > 2 &&
composeDispatch({
2020-12-03 22:03:06 +01:00
type: 'poll',
2020-12-07 12:31:40 +01:00
payload: { ...composeState.poll, total: composeState.poll.total - 1 }
2020-12-03 22:03:06 +01:00
})
}
icon='minus'
2020-12-07 12:31:40 +01:00
disabled={!(composeState.poll.total > 2)}
2020-12-03 22:03:06 +01:00
buttonSize='S'
/>
</View>
2020-12-06 21:42:19 +01:00
<ButtonRow
2020-12-03 22:03:06 +01:00
onPress={() =>
2020-12-07 12:31:40 +01:00
composeState.poll.total < 4 &&
composeDispatch({
2020-12-03 22:03:06 +01:00
type: 'poll',
2020-12-07 12:31:40 +01:00
payload: { ...composeState.poll, total: composeState.poll.total + 1 }
2020-12-03 22:03:06 +01:00
})
}
icon='plus'
2020-12-07 12:31:40 +01:00
disabled={!(composeState.poll.total < 4)}
2020-12-03 22:03:06 +01:00
buttonSize='S'
/>
</View>
<MenuContainer>
<MenuRow
title='可选项'
2020-12-07 12:31:40 +01:00
content={composeState.poll.multiple ? '多选' : '单选'}
2020-12-03 22:03:06 +01:00
onPress={() =>
ActionSheetIOS.showActionSheetWithOptions(
{
options: ['单选', '多选', '取消'],
cancelButtonIndex: 2
},
index =>
index < 2 &&
2020-12-07 12:31:40 +01:00
composeDispatch({
2020-12-03 22:03:06 +01:00
type: 'poll',
2020-12-07 12:31:40 +01:00
payload: { ...composeState.poll, multiple: index === 1 }
2020-12-03 22:03:06 +01:00
})
)
}
iconBack='chevron-right'
/>
<MenuRow
title='有效期'
2020-12-07 12:31:40 +01:00
content={expireMapping[composeState.poll.expire]}
2020-12-03 22:03:06 +01:00
onPress={() =>
ActionSheetIOS.showActionSheetWithOptions(
{
options: [...Object.values(expireMapping), '取消'],
cancelButtonIndex: 7
},
index =>
index < 7 &&
2020-12-07 12:31:40 +01:00
composeDispatch({
2020-12-03 22:03:06 +01:00
type: 'poll',
payload: {
2020-12-07 12:31:40 +01:00
...composeState.poll,
2020-12-03 22:03:06 +01:00
expire: Object.keys(expireMapping)[index]
}
})
)
}
iconBack='chevron-right'
/>
</MenuContainer>
</View>
)
}
const styles = StyleSheet.create({
base: {
flex: 1,
borderWidth: StyleSheet.hairlineWidth,
borderRadius: 6
},
options: {
marginTop: StyleConstants.Spacing.M,
marginBottom: StyleConstants.Spacing.S
},
option: {
marginLeft: StyleConstants.Spacing.M,
marginRight: StyleConstants.Spacing.M,
marginBottom: StyleConstants.Spacing.S,
flexDirection: 'row',
alignItems: 'center'
},
textInput: {
flex: 1,
padding: StyleConstants.Spacing.S,
borderWidth: StyleSheet.hairlineWidth,
borderRadius: 6,
fontSize: StyleConstants.Font.Size.M,
marginLeft: StyleConstants.Spacing.S
},
controlAmount: {
flex: 1,
flexDirection: 'row',
justifyContent: 'flex-end',
marginRight: StyleConstants.Spacing.M,
marginBottom: StyleConstants.Spacing.M
},
firstButton: {
marginRight: StyleConstants.Spacing.S
}
})
export default ComposePoll