Skip to content

Commit 1c0ce07

Browse files
committed
chore(SegmentedControl): SegmentedControl 设置color属性,文字颜色 (#256)
1 parent 312b8d3 commit 1c0ce07

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

example/examples/src/routes/SegmentedControl/index.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ export default class SegmentedControlView extends React.Component<SegmentedContr
3737
value={['申请', '审批', '提交']}
3838
/>
3939
</Card>
40+
<Card
41+
title={
42+
'设置自定义文本颜色 textColor?: [选中: string, 未选中: string]'
43+
}>
44+
<SegmentedControl
45+
textColor={['#333', '#ccc']}
46+
color="#999"
47+
selectedIndex={2}
48+
value={['申请', '审批', '提交']}
49+
/>
50+
</Card>
4051
<Card title={'设置间距 gutter?: number'}>
4152
<SegmentedControl
4253
gutter={10}

packages/core/src/SegmentedControl/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ function Demo() {
7171
| `style` | 自定义样式 | Object | {} |
7272
| `value` | 初始值 | String[] | [] |
7373
| `color` | 组件主色调 | String | `#108ee9` |
74+
| `textColor` | 文本颜色 | [选中颜色: string, 未选中颜色: string] | `[#fff, color]` |
7475
| `size` | 按钮尺寸 | `small`, `default`, `large` | `default` |
7576
| `disabled` | 是否启用 | Boolean | `false` |
7677
| `selectedIndex` | 选中项在数组中的索引 | Number | 0 |

packages/core/src/SegmentedControl/index.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Component } from 'react';
2-
import { ViewStyle, TextStyle } from 'react-native';
2+
import { ViewStyle, TextStyle, Text } from 'react-native';
33
import ButtonGroup, { ButtonGroupProps } from '../ButtonGroup';
44
import Button from '../Button';
55

@@ -8,6 +8,7 @@ export interface SegmentedControlProps<T> extends ButtonGroupProps {
88
selectedIndex?: number;
99
renderItem?: (label: string | T, selectedIndex: number, props: ButtonGroupProps) => JSX.Element;
1010
onValueChange?: (label: string | T, selectedIndex: number) => void;
11+
textColor?: [string, string];
1112
}
1213

1314
export interface SegmentedControlState {
@@ -35,16 +36,24 @@ export default class SegmentedControl<T> extends Component<SegmentedControlProps
3536
};
3637
render() {
3738
// eslint-disable-next-line @typescript-eslint/no-unused-vars
38-
const { value, selectedIndex, renderItem, ...otherProps } = this.props;
39+
const {
40+
value,
41+
selectedIndex,
42+
renderItem,
43+
textColor = ['#fff', this.props.color ?? '#108ee9'],
44+
...otherProps
45+
} = this.props;
3946
return (
4047
<ButtonGroup {...otherProps}>
4148
{value &&
4249
(value as (string | T)[]).map((label: string | T, key: number) => {
4350
const styl: ViewStyle = {};
4451
const textStyle: TextStyle = {};
52+
let textStyleColor: string = textColor[0];
4553
if (this.state.selectedIndex !== key + 1) {
4654
styl.backgroundColor = '#fff';
4755
textStyle.color = otherProps.color;
56+
textStyleColor = textColor[1];
4857
}
4958
const props: ButtonGroupProps = {
5059
type: 'primary',
@@ -55,7 +64,11 @@ export default class SegmentedControl<T> extends Component<SegmentedControlProps
5564
if (renderItem) {
5665
return renderItem(label, key + 1, props);
5766
}
58-
return React.cloneElement(<Button key={key} />, { ...props }, label);
67+
return React.cloneElement(
68+
<Button key={key} />,
69+
{ ...props },
70+
<Text style={{ color: textStyleColor }}>{label}</Text>,
71+
);
5972
})}
6073
</ButtonGroup>
6174
);

0 commit comments

Comments
 (0)