react-native-surveys
Version:
Build your own forms, surveys and polls for your React Native apps.
251 lines (244 loc) • 6.49 kB
JavaScript
import React, { Fragment, memo, useState, useEffect } from "react";
import {
View,
StyleSheet,
TouchableOpacity,
Text,
TextInput,
Platform
} from "react-native";
import { colors, MAX_FORM_WIDTH } from "../theme";
import { CheckIcon } from "../assets/icons";
import QuestionHeader from "../components/QuestionHeader";
const SingleSelection = ({
form,
block,
isPreview,
currentPageIndex,
blockIndex,
allBlocks
}) => {
const {
backgroundColor = colors.white,
color = colors.main,
textColor = colors.primary
} = form;
const { answers, other = {} } = block;
const isOther = block.value
? answers.filter(a => a.value === block.value).length === 0
: false;
const [isOtherSelected, selectOther] = useState(isOther);
const [otherInputValue, updateValue] = useState(isOther ? block.value : "");
const [selectedAnswer, selectAnswer] = useState(block.value);
useEffect(() => {
if (!selectedAnswer && block.value) {
const isOther = block.value
? answers.filter(a => a.value === block.value).length === 0
: false;
selectOther(isOther, block.value);
updateValue(isOther ? block.value : "");
selectAnswer(block.value);
}
}, [answers, block.value, selectedAnswer]);
const onAnswerSelected = answer => () => {
if (isPreview) return;
if (answer === other.label) {
block.value = otherInputValue;
selectAnswer("");
selectOther(!isOtherSelected);
updateInput("");
} else {
block.value = answer;
selectAnswer(answer);
selectOther(false);
}
};
const updateInput = value => {
block.value = value;
updateValue(value);
};
const otherStyle = {
borderColor: isOtherSelected ? color + "80" : colors.border,
borderWidth: 1,
borderStyle: "solid",
backgroundColor: isOtherSelected ? `${color}10` : backgroundColor
};
const otherBoxStyle = {
backgroundColor: isOtherSelected ? color : colors.background,
borderColor: isOtherSelected ? "transparent" : colors.lightgrey,
borderWidth: 1,
borderStyle: "solid"
};
const otherTextStyle = {
fontWeight: isOtherSelected
? "bold"
: Platform.OS === "android"
? "400"
: "500",
color: isOtherSelected ? color : textColor,
opacity: isOtherSelected ? 1 : 0.9,
fontSize: 15
};
const inputStyle = {
color: textColor
};
return React.createElement(
View,
{
style: styles.container
},
React.createElement(QuestionHeader, {
form: form,
block: block,
currentPageIndex: currentPageIndex,
blockIndex: blockIndex,
allBlocks: allBlocks
}),
React.createElement(
View,
{
style: styles.answersContainer
},
answers.map((answer, i) => {
const value = answer.value;
const isSelected = selectedAnswer === value;
const answerStyle = {
borderColor: isSelected ? color + "80" : colors.border,
borderWidth: 1,
borderStyle: "solid",
backgroundColor: isSelected ? `${color}10` : backgroundColor
};
const boxStyle = {
backgroundColor: isSelected ? color : colors.background,
borderColor: isSelected ? "transparent" : colors.lightgrey,
borderWidth: 1,
borderStyle: "solid"
};
const answerTextStyle = {
fontWeight: isSelected
? "bold"
: Platform.OS === "android"
? "400"
: "500",
color: isSelected ? color : textColor,
opacity: isSelected ? 1 : 0.9,
fontSize: 15
};
return React.createElement(
TouchableOpacity,
{
key: value + i,
onPress: onAnswerSelected(value),
activeOpacity: 0.8,
style: [styles.answer, answerStyle]
},
React.createElement(
View,
{
style: [styles.box, boxStyle]
},
isSelected &&
React.createElement(CheckIcon, {
size: 12,
color: colors.white
})
),
React.createElement(
Text,
{
style: answerTextStyle
},
value
)
);
}),
other.label &&
React.createElement(
Fragment,
null,
React.createElement(
TouchableOpacity,
{
onPress: onAnswerSelected(other.label),
activeOpacity: 0.8,
style: [styles.answer, otherStyle]
},
React.createElement(
View,
{
style: [styles.box, otherBoxStyle]
},
isOtherSelected &&
React.createElement(CheckIcon, {
size: 12,
color: colors.white
})
),
React.createElement(
Text,
{
style: otherTextStyle
},
other.label
)
),
isOtherSelected &&
React.createElement(TextInput, {
editable: !isPreview,
maxLength: 500,
style: [inputStyle, styles.input],
onChangeText: updateInput,
value: otherInputValue,
placeholder: other.placeholder || "Enter here..."
})
)
)
);
};
const styles = StyleSheet.create({
container: {
display: "flex",
flexDirection: "column",
alignItems: "center"
},
answersContainer: {
display: "flex",
flexDirection: "column",
width: "100%",
backgroundColor: "transparent",
maxWidth: MAX_FORM_WIDTH,
paddingVertical: 24,
paddingHorizontal: 24
},
answer: {
display: "flex",
flexDirection: "row",
width: "100%",
alignItems: "center",
padding: 16,
borderRadius: 6,
marginBottom: 20
},
box: {
display: "flex",
alignItems: "center",
justifyContent: "center",
width: 16,
height: 16,
borderRadius: 16,
marginRight: 16
},
input: {
borderColor: colors.buttonBorder,
borderWidth: 1,
borderStyle: "solid",
borderRadius: 6,
fontSize: 15,
minHeight: 46,
maxHeight: 46,
padding: 16,
width: "100%",
backgroundColor: colors.white
}
});
export default memo(SingleSelection);