Skip to content

Commit

Permalink
feat: support Accessibility
Browse files Browse the repository at this point in the history
  • Loading branch information
1uokun committed Mar 1, 2022
1 parent 3c13f53 commit 90579f7
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 30 deletions.
5 changes: 3 additions & 2 deletions components/activity-indicator/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react'
import {
ActivityIndicator,
ColorValue,
StyleProp,
Text,
View,
StyleProp,
ViewStyle,
} from 'react-native'
import { WithTheme, WithThemeStyles } from '../style'
Expand All @@ -12,7 +13,7 @@ import ActivityIndicatorStyles, { ActivityIndicatorStyle } from './style/index'
export interface ActivityIndicatorNativeProps
extends WithThemeStyles<ActivityIndicatorStyle> {
styles?: { [key: string]: StyleProp<ViewStyle> }
color?: string
color?: ColorValue
animating?: boolean
toast?: boolean
size?: number | 'large' | 'small'
Expand Down
14 changes: 12 additions & 2 deletions components/button/ButtonWave.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,28 @@ interface ButtonWaveProps extends TouchableNativeFeedbackProps {

export default (props: ButtonWaveProps) => {
const { Color, ...restProps } = props
const accessibilityState = {
disabled: !!props.disabled,
}
return IS_ANDROID ? (
<TouchableNativeFeedback
accessibilityRole="button"
accessibilityState={accessibilityState}
background={
Platform.Version >= 21
? TouchableNativeFeedback.Ripple(Color || '', true, 13)
: TouchableNativeFeedback.SelectableBackground()
}
useForeground={true}
{...restProps}>
{props.children}
<>{props.children}</>
</TouchableNativeFeedback>
) : (
<Pressable {...restProps}>{props.children}</Pressable>
<Pressable
accessibilityRole="button"
accessibilityState={accessibilityState}
{...restProps}>
{props.children}
</Pressable>
)
}
2 changes: 2 additions & 0 deletions components/button/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ export default class Button extends React.Component<ButtonProps, any> {

return (
<TouchableHighlight
accessibilityRole="button"
accessibilityState={{ disabled: !!disabled }}
activeOpacity={0.4}
{...restProps}
style={wrapperStyle}
Expand Down
24 changes: 14 additions & 10 deletions components/checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import classNames from 'classnames'
import useMergedState from 'rc-util/lib/hooks/useMergedState'
import * as React from 'react'
import {
AccessibilityProps,
Animated,
Easing,
Pressable,
Expand All @@ -19,7 +20,8 @@ import CheckboxStyles, { CheckboxStyle } from './style/index'

export interface CheckboxProps
extends CheckboxPropsType,
WithThemeStyles<CheckboxStyle> {
WithThemeStyles<CheckboxStyle>,
AccessibilityProps {
style?: StyleProp<ViewStyle>
}
//TODO: ref interface
Expand Down Expand Up @@ -139,20 +141,22 @@ const InternalCheckbox = (
? _styles.checkbox_checked?.borderColor
: _styles.checkbox?.borderColor
return (
<View style={[_styles[`${prefixCls}_wrapper`], style]}>
<View
accessibilityRole="checkbox"
accessibilityState={{ checked: innerChecked, disabled }}
style={[_styles[`${prefixCls}_wrapper`], style]}>
<View style={_styles.checkbox_wave}>
<ButtonWave
style={antd_checkbox}
Color={Color}
disabled={disabled}
onPress={this.onPress}>
<View style={antd_checkbox}>
<Animated.View
style={[antd_checkbox_inner, transitionOpacity]}
/>
<Animated.View
style={[transitionTransform, antd_checkbox_inner_after]}
/>
</View>
<Animated.View
style={[antd_checkbox_inner, transitionOpacity]}
/>
<Animated.View
style={[transitionTransform, antd_checkbox_inner_after]}
/>
</ButtonWave>
</View>
<Pressable disabled={disabled} onPress={this.onPress}>
Expand Down
5 changes: 5 additions & 0 deletions components/radio/Radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ const InternalRadio = (
<WithTheme themeStyles={RadioStyle} styles={styles}>
{(_styles) => (
<Checkbox
accessibilityRole="radio"
accessibilityState={{
checked: restProps.checked,
disabled: restProps.disabled,
}}
{...restProps}
ref={ref}
indeterminate={false}
Expand Down
6 changes: 5 additions & 1 deletion components/radio/RadioGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ const RadioGroup = React.forwardRef<any, RadioGroupProps>(
}

return (
<View style={style} ref={ref}>
<View
accessibilityRole="radiogroup"
accessibilityState={{ checked: value, disabled }}
style={style}
ref={ref}>
<RadioGroupContextProvider
value={{
onChange: onRadioChange,
Expand Down
5 changes: 4 additions & 1 deletion components/switch/PropsType.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { StyleProp, ViewStyle } from 'react-native'
import { ColorValue, StyleProp, ViewStyle } from 'react-native'

export type SwitchChangeEventHandler = (checked: boolean) => void
export type SwitchClickEventHandler = SwitchChangeEventHandler
Expand All @@ -9,6 +9,9 @@ export interface SwitchPropsType {
checked?: boolean
defaultChecked?: boolean
color?: string
trackColor?: { false?: ColorValue; true?: ColorValue }

This comment has been minimized.

Copy link
@1uokun

1uokun Mar 7, 2022

Author Collaborator
thumbColor?: ColorValue
thumbTintColor?: ColorValue
disabled?: boolean
loading?: boolean
styles?: { [key: string]: StyleProp<ViewStyle> }
Expand Down
45 changes: 35 additions & 10 deletions components/switch/Switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const AntmSwitch = ({
unCheckedChildren,
onPress,
onChange,
trackColor,
thumbColor,
thumbTintColor,
...restProps
}: SwitchPropsType) => {
devWarning(
Expand Down Expand Up @@ -61,7 +64,7 @@ const AntmSwitch = ({
const transitionWidth = {
width: animatedValue2.interpolate({
inputRange: [0, 1],
outputRange: [18, 24],
outputRange: [22, 28],
}),
left: !innerChecked
? animatedValue.interpolate({
Expand Down Expand Up @@ -143,16 +146,33 @@ const AntmSwitch = ({
.map((a) => styles[a])

// color props
const SwitchCheckedColor = {
backgroundColor: color,
borderColor: color,
}
const Color = innerChecked
? color || theme.switch_fill
: theme.switch_unchecked
? color || trackColor?.true || theme.switch_fill
: trackColor?.false || theme.switch_unchecked

const SwitchTrackColor = {
backgroundColor: Color,
borderColor: Color,
opacity: Color ? (disabled ? 0.6 : 1) : 1,
}

const SwitchThumbColor = JSON.parse(
JSON.stringify({
backgroundColor: innerChecked ? thumbTintColor : thumbColor,
}),
)

const accessibilityState = {
checked: innerChecked,
disabled,
busy: loading,
}

return (
<View style={[styles[prefixCls], { padding: 1 }]}>
<View
accessibilityRole="switch"
accessibilityState={accessibilityState}
style={[styles[prefixCls], { padding: 1 }]}>
<ButtonWave
{...restProps}
Color={Color}
Expand All @@ -163,9 +183,14 @@ const AntmSwitch = ({
<View
style={[
ant_switch,
Boolean(color && innerChecked) && SwitchCheckedColor,
Boolean(trackColor || color) && SwitchTrackColor,
]}>
<Animated.View style={[ant_switch_handle, transitionWidth]}>
<Animated.View
style={[
ant_switch_handle,
SwitchThumbColor,
transitionWidth,
]}>
{loading && (
<RNActivityIndicator
color={Color}
Expand Down
8 changes: 4 additions & 4 deletions components/switch/style/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export default (theme: Theme) =>
StyleSheet.create<SwitchStyle>({
switch: {
position: 'relative',
minWidth: 44,
minHeight: 22,
minWidth: 50,
minHeight: 25,
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
Expand All @@ -34,8 +34,8 @@ export default (theme: Theme) =>
// handle
switch_handle: {
position: 'absolute',
width: 18,
height: 18,
width: 22,
height: 22,
borderRadius: 999,
backgroundColor: '#ffffff',
},
Expand Down

0 comments on commit 90579f7

Please sign in to comment.