mirror of
https://github.com/zoriya/react-native-web.git
synced 2026-06-02 18:41:17 +00:00
[change] Switch prop 'trackColor' support
Remove support for legacy React Native props and add support for the 'trackColor' object. Retains support for the web-only equivalents as I think this API is preferable to the one in React Native, both in terms of flexibility and performance (no inline objects). Fix #1382
This commit is contained in:
@@ -26,7 +26,7 @@ The color of the thumb grip when the switch is turned on. (Web-only)
|
|||||||
|
|
||||||
### activeTrackColor
|
### activeTrackColor
|
||||||
|
|
||||||
The color of the track when the switch is turned on. (Web-only, equivalent to React Native's `onTintColor`)
|
The color of the track when the switch is turned on. (Web-only)
|
||||||
|
|
||||||
<Preview withSource='none'>
|
<Preview withSource='none'>
|
||||||
<Story name="activeTrackColor">
|
<Story name="activeTrackColor">
|
||||||
@@ -56,7 +56,7 @@ Invoked with the new value when the value changes.
|
|||||||
|
|
||||||
### thumbColor
|
### thumbColor
|
||||||
|
|
||||||
The color of the thumb grip when the switch is turned off. (Web-only, equivalent to React Native's `thumbTintColor`)
|
The color of the thumb grip when the switch is turned off.
|
||||||
|
|
||||||
<Preview withSource='none'>
|
<Preview withSource='none'>
|
||||||
<Story name="thumbColor">
|
<Story name="thumbColor">
|
||||||
@@ -66,7 +66,7 @@ The color of the thumb grip when the switch is turned off. (Web-only, equivalent
|
|||||||
|
|
||||||
### trackColor
|
### trackColor
|
||||||
|
|
||||||
The color of the track when the switch is turned off. (Web-only, equivalent to React Native's `tintColor`)
|
The color of the track when the switch is turned off.
|
||||||
|
|
||||||
<Preview withSource='none'>
|
<Preview withSource='none'>
|
||||||
<Story name="trackColor">
|
<Story name="trackColor">
|
||||||
|
|||||||
+16
-36
@@ -25,12 +25,8 @@ type SwitchProps = {
|
|||||||
disabled?: boolean,
|
disabled?: boolean,
|
||||||
onValueChange?: (e: any) => void,
|
onValueChange?: (e: any) => void,
|
||||||
thumbColor?: ColorValue,
|
thumbColor?: ColorValue,
|
||||||
trackColor?: ColorValue,
|
trackColor?: ColorValue | {| false: ColorValue, true: ColorValue |},
|
||||||
value?: boolean,
|
value?: boolean
|
||||||
// RN compat
|
|
||||||
onTintColor?: ColorValue,
|
|
||||||
thumbTintColor?: ColorValue,
|
|
||||||
tintColor?: ColorValue
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const emptyObject = {};
|
const emptyObject = {};
|
||||||
@@ -62,10 +58,6 @@ class Switch extends React.Component<SwitchProps> {
|
|||||||
thumbColor = '#FAFAFA',
|
thumbColor = '#FAFAFA',
|
||||||
trackColor = '#939393',
|
trackColor = '#939393',
|
||||||
value = false,
|
value = false,
|
||||||
// React Native compatibility
|
|
||||||
onTintColor,
|
|
||||||
thumbTintColor,
|
|
||||||
tintColor,
|
|
||||||
...other
|
...other
|
||||||
} = this.props;
|
} = this.props;
|
||||||
|
|
||||||
@@ -74,30 +66,33 @@ class Switch extends React.Component<SwitchProps> {
|
|||||||
const minWidth = multiplyStyleLengthValue(height, 2);
|
const minWidth = multiplyStyleLengthValue(height, 2);
|
||||||
const width = styleWidth > minWidth ? styleWidth : minWidth;
|
const width = styleWidth > minWidth ? styleWidth : minWidth;
|
||||||
const trackBorderRadius = multiplyStyleLengthValue(height, 0.5);
|
const trackBorderRadius = multiplyStyleLengthValue(height, 0.5);
|
||||||
const trackCurrentColor = value ? onTintColor || activeTrackColor : tintColor || trackColor;
|
const trackCurrentColor = value
|
||||||
const thumbCurrentColor = value ? activeThumbColor : thumbTintColor || thumbColor;
|
? (trackColor != null && typeof trackColor === 'object' && trackColor.true) ||
|
||||||
|
activeTrackColor
|
||||||
|
: (trackColor != null && typeof trackColor === 'object' && trackColor.false) || trackColor;
|
||||||
|
const thumbCurrentColor = value ? activeThumbColor : thumbColor;
|
||||||
const thumbHeight = height;
|
const thumbHeight = height;
|
||||||
const thumbWidth = thumbHeight;
|
const thumbWidth = thumbHeight;
|
||||||
|
|
||||||
const rootStyle = [styles.root, style, { height, width }, disabled && styles.cursorDefault];
|
const rootStyle = [styles.root, style, disabled && styles.cursorDefault, { height, width }];
|
||||||
|
|
||||||
const trackStyle = [
|
const trackStyle = [
|
||||||
styles.track,
|
styles.track,
|
||||||
{
|
{
|
||||||
backgroundColor: trackCurrentColor,
|
backgroundColor: disabled ? '#D5D5D5' : trackCurrentColor,
|
||||||
borderRadius: trackBorderRadius
|
borderRadius: trackBorderRadius
|
||||||
},
|
}
|
||||||
disabled && styles.disabledTrack
|
|
||||||
];
|
];
|
||||||
|
|
||||||
const thumbStyle = [
|
const thumbStyle = [
|
||||||
styles.thumb,
|
styles.thumb,
|
||||||
|
value && styles.thumbActive,
|
||||||
{
|
{
|
||||||
backgroundColor: thumbCurrentColor,
|
backgroundColor: disabled ? '#BDBDBD' : thumbCurrentColor,
|
||||||
height: thumbHeight,
|
height: thumbHeight,
|
||||||
|
marginStart: value ? multiplyStyleLengthValue(thumbWidth, -1) : 0,
|
||||||
width: thumbWidth
|
width: thumbWidth
|
||||||
},
|
}
|
||||||
disabled && styles.disabledThumb
|
|
||||||
];
|
];
|
||||||
|
|
||||||
const nativeControl = createElement('input', {
|
const nativeControl = createElement('input', {
|
||||||
@@ -115,16 +110,7 @@ class Switch extends React.Component<SwitchProps> {
|
|||||||
return (
|
return (
|
||||||
<View {...other} style={rootStyle}>
|
<View {...other} style={rootStyle}>
|
||||||
<View style={trackStyle} />
|
<View style={trackStyle} />
|
||||||
<View
|
<View ref={this._setThumbRef} style={thumbStyle} />
|
||||||
ref={this._setThumbRef}
|
|
||||||
style={[
|
|
||||||
thumbStyle,
|
|
||||||
value && styles.thumbOn,
|
|
||||||
{
|
|
||||||
marginStart: value ? multiplyStyleLengthValue(thumbWidth, -1) : 0
|
|
||||||
}
|
|
||||||
]}
|
|
||||||
/>
|
|
||||||
{nativeControl}
|
{nativeControl}
|
||||||
</View>
|
</View>
|
||||||
);
|
);
|
||||||
@@ -170,9 +156,6 @@ const styles = StyleSheet.create({
|
|||||||
transitionDuration: '0.1s',
|
transitionDuration: '0.1s',
|
||||||
width: '100%'
|
width: '100%'
|
||||||
},
|
},
|
||||||
disabledTrack: {
|
|
||||||
backgroundColor: '#D5D5D5'
|
|
||||||
},
|
|
||||||
thumb: {
|
thumb: {
|
||||||
alignSelf: 'flex-start',
|
alignSelf: 'flex-start',
|
||||||
borderRadius: '100%',
|
borderRadius: '100%',
|
||||||
@@ -181,12 +164,9 @@ const styles = StyleSheet.create({
|
|||||||
transform: [{ translateZ: 0 }],
|
transform: [{ translateZ: 0 }],
|
||||||
transitionDuration: '0.1s'
|
transitionDuration: '0.1s'
|
||||||
},
|
},
|
||||||
thumbOn: {
|
thumbActive: {
|
||||||
start: '100%'
|
start: '100%'
|
||||||
},
|
},
|
||||||
disabledThumb: {
|
|
||||||
backgroundColor: '#BDBDBD'
|
|
||||||
},
|
|
||||||
nativeControl: {
|
nativeControl: {
|
||||||
...StyleSheet.absoluteFillObject,
|
...StyleSheet.absoluteFillObject,
|
||||||
height: '100%',
|
height: '100%',
|
||||||
|
|||||||
Reference in New Issue
Block a user