0.0.1 • Published 4 years ago

react-native-segmented-controller v0.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

React Native Segmented Controller

npm version npm download

Installation

npm install react-native-segmented-controller

or

yarn add react-native-segmented-controller

Usage

import SegmentedController from "react-native-segmented-controller";

Props

NameTypeDefaultDescription
datastring[]requiredThe labels for the control's segment buttons, in order.
selectedIndexnumber0The index in data of the segment to be (pre)selected.
activeBackgroundstring"#157aff"Color background when selected
activeTextstringColor text when selected
inactiveTextstringColor text when no selected
separateColorstring"rgba(0,0,0,0.1)"Color separator bar between segments
renderText(item: object)
renderItem(item: object,index: number,color: object,focused: boolean)
onChange(item: object, index: number)Callback that is called when the user taps a segment; passes the event as an argument
disabledbooleanfalseIf true the user won't be able to interact with the control
activeOpacitynumber0.7Opacity when click
containerStylesViewStyle
textStylesTextStyle
separateStylesViewStyle

Examples

1 - Default

export default function index() {
  const [value, setvalue] = useState(0);
  return (
    <View>
      <View style={{ margin: 20 }}>
        <Text>default</Text>
      </View>
      <Segmented
        data={["One", "Two"]}
        selectedIndex={value}
        containerStyles={{ marginHorizontal: 20 }}
        onChange={(item, index) => setvalue(index)}
      />
    </View>
  );
}

2 - Customizing render with renderText

export default function index() {
  const [value, setvalue] = useState(0);
  const data = [
    { color: "red" },
    { color: "purple" },
    { color: "blue" },
    { color: "pink" },
    { color: "green" },
  ];
  return (
    <View>
      <View style={{ margin: 20 }}>
        <Text>renderText</Text>
      </View>
      <Segmented
        data={data}
        activeBackground={data[value].color}
        selectedIndex={value}
        containerStyles={{ marginHorizontal: 20 }}
        renderText={(item) => item.color}
        onChange={(item, index) => setvalue(index)}
      />
    </View>
  );
}

3 - Customizing render with renderItem

export default function index() {
  const [value, setvalue] = useState(0);
  const data = [
    { color: "red" },
    { color: "purple" },
    { color: "blue" },
    { color: "pink" },
    { color: "green" },
  ];
  return (
    <View>
      <View style={{ margin: 20 }}>
        <Text>renderItem</Text>
      </View>
      <Segmented
        data={data}
        activeBackground={data[value].color}
        selectedIndex={value}
        containerStyles={{ marginHorizontal: 20 }}
        renderItem={(item, index, color, focused) => {
          return (
            <Icon
              name="circle"
              color={focused ? color.activeText : item.color}
              size={20}
              solid
            />
          );
        }}
        onChange={(item, index) => setvalue(index)}
      />
    </View>
  );
}

License

MIT

Pull requests

Always welcome!