mirror of
https://github.com/zoriya/react-native-web.git
synced 2026-06-08 12:32:58 +00:00
[fix] Switch support for custom colors when disabled
Close #2164 Fix #1758
This commit is contained in:
@@ -24,6 +24,14 @@ export default function SwitchPage() {
|
|||||||
<Switch disabled={true} value={false} />
|
<Switch disabled={true} value={false} />
|
||||||
<Divider />
|
<Divider />
|
||||||
<Switch disabled={true} value={true} />
|
<Switch disabled={true} value={true} />
|
||||||
|
<Divider />
|
||||||
|
<Switch activeThumbColor="#fff" activeTrackColor="#E0245E" disabled={true} value={true} />
|
||||||
|
<Divider />
|
||||||
|
<Switch disabled={true} thumbColor="#fff" trackColor="#E0245E" value={false} />
|
||||||
|
<Divider />
|
||||||
|
<Switch disabled={true} trackColor={{ true: '#E0245E', false: '#1DA1F2' }} value={false} />
|
||||||
|
<Divider />
|
||||||
|
<Switch disabled={true} trackColor={{ true: '#E0245E', false: '#1DA1F2' }} value={true} />
|
||||||
</View>
|
</View>
|
||||||
<View style={styles.row}>
|
<View style={styles.row}>
|
||||||
<Switch value={false} />
|
<Switch value={false} />
|
||||||
|
|||||||
+43
@@ -0,0 +1,43 @@
|
|||||||
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||||
|
|
||||||
|
exports[`components/Switch disabled when "true" and value="false", a disabled checkbox is rendered with provided false value of trackColor 1`] = `
|
||||||
|
<div
|
||||||
|
class="css-view-1dbjc4n r-bottom-1p0dtai r-forcedColorAdjust-1c6unfx r-height-1dernwh r-left-1d2f490 r-margin-ywje51 r-position-u8s1d r-right-zchlnj r-top-ipm5af r-transitionDuration-13tjlyg r-width-13qz1uu"
|
||||||
|
style="background-color: rgb(29, 161, 242); border-top-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; border-bottom-left-radius: 10px;"
|
||||||
|
/>
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`components/Switch disabled when "true" and value="false", a disabled checkbox is rendered with provided thumbColor 1`] = `
|
||||||
|
<div
|
||||||
|
class="css-view-1dbjc4n r-alignSelf-k200y r-borderRadius-1j16mh1 r-boxShadow-1ewcgjf r-forcedColorAdjust-1c6unfx r-left-1fe0xdi r-transform-1sncvnh r-transitionDuration-13tjlyg"
|
||||||
|
style="background-color: rgb(255, 255, 255); height: 20px; margin-left: 0px; width: 20px;"
|
||||||
|
/>
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`components/Switch disabled when "true" and value="false", a disabled checkbox is rendered with provided trackColor 1`] = `
|
||||||
|
<div
|
||||||
|
class="css-view-1dbjc4n r-bottom-1p0dtai r-forcedColorAdjust-1c6unfx r-height-1dernwh r-left-1d2f490 r-margin-ywje51 r-position-u8s1d r-right-zchlnj r-top-ipm5af r-transitionDuration-13tjlyg r-width-13qz1uu"
|
||||||
|
style="background-color: rgb(224, 36, 94); border-top-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; border-bottom-left-radius: 10px;"
|
||||||
|
/>
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`components/Switch disabled when "true" and value="true", a disabled checkbox is rendered with provided activeThumbColor 1`] = `
|
||||||
|
<div
|
||||||
|
class="css-view-1dbjc4n r-alignSelf-k200y r-borderRadius-1j16mh1 r-boxShadow-1ewcgjf r-forcedColorAdjust-1c6unfx r-left-7b7h2f r-transform-1sncvnh r-transitionDuration-13tjlyg"
|
||||||
|
style="background-color: rgb(255, 255, 255); height: 20px; margin-left: -20px; width: 20px;"
|
||||||
|
/>
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`components/Switch disabled when "true" and value="true", a disabled checkbox is rendered with provided activeTrackColor 1`] = `
|
||||||
|
<div
|
||||||
|
class="css-view-1dbjc4n r-bottom-1p0dtai r-forcedColorAdjust-1c6unfx r-height-1dernwh r-left-1d2f490 r-margin-ywje51 r-position-u8s1d r-right-zchlnj r-top-ipm5af r-transitionDuration-13tjlyg r-width-13qz1uu"
|
||||||
|
style="background-color: rgb(224, 36, 94); border-top-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; border-bottom-left-radius: 10px;"
|
||||||
|
/>
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`components/Switch disabled when "true" and value="true", a disabled checkbox is rendered with provided true value of trackColor 1`] = `
|
||||||
|
<div
|
||||||
|
class="css-view-1dbjc4n r-bottom-1p0dtai r-forcedColorAdjust-1c6unfx r-height-1dernwh r-left-1d2f490 r-margin-ywje51 r-position-u8s1d r-right-zchlnj r-top-ipm5af r-transitionDuration-13tjlyg r-width-13qz1uu"
|
||||||
|
style="background-color: rgb(224, 36, 94); border-top-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; border-bottom-left-radius: 10px;"
|
||||||
|
/>
|
||||||
|
`;
|
||||||
@@ -8,6 +8,14 @@ function findCheckbox(container) {
|
|||||||
return container.firstChild.querySelector('input');
|
return container.firstChild.querySelector('input');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function findSwitchTrack(container) {
|
||||||
|
return container.firstChild.querySelector('div');
|
||||||
|
}
|
||||||
|
|
||||||
|
function findSwitchThumb(container) {
|
||||||
|
return container.firstChild.childNodes[1];
|
||||||
|
}
|
||||||
|
|
||||||
describe('components/Switch', () => {
|
describe('components/Switch', () => {
|
||||||
test('accessibilityLabel is applied to native checkbox', () => {
|
test('accessibilityLabel is applied to native checkbox', () => {
|
||||||
const { container } = render(<Switch accessibilityLabel="switch" />);
|
const { container } = render(<Switch accessibilityLabel="switch" />);
|
||||||
@@ -24,6 +32,40 @@ describe('components/Switch', () => {
|
|||||||
const { container } = render(<Switch disabled />);
|
const { container } = render(<Switch disabled />);
|
||||||
expect(findCheckbox(container).disabled).toBe(true);
|
expect(findCheckbox(container).disabled).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('when "true" and value="true", a disabled checkbox is rendered with provided activeTrackColor', () => {
|
||||||
|
const { container } = render(<Switch activeTrackColor="#E0245E" disabled value={true} />);
|
||||||
|
expect(findSwitchTrack(container)).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('when "true" and value="true", a disabled checkbox is rendered with provided activeThumbColor', () => {
|
||||||
|
const { container } = render(<Switch activeThumbColor="#fff" disabled value={true} />);
|
||||||
|
expect(findSwitchThumb(container)).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('when "true" and value="false", a disabled checkbox is rendered with provided trackColor', () => {
|
||||||
|
const { container } = render(<Switch disabled trackColor="#E0245E" value={false} />);
|
||||||
|
expect(findSwitchTrack(container)).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('when "true" and value="false", a disabled checkbox is rendered with provided thumbColor', () => {
|
||||||
|
const { container } = render(<Switch disabled thumbColor="#fff" value={false} />);
|
||||||
|
expect(findSwitchThumb(container)).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('when "true" and value="true", a disabled checkbox is rendered with provided true value of trackColor', () => {
|
||||||
|
const { container } = render(
|
||||||
|
<Switch disabled={true} trackColor={{ true: '#E0245E', false: '#1DA1F2' }} value={true} />
|
||||||
|
);
|
||||||
|
expect(findSwitchTrack(container)).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('when "true" and value="false", a disabled checkbox is rendered with provided false value of trackColor', () => {
|
||||||
|
const { container } = render(
|
||||||
|
<Switch disabled={true} trackColor={{ true: '#E0245E', false: '#1DA1F2' }} value={false} />
|
||||||
|
);
|
||||||
|
expect(findSwitchTrack(container)).toMatchSnapshot();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('onValueChange', () => {
|
describe('onValueChange', () => {
|
||||||
|
|||||||
+60
-9
@@ -31,19 +31,27 @@ const emptyObject = {};
|
|||||||
const thumbDefaultBoxShadow = '0px 1px 3px rgba(0,0,0,0.5)';
|
const thumbDefaultBoxShadow = '0px 1px 3px rgba(0,0,0,0.5)';
|
||||||
const thumbFocusedBoxShadow = `${thumbDefaultBoxShadow}, 0 0 0 10px rgba(0,0,0,0.1)`;
|
const thumbFocusedBoxShadow = `${thumbDefaultBoxShadow}, 0 0 0 10px rgba(0,0,0,0.1)`;
|
||||||
|
|
||||||
|
const defaultActiveTrackColor = '#A3D3CF';
|
||||||
|
const defaultTrackColor = '#939393';
|
||||||
|
const defaultDisabledTrackColor = '#D5D5D5';
|
||||||
|
|
||||||
|
const defaultActiveThumbColor = '#009688';
|
||||||
|
const defaultThumbColor = '#FAFAFA';
|
||||||
|
const defaultDisabledThumbColor = '#BDBDBD';
|
||||||
|
|
||||||
const Switch: React.AbstractComponent<
|
const Switch: React.AbstractComponent<
|
||||||
SwitchProps,
|
SwitchProps,
|
||||||
React.ElementRef<typeof View>
|
React.ElementRef<typeof View>
|
||||||
> = React.forwardRef((props, forwardedRef) => {
|
> = React.forwardRef((props, forwardedRef) => {
|
||||||
const {
|
const {
|
||||||
accessibilityLabel,
|
accessibilityLabel,
|
||||||
activeThumbColor = '#009688',
|
activeThumbColor,
|
||||||
activeTrackColor = '#A3D3CF',
|
activeTrackColor,
|
||||||
disabled = false,
|
disabled = false,
|
||||||
onValueChange,
|
onValueChange,
|
||||||
style = emptyObject,
|
style = emptyObject,
|
||||||
thumbColor = '#FAFAFA',
|
thumbColor,
|
||||||
trackColor = '#939393',
|
trackColor,
|
||||||
value = false,
|
value = false,
|
||||||
...other
|
...other
|
||||||
} = props;
|
} = props;
|
||||||
@@ -69,31 +77,74 @@ const Switch: React.AbstractComponent<
|
|||||||
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 = (function () {
|
const trackCurrentColor = (function () {
|
||||||
if (value === true) {
|
if (value === true) {
|
||||||
if (trackColor != null && typeof trackColor === 'object') {
|
if (trackColor != null && typeof trackColor === 'object') {
|
||||||
return trackColor.true;
|
return trackColor.true;
|
||||||
} else {
|
} else {
|
||||||
return activeTrackColor;
|
return activeTrackColor ?? defaultActiveTrackColor;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (trackColor != null && typeof trackColor === 'object') {
|
if (trackColor != null && typeof trackColor === 'object') {
|
||||||
return trackColor.false;
|
return trackColor.false;
|
||||||
} else {
|
} else {
|
||||||
return trackColor;
|
return trackColor ?? defaultTrackColor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
const thumbCurrentColor = value ? activeThumbColor : thumbColor;
|
|
||||||
|
const thumbCurrentColor = value
|
||||||
|
? activeThumbColor ?? defaultActiveThumbColor
|
||||||
|
: thumbColor ?? defaultThumbColor;
|
||||||
|
|
||||||
const thumbHeight = height;
|
const thumbHeight = height;
|
||||||
const thumbWidth = thumbHeight;
|
const thumbWidth = thumbHeight;
|
||||||
|
|
||||||
const rootStyle = [styles.root, style, disabled && styles.cursorDefault, { height, width }];
|
const rootStyle = [styles.root, style, disabled && styles.cursorDefault, { height, width }];
|
||||||
|
|
||||||
|
const disabledTrackColor = (function () {
|
||||||
|
if (value === true) {
|
||||||
|
if (
|
||||||
|
(typeof activeTrackColor === 'string' && activeTrackColor != null) ||
|
||||||
|
(typeof trackColor === 'object' && trackColor?.true)
|
||||||
|
) {
|
||||||
|
return trackCurrentColor;
|
||||||
|
} else {
|
||||||
|
return defaultDisabledTrackColor;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (
|
||||||
|
(typeof trackColor === 'string' && trackColor != null) ||
|
||||||
|
(typeof trackColor === 'object' && trackColor?.false)
|
||||||
|
) {
|
||||||
|
return trackCurrentColor;
|
||||||
|
} else {
|
||||||
|
return defaultDisabledTrackColor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
const disabledThumbColor = (function () {
|
||||||
|
if (value === true) {
|
||||||
|
if (activeThumbColor == null) {
|
||||||
|
return defaultDisabledThumbColor;
|
||||||
|
} else {
|
||||||
|
return thumbCurrentColor;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (thumbColor == null) {
|
||||||
|
return defaultDisabledThumbColor;
|
||||||
|
} else {
|
||||||
|
return thumbCurrentColor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
const trackStyle = [
|
const trackStyle = [
|
||||||
styles.track,
|
styles.track,
|
||||||
{
|
{
|
||||||
backgroundColor: disabled ? '#D5D5D5' : trackCurrentColor,
|
backgroundColor: disabled ? disabledTrackColor : trackCurrentColor,
|
||||||
borderRadius: trackBorderRadius
|
borderRadius: trackBorderRadius
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
@@ -102,7 +153,7 @@ const Switch: React.AbstractComponent<
|
|||||||
styles.thumb,
|
styles.thumb,
|
||||||
value && styles.thumbActive,
|
value && styles.thumbActive,
|
||||||
{
|
{
|
||||||
backgroundColor: disabled ? '#BDBDBD' : thumbCurrentColor,
|
backgroundColor: disabled ? disabledThumbColor : thumbCurrentColor,
|
||||||
height: thumbHeight,
|
height: thumbHeight,
|
||||||
marginStart: value ? multiplyStyleLengthValue(thumbWidth, -1) : 0,
|
marginStart: value ? multiplyStyleLengthValue(thumbWidth, -1) : 0,
|
||||||
width: thumbWidth
|
width: thumbWidth
|
||||||
|
|||||||
Reference in New Issue
Block a user