@prosperitainova/dumbo-react-native
Version:
Dumbo for React Native Library
95 lines (93 loc) • 2.73 kB
JavaScript
;
import React from 'react';
import { StyleSheet, Switch, View } from 'react-native';
import { getColor } from '../../styles/colors';
import { styleReferenceBreaker } from '../../helpers';
import { getTextInputStyle } from '../BaseTextInputs';
import { Text } from '../Text';
/** Props for Toggle component */
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
* Toggle component for rendering a boolean toggle switch
* This component utilizes Native Switch from iOS and Android.
* It is styled to be similar to Carbon.
* But relies on the final styling from the OS.
*
* {@link https://github.com/carbon-design-system/carbon-react-native/blob/main/example/src/Views/Toggle.tsx | Example code}
*/
export class Toggle extends React.Component {
get styles() {
return StyleSheet.create({
wrapper: {
paddingTop: 22
},
switchWrapper: {
flexDirection: 'row',
paddingTop: 4
},
selectedText: {
marginLeft: 8,
marginTop: 4
}
});
}
get textInputStyles() {
return getTextInputStyle();
}
get trackColor() {
return {
false: getColor('toggleOff'),
true: getColor('supportSuccess')
};
}
onChange = value => {
const {
onChange
} = this.props;
if (typeof onChange === 'function') {
onChange(value);
}
};
render() {
const {
disabled,
componentProps,
hideLabel,
label,
helperText,
style,
selectedLabelText,
toggled,
toggleWrapperStyle
} = this.props;
return /*#__PURE__*/_jsxs(View, {
style: styleReferenceBreaker(this.styles.wrapper, style),
accessible: true,
accessibilityLabel: label,
accessibilityHint: helperText,
children: [!!(label && !hideLabel) && /*#__PURE__*/_jsx(Text, {
style: this.textInputStyles.label,
type: "label-02",
text: label
}), /*#__PURE__*/_jsxs(View, {
style: styleReferenceBreaker(this.styles.switchWrapper, toggleWrapperStyle),
children: [/*#__PURE__*/_jsx(Switch, {
value: toggled,
onValueChange: this.onChange,
disabled: disabled,
trackColor: this.trackColor,
thumbColor: getColor('iconOnColor'),
...(componentProps || {})
}), !!selectedLabelText && /*#__PURE__*/_jsx(Text, {
style: this.styles.selectedText,
text: toggled ? selectedLabelText.on : selectedLabelText.off
})]
}), !!helperText && /*#__PURE__*/_jsx(Text, {
style: this.textInputStyles.helperText,
type: "helper-text-02",
text: helperText
})]
});
}
}
//# sourceMappingURL=index.js.map