| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- import {
- type FC
- } from "react";
- import {
- TouchableOpacity,
- View
- } from "react-native";
- import type IRadioButtonProps from "./type";
- import stylesheet, {
- getRadioButtonType,
- useStyles
- } from "./stylesheet";
- import {
- NCoreUIKitLocalize,
- NCoreUIKitTheme
- } from "../../core/hooks";
- import type ITextProps from "../text/type";
- import Loading from "../loading";
- import Text from "../text";
- const RadioButton: FC<IRadioButtonProps> = ({
- displayBehaviourWhileLoading = "disabled",
- spreadBehaviour = "baseline",
- isOptional = false,
- isDisabled = false,
- type = "neutral",
- isFlip = false,
- customLocalize,
- subTitleStyle,
- optionalText,
- customTheme,
- titleStyle,
- isChecked,
- isLoading,
- subTitle,
- onPress,
- title,
- style,
- ...props
- }) => {
- const {
- inlineSpaces,
- radiuses,
- borders,
- colors,
- spaces
- } = NCoreUIKitTheme.useContext(customTheme);
- const {
- localize
- } = NCoreUIKitLocalize.useContext(customLocalize);
- const currentType = getRadioButtonType({
- type
- });
- const RADIO_INDICATOR_SIZE = 16;
- const {
- indicatorContainer: indicatorContainerDynamicStyle,
- contentContainer: contentContainerDynamicStyle,
- titleContainer: titleContainerDynamicStyle,
- optionalText: optionalTextDynamicStyle,
- container: containerDynamicStyle,
- indicator: indicatorDynamicStyle,
- subTitle: subTitleDynamicStyle,
- loading: loadingDynamicStyle,
- overlay: overlayDynamicStyle,
- title: titleDynamicStyle
- } = useStyles({
- displayBehaviourWhileLoading,
- RADIO_INDICATOR_SIZE,
- spreadBehaviour,
- inlineSpaces,
- currentType,
- isDisabled,
- isChecked,
- isLoading,
- radiuses,
- borders,
- isFlip,
- colors,
- spaces,
- type
- });
- const titleProps: ITextProps = {
- color: currentType.titleColor,
- };
- const subTitleProps: ITextProps = {
- color: currentType.subTitleColor,
- };
- const indicatorIconProps: {
- color: keyof NCoreUIKit.IconContentColors;
- customColor?: string;
- } = {
- color: currentType.indicatorColor
- };
- if (isDisabled || isLoading) {
- const stateType = type === "danger" ? "error" : type;
- subTitleProps.customColor = colors.system.state.content.disabled[stateType];
- titleProps.customColor = colors.system.state.content.disabled[stateType];
- if(isChecked && stateType === "neutral") {
- indicatorIconProps.customColor = colors.system.state.border.disabled.primary;
- } else {
- indicatorIconProps.customColor = colors.system.state.border.disabled[stateType];
- }
- }
- const renderOptionalText = () => {
- if(!isOptional && !optionalText) {
- return null;
- }
- return <Text
- variant="labelLargeSize"
- color={titleProps.color}
- style={[
- optionalTextDynamicStyle
- ]}
- {...titleProps}
- >
- ( {isOptional ? localize("is-optional") : optionalText} )
- </Text>;
- };
- const renderTitle = () => {
- if (!title) {
- return null;
- }
- return <View
- style={[
- stylesheet.titleContainer,
- titleContainerDynamicStyle
- ]}
- >
- <Text
- variant="bodyMediumSize"
- style={[
- titleStyle,
- stylesheet.title,
- titleDynamicStyle
- ]}
- {...titleProps}
- >
- {title}
- </Text>
- {renderOptionalText()}
- </View>;
- };
- const renderIndicator = () => {
- if(!isChecked) {
- return <View
- style={[
- indicatorDynamicStyle,
- {
- backgroundColor: "transparent"
- }
- ]}
- />;
- }
- return <View
- style={[
- stylesheet.indicator,
- indicatorDynamicStyle
- ]}
- />;
- };
- const renderIndicatorContainer = () => {
- if (isLoading) {
- return <Loading
- style={[
- loadingDynamicStyle
- ]}
- />;
- }
- return <View
- style={[
- stylesheet.indicatorContainer,
- indicatorContainerDynamicStyle
- ]}
- >
- {renderIndicator()}
- {renderOverlay()}
- </View>;
- };
- const renderOverlay = () => {
- return <View
- style={[
- stylesheet.overlay,
- overlayDynamicStyle
- ]}
- />;
- };
- const renderSubtitle = () => {
- if(!subTitle) {
- return null;
- }
- return <Text
- {...subTitleProps}
- style={[
- subTitleStyle,
- stylesheet.subTitle,
- subTitleDynamicStyle
- ]}
- >
- {subTitle}
- </Text>;
- };
- const renderContent = () => {
- if(!title) {
- return null;
- }
- return <View
- style={[
- stylesheet.contentContainer,
- contentContainerDynamicStyle
- ]}
- >
- {renderTitle()}
- {renderSubtitle()}
- </View>;
- };
- return (
- <TouchableOpacity
- {...props}
- onPress={isDisabled || isLoading ? () => null : onPress ? onPress : undefined}
- disabled={isDisabled || isLoading || !onPress}
- style={[
- style,
- stylesheet.container,
- containerDynamicStyle
- ]}
- >
- {isFlip ? null : renderIndicatorContainer()}
- {renderContent()}
- {isFlip ? renderIndicatorContainer() : null}
- </TouchableOpacity>
- );
- };
- export default RadioButton;
|