Skip to content

Commit 2c5532d

Browse files
committed
feat(SegmantedControl): Add renderItem props.
1 parent 0b48aec commit 2c5532d

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

components/SegmentedControl/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,5 @@ function Demo() {
7373
| `size` | 按钮尺寸 | `small`, `default`, `large` | `default` |
7474
| `disabled` | 是否启用 | Boolean | `false` |
7575
| `selectedIndex` | 选中项在数组中的索引 | Number | 0 |
76+
| `renderItem` | 自定义单元格 | (data, index, rowNum): void | - |
7677
| `onValueChange` | 回调函数 | (selectedIndex): void | - |

components/SegmentedControl/index.tsx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,25 @@ import { ViewStyle, TextStyle } from 'react-native';
33
import ButtonGroup, { ButtonGroupProps } from '../ButtonGroup';
44
import Button from '../Button';
55

6-
export interface SegmentedControlProps extends ButtonGroupProps {
7-
value?: string[];
6+
export interface SegmentedControlProps<T> extends ButtonGroupProps {
7+
value?: string[] | T[];
88
selectedIndex?: number;
9+
renderItem?: (label: string | T, selectedIndex: number, props: ButtonGroupProps) => JSX.Element;
910
onValueChange?: (selectedIndex: number) => void;
1011
}
1112

1213
export interface SegmentedControlState {
1314
selectedIndex: number;
1415
}
1516

16-
export default class SegmentedControl extends Component<SegmentedControlProps, SegmentedControlState> {
17-
constructor(props: SegmentedControlProps) {
17+
export default class SegmentedControl<T> extends Component<SegmentedControlProps<T>, SegmentedControlState> {
18+
constructor(props: SegmentedControlProps<T>) {
1819
super(props);
1920
this.state = {
2021
selectedIndex: props.selectedIndex || 0,
2122
};
2223
}
23-
static defaultProps: SegmentedControlProps = {
24+
static defaultProps: SegmentedControlProps<{}> = {
2425
value: [],
2526
size: 'small',
2627
selectedIndex: 0,
@@ -33,23 +34,26 @@ export default class SegmentedControl extends Component<SegmentedControlProps, S
3334
});
3435
}
3536
render() {
36-
const { value, selectedIndex, ...otherProps } = this.props;
37+
const { value, selectedIndex, renderItem, ...otherProps } = this.props;
3738
return (
3839
<ButtonGroup {...otherProps}>
39-
{value!.map((label, key) => {
40+
{value && (value as (string | T)[]).map((label: string | T, key: number) => {
4041
const styl: ViewStyle = {};
4142
const textStyle: TextStyle = {};
4243
if (this.state.selectedIndex !== key + 1) {
4344
styl.backgroundColor = '#fff';
4445
textStyle.color = otherProps.color;
4546
}
46-
return React.cloneElement(<Button />, {
47-
key,
47+
const props: ButtonGroupProps = {
4848
type: 'primary',
4949
style: [styl, otherProps.textStyle],
5050
textStyle: [textStyle],
5151
onPress: this.handlePress.bind(this, key + 1),
52-
}, label);
52+
}
53+
if (renderItem) {
54+
return renderItem(label, key + 1, props);
55+
}
56+
return React.cloneElement(<Button key={key} />, { ...props }, label);
5357
})}
5458
</ButtonGroup>
5559
);

0 commit comments

Comments
 (0)