Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 31 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,35 +68,37 @@ const options = [
### Props

| Prop | Type | Default | Required | Note |
| ------------------------- | ----------------------- | ----------- | -------- | -------------------------------------------------------------------------------- |
| options | array | null | true | Items array to render. Each item has a label and a value and optionals icons |
| options[].label | string | null | true | Label from each item |
| options[].value | string | null | true | Value from each item |
| options[].customIcon | Jsx element ou Function | null | false | Optional custom icon from each item |
| options[].imageIcon | string | null | false | Source from a image icon form each item. Has the same color then label in render |
| options[].activeColor | string | null | false | Color from each item when is selected |
| initial | number | 0 | true | Item selected in initial render |
| value | number | undefined | false | The switch value (will call onPress) |
| onPress | function | console.log | true | Callback function called after change value. |
| disableValueChangeOnPress | bool | false | false | Disables the onPress call when the value is manually changed |
| fontSize | number | null | false | Font size from labels. If null default fontSize of the app is used. |
| selectedColor | string | '#fff' | false | Color text of the item selected |
| buttonColor | string | '#BCD635' | false | Color bg of the item selected |
| textColor | string | '#000' | false | Color text of the not selecteds items |
| backgroundColor | string | '#ffffff' | false | Color bg of the component |
| borderColor | string | '#c9c9c9' | false | Border Color of the component |
| borderRadius | number | 50 | false | Border Radius of the component |
| hasPadding | bool | false | false | Indicate if item has padding |
| animationDuration | number | 250 | false | Duration of the animation |
| valuePadding | number | 1 | false | Size of padding |
| height | number | 40 | false | Height of component |
| bold | bool | false | false | Indicate if text has fontWeight bold |
| textStyle | object | {} | false | Text style |
| selectedTextStyle | object | {} | false | Selected text style |
| imageStyle | object | {} | false | Image style |
| style | object | {} | false | Container style |
| returnObject | bool | false | false | Indicate if onPress function return an option instead of option.value |
| disabled | bool | false | false | Disables the switch |
| ------------------------- | ----------------------- | ----------- | -------- | -------------------------------------------------------------------------------- |
| options | array | null | true | Items array to render. Each item has a label and a value and optionals icons |
| options[].label | string | null | true | Label from each item |
| options[].value | string | null | true | Value from each item |
| options[].customIcon | Jsx element ou Function | null | false | Optional custom icon from each item |
| options[].imageIcon | string | null | false | Source from a image icon form each item. Has the same color then label in render |
| options[].activeColor | string | null | false | Color from each item when is selected |
| initial | number | 0 | true | Item selected in initial render |
| value | number | undefined | false | The switch value (will call onPress) |
| onPress | function | console.log | true | Callback function called after change value. |
| disableValueChangeOnPress | bool | false | false | Disables the onPress call when the value is manually changed |
| fontSize | number | null | false | Font size from labels. If null default fontSize of the app is used. |
| selectedColor | string | '#fff' | false | Color text of the item selected |
| buttonColor | string | '#BCD635' | false | Color bg of the item selected |
| textColor | string | '#000' | false | Color text of the not selecteds items |
| backgroundColor | string | '#ffffff' | false | Color bg of the component |
| borderColor | string | '#c9c9c9' | false | Border Color of the component |
| borderRadius | number | 50 | false | Border Radius of the component |
| hasPadding | bool | false | false | Indicate if item has padding |
| animationDuration | number | 250 | false | Duration of the animation |
| valuePadding | number | 1 | false | Size of padding |
| height | number | 40 | false | Height of component |
| bold | bool | false | false | Indicate if text has fontWeight bold |
| textStyle | object | {} | false | Text style |
| selectedTextStyle | object | {} | false | Selected text style |
| textContainerStyle | object | {} | false | Style for text (and icon) container (TouchableOpacity) |
| selectedTextContainerStyle | object | {} | false | Style for selected text (and icon) container (TouchableOpacity) |
| imageStyle | object | {} | false | Image style |
| style | object | {} | false | Container style |
| returnObject | bool | false | false | Indicate if onPress function return an option instead of option.value |
| disabled | bool | false | false | Disables the switch |

### Authors

Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ declare module "react-native-switch-selector" {
bold?: boolean;
textStyle?: TextStyle | RegisteredStyle<TextStyle>;
selectedTextStyle?: TextStyle | RegisteredStyle<TextStyle>;
textCStyle?: TextStyle | RegisteredStyle<TextStyle>;
selectedTextContainerStyle?: TextStyle | RegisteredStyle<TextStyle>;
imageStyle?: ImageStyle | RegisteredStyle<ImageStyle>;
style?: ViewStyle | RegisteredStyle<ViewStyle>;
returnObject?: boolean;
Expand Down
80 changes: 44 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ export default class SwitchSelector extends Component {
style,
textStyle,
selectedTextStyle,
textContainerStyle,
selectedTextContainerStyle,
imageStyle,
textColor,
selectedColor,
Expand All @@ -141,46 +143,50 @@ export default class SwitchSelector extends Component {
disabled
} = this.props;

const options = this.props.options.map((element, index) => (
<TouchableOpacity
key={index}
disabled={disabled}
style={styles.button}
onPress={() => this.toggleItem(index)}
>
{typeof element.customIcon === "function"
? element.customIcon(this.state.selected == index)
: element.customIcon}
{element.imageIcon && (
<Image
source={element.imageIcon}
const options = this.props.options.map((element, index) => {
const is_selected = this.state.selected == index;

return (
<TouchableOpacity
key={index}
disabled={disabled}
style={[styles.button, is_selected ? selectedTextContainerStyle : textContainerStyle]}
onPress={() => this.toggleItem(index)}
>
{typeof element.customIcon === "function"
? element.customIcon(is_selected)
: element.customIcon}
{element.imageIcon && (
<Image
source={element.imageIcon}
style={[
{
height: 30,
width: 30,
tintColor:
is_selected ? selectedColor : textColor
},
imageStyle
]}
/>
)}
<Text
style={[
{
height: 30,
width: 30,
tintColor:
this.state.selected == index ? selectedColor : textColor
fontSize,
fontWeight: bold ? "bold" : "normal",
textAlign: "center",
color: is_selected ? selectedColor : textColor,
backgroundColor: "transparent"
},
imageStyle
is_selected ? selectedTextStyle : textStyle
]}
/>
)}
<Text
style={[
{
fontSize,
fontWeight: bold ? "bold" : "normal",
textAlign: "center",
color: this.state.selected == index ? selectedColor : textColor,
backgroundColor: "transparent"
},
this.state.selected == index ? selectedTextStyle : textStyle
]}
>
{element.label}
</Text>
</TouchableOpacity>
));
>
{element.label}
</Text>
</TouchableOpacity>
)
});

return (
<View style={[{ flexDirection: "row" }, style]}>
Expand Down Expand Up @@ -248,6 +254,8 @@ SwitchSelector.defaultProps = {
style: {},
textStyle: {},
selectedTextStyle: {},
textContainerStyle: {},
selectedTextContainerStyle: {},
imageStyle: {},
textColor: "#000000",
selectedColor: "#FFFFFF",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-switch-selector",
"version": "1.1.14",
"version": "1.1.15",
"description": "Switch Selector to React Native.",
"main": "index.js",
"scripts": {
Expand Down