react-native-paper
Version:
Material design for React Native
80 lines (79 loc) • 2.22 kB
JavaScript
import * as React from 'react';
import { StyleSheet, View } from 'react-native';
import ToggleButton from './ToggleButton';
import ToggleButtonGroup from './ToggleButtonGroup';
/**
* Toggle button row renders a group of toggle buttons in a row.
*
* ## Usage
* ```js
* import * as React from 'react';
* import { ToggleButton } from 'react-native-paper';
*
* const MyComponent = () => {
* const [value, setValue] = React.useState('left');
*
* return (
* <ToggleButton.Row onValueChange={value => setValue(value)} value={value}>
* <ToggleButton icon="format-align-left" value="left" />
* <ToggleButton icon="format-align-right" value="right" />
* </ToggleButton.Row>
* );
* };
*
* export default MyComponent;
*
*```
*/
const ToggleButtonRow = _ref => {
let {
value,
onValueChange,
children,
style
} = _ref;
const count = React.Children.count(children);
return /*#__PURE__*/React.createElement(ToggleButtonGroup, {
value: value,
onValueChange: onValueChange
}, /*#__PURE__*/React.createElement(View, {
style: [styles.row, style]
}, React.Children.map(children, (child, i) => {
// @ts-expect-error: TypeScript complains about child.type but it doesn't matter
if (child && child.type === ToggleButton) {
// @ts-expect-error: We're sure that child is a React Element
return /*#__PURE__*/React.cloneElement(child, {
style: [styles.button, i === 0 ? styles.first : i === count - 1 ? styles.last : styles.middle,
// @ts-expect-error: We're sure that child is a React Element
child.props.style]
});
}
return child;
})));
};
ToggleButtonRow.displayName = 'ToggleButton.Row';
const styles = StyleSheet.create({
row: {
flexDirection: 'row'
},
button: {
borderWidth: StyleSheet.hairlineWidth
},
first: {
borderTopRightRadius: 0,
borderBottomRightRadius: 0
},
middle: {
borderRadius: 0,
borderLeftWidth: 0
},
last: {
borderLeftWidth: 0,
borderTopLeftRadius: 0,
borderBottomLeftRadius: 0
}
});
export default ToggleButtonRow;
// @component-docs ignore-next-line
export { ToggleButtonRow };
//# sourceMappingURL=ToggleButtonRow.js.map