react-native#TouchableHighlightProps TypeScript Examples

The following examples show how to use react-native#TouchableHighlightProps. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Checkbox.tsx    From react-native-jigsaw with MIT License 4 votes vote down vote up
Checkbox: React.FC<CheckboxProps & TouchableHighlightProps & IconSlot> =
  ({
    Icon,
    status,
    disabled = false,
    onPress,
    onCheck,
    onUncheck,
    color,
    uncheckedColor,
    defaultValue,
    checkedIcon = "MaterialCommunityIcons/checkbox-marked",
    uncheckedIcon = "MaterialCommunityIcons/checkbox-blank-outline",
    size = 24,
    style,
    ...rest
  }) => {
    const [internalValue, setInternalValue] = React.useState<boolean>(
      status || defaultValue || false
    );

    React.useEffect(() => {
      if (status != null) {
        setInternalValue(status);
      }
    }, [status]);

    // This special logic is to handle weird APIs like Airtable that return
    // true or undefined for a boolean
    const previousDefaultValue = usePrevious(defaultValue) as
      | boolean
      | undefined;

    React.useEffect(() => {
      if (defaultValue !== previousDefaultValue) {
        setInternalValue(Boolean(defaultValue));
      }
    }, [defaultValue, previousDefaultValue]);

    const { colors } = useTheme();

    const checkboxColor = internalValue
      ? color || colors.primary
      : uncheckedColor || colors.primary;

    const handlePress = () => {
      const newValue = !internalValue;

      setInternalValue(newValue);
      onPress?.(newValue);

      if (newValue) {
        onCheck?.();
      }

      if (!newValue) {
        onUncheck?.();
      }
    };

    return (
      <Touchable
        {...rest}
        onPress={handlePress}
        disabled={disabled}
        accessibilityState={{ disabled }}
        accessibilityRole="button"
        accessibilityLiveRegion="polite"
        style={[styles.container, style, { width: size, height: size }]}
      >
        <Icon
          style={styles.icon}
          name={internalValue ? checkedIcon : uncheckedIcon}
          size={size}
          color={checkboxColor}
        />
        <View style={[StyleSheet.absoluteFill, styles.fillContainer]}>
          <View
            style={[
              styles.fill,
              { opacity: disabled ? 0.5 : 1 },
              { borderColor: checkboxColor },
            ]}
          />
        </View>
      </Touchable>
    );
  }