Skip to content

Commit 3e76660

Browse files
ChipScreen - adding example for dynamic label (#1253)
* ChipScreen - adding example for onPress to open Dialog and dynamic label. WheelPickerDialog - container self alignment and code cleanup. * example title * counter as badge example name * moving default values out of static * Chip - adding 'useCounter' boolean * WheelPicker - type fix Co-authored-by: Ethan Sharabi <[email protected]>
1 parent 36b1795 commit 3e76660

File tree

6 files changed

+202
-109
lines changed

6 files changed

+202
-109
lines changed

demo/src/screens/componentScreens/ChipScreen.tsx

Lines changed: 91 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,90 @@
1+
import _ from 'lodash';
12
import React, {Component} from 'react';
23
import {Alert} from 'react-native';
3-
import {Chip, Colors, Spacings, Text, Typography, View} from 'react-native-ui-lib';
4+
import {Chip, Colors, Spacings, Text, Typography, View, Dialog, WheelPickerDialog} from 'react-native-ui-lib';
45

56
const avatarImage = {
67
uri: 'https://randomuser.me/api/portraits/women/24.jpg'
78
};
89
const checkmark = require('../../assets/icons/check-small.png');
10+
const chevron = require('../../assets/icons/chevronDown.png');
11+
912

1013
export default class ChipScreen extends Component {
14+
15+
colors = [
16+
{value: Colors.red10, label: 'Red'},
17+
{value: Colors.blue10, label: 'Blue'},
18+
{value: Colors.purple10, label: 'Purple'},
19+
{value: Colors.green10, label: 'Green'},
20+
{value: Colors.yellow10, label: 'Yellow'}
21+
];
22+
23+
state = {
24+
showDialog: false,
25+
selectedValue: this.colors[2].label
26+
};
27+
28+
toggleDialog = (showDialog: boolean) => {
29+
this.setState({showDialog});
30+
};
31+
32+
openDialog = () => {
33+
this.toggleDialog(true);
34+
}
35+
36+
closeDialog = () => {
37+
this.toggleDialog(false);
38+
};
39+
40+
onSelect = (itemValue: string) => {
41+
const values = _.filter(this.colors, {value: itemValue});
42+
if (values.length > 0) {
43+
this.setState({selectedValue: values[0].label});
44+
}
45+
this.closeDialog();
46+
};
47+
48+
renderItem = ({item: color}) => {
49+
return (
50+
<Text text50 margin-20 color={color.value}>
51+
{color.label}
52+
</Text>
53+
);
54+
};
55+
56+
renderContent = () => {
57+
const {selectedValue} = this.state;
58+
59+
return (
60+
<WheelPickerDialog
61+
items={this.colors}
62+
selectedValue={selectedValue}
63+
onSelect={this.onSelect}
64+
onCancel={this.closeDialog}
65+
wheelPickerProps={{
66+
style: {width: 200}
67+
}}
68+
/>
69+
);
70+
};
71+
72+
renderPickerDialog = () => {
73+
const {showDialog} = this.state;
74+
75+
return (
76+
<Dialog
77+
migrate
78+
visible={showDialog}
79+
useSafeArea
80+
bottom
81+
onDismiss={this.closeDialog}
82+
>
83+
{this.renderContent()}
84+
</Dialog>
85+
);
86+
};
87+
1188
renderExample = (text: string, chip: JSX.Element) => {
1289
return (
1390
<View row spread marginB-12>
@@ -20,22 +97,23 @@ export default class ChipScreen extends Component {
2097
render() {
2198
return (
2299
<View style={{padding: 20}}>
100+
{this.state.showDialog && this.renderPickerDialog()}
23101
<Text marginB-20 text40>
24102
Chip
25103
</Text>
26104
<Text marginB-10 text70BO>
27105
Default
28106
</Text>
29107
{this.renderExample(
30-
'label',
108+
'Label',
31109
<Chip label={'Chip'}/>
32110
)}
33111
{this.renderExample(
34-
'label with onPress',
112+
'Label + onPress',
35113
<Chip label={'Chip'} onPress={() => Alert.alert('onPress')}/>
36114
)}
37115
{this.renderExample(
38-
'label + onDismiss',
116+
'Label + onDismiss',
39117
<Chip
40118
label={'Chip'}
41119
iconColor={Colors.black}
@@ -62,31 +140,31 @@ export default class ChipScreen extends Component {
62140
/>
63141
)}
64142
{this.renderExample(
65-
'Right icon',
143+
'Right icon + onPress + dynamic label',
66144
<Chip
67-
label={'Chip'}
68-
rightIconSource={checkmark}
69-
iconStyle={{width: 24, height: 24}}
70-
iconProps={{tintColor: Colors.black}}
145+
label={this.state.selectedValue}
146+
rightIconSource={chevron}
147+
iconStyle={{margin: 8}}
148+
onPress={this.openDialog}
71149
/>
72150
)}
73151
{this.renderExample(
74-
'label + Avatar',
152+
'Label + Avatar',
75153
<Chip
76154
label={'Chip'}
77155
avatarProps={{source: avatarImage, size: 20}}
78156
/>
79157
)}
80158
{this.renderExample(
81-
'label + Badge',
159+
'Label + Counter',
82160
<Chip
83161
label={'Chip'}
84162
labelStyle={{
85163
marginRight: undefined
86164
}}
165+
useCounter
87166
badgeProps={{
88167
label: '4',
89-
backgroundColor: 'transparent',
90168
labelStyle: {
91169
...Typography.text80R,
92170
color: Colors.grey20
@@ -95,7 +173,7 @@ export default class ChipScreen extends Component {
95173
/>
96174
)}
97175
{this.renderExample(
98-
'label + Badge',
176+
'Label + Badge',
99177
<Chip
100178
label={'Chip'}
101179
badgeProps={{

generatedTypes/components/chip/index.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ export declare type ChipProps = ViewProps & TouchableOpacityProps & {
5252
* Badge props object
5353
*/
5454
badgeProps?: BadgeProps;
55+
/**
56+
* Display badge as counter (no background)
57+
*/
58+
useCounter?: boolean;
5559
/**
5660
* Avatar props object
5761
*/
@@ -142,6 +146,10 @@ declare const _default: React.ComponentClass<ViewProps & TouchableOpacityProps &
142146
* Badge props object
143147
*/
144148
badgeProps?: BadgeProps | undefined;
149+
/**
150+
* Display badge as counter (no background)
151+
*/
152+
useCounter?: boolean | undefined;
145153
/**
146154
* Avatar props object
147155
*/
Lines changed: 46 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,49 @@
1-
/// <reference types="react" />
2-
import {TextStyle} from 'react-native';
3-
import {ItemProps} from './Item';
1+
import React from 'react';
2+
import { TextStyle, ViewStyle } from 'react-native';
3+
import { ItemProps } from './Item';
44
export interface WheelPickerProps {
5-
/**
6-
* Data source for WheelPicker
7-
*/
8-
items?: ItemProps[];
9-
/**
10-
* Describe the height of each item in the WheelPicker
11-
* default value: 44
12-
*/
13-
itemHeight?: number;
14-
/**
15-
* Describe the number of rows visible
16-
* default value: 5
17-
*/
18-
numberOfVisibleRows?: number;
19-
/**
20-
* Text color for the focused row
21-
*/
22-
activeTextColor?: string;
23-
/**
24-
* Text color for other, non-focused rows
25-
*/
26-
inactiveTextColor?: string;
27-
/**
28-
* Row text style
29-
*/
30-
textStyle?: TextStyle;
31-
/**
32-
* Event, on active row change
33-
*/
34-
onChange?: (item: string | undefined, index: number) => void;
35-
/**
36-
* Container's ViewStyle, height is computed according to itemHeight * numberOfVisibleRows
37-
*/
38-
style?: Omit<ViewStyle, 'height'>;
39-
/**
40-
* Support passing items as children props
41-
*/
42-
children?: JSX.Element | JSX.Element[];
43-
/**
44-
* WheelPicker initial value, can be ItemProps.value, number as index
45-
*/
46-
selectedValue?: ItemProps | string | number;
5+
/**
6+
* Data source for WheelPicker
7+
*/
8+
items?: ItemProps[];
9+
/**
10+
* Describe the height of each item in the WheelPicker
11+
* default value: 44
12+
*/
13+
itemHeight?: number;
14+
/**
15+
* Describe the number of rows visible
16+
* default value: 5
17+
*/
18+
numberOfVisibleRows?: number;
19+
/**
20+
* Text color for the focused row
21+
*/
22+
activeTextColor?: string;
23+
/**
24+
* Text color for other, non-focused rows
25+
*/
26+
inactiveTextColor?: string;
27+
/**
28+
* Row text style
29+
*/
30+
textStyle?: TextStyle;
31+
/**
32+
* Event, on active row change
33+
*/
34+
onChange?: (item: string | number, index: number) => void;
35+
/**
36+
* Container's ViewStyle, height is computed according to itemHeight * numberOfVisibleRows
37+
*/
38+
style?: Omit<ViewStyle, 'height'>;
39+
/**
40+
* Support passing items as children props
41+
*/
42+
children?: JSX.Element | JSX.Element[];
43+
/**
44+
* WheelPicker initial value, can be ItemProps.value, number as index
45+
*/
46+
selectedValue: ItemProps | number | string;
4747
}
48-
declare const WheelPicker: ({
49-
items,
50-
itemHeight,
51-
activeTextColor,
52-
inactiveTextColor,
53-
textStyle,
54-
onChange: onChangeEvent
55-
}: WheelPickerProps) => JSX.Element;
48+
declare const WheelPicker: React.MemoExoticComponent<({ items: propItems, itemHeight, numberOfVisibleRows, activeTextColor, inactiveTextColor, textStyle, onChange, style, children, selectedValue }: WheelPickerProps) => JSX.Element>;
5649
export default WheelPicker;

generatedTypes/incubator/WheelPicker/usePresenter.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/// <reference types="react" />
22
import { ItemProps } from './Item';
3-
declare type ItemValueTypes = ItemProps | number | string;
4-
export { ItemValueTypes };
3+
export declare type ItemValueTypes = ItemProps | number | string;
54
declare type PropTypes = {
65
selectedValue: ItemValueTypes;
76
children?: JSX.Element | JSX.Element[];

src/components/chip/index.tsx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ export type ChipProps = ViewProps & TouchableOpacityProps & {
6262
* Badge props object
6363
*/
6464
badgeProps?: BadgeProps;
65-
65+
/**
66+
* Display badge as counter (no background)
67+
*/
68+
useCounter?: boolean;
6669
//AVATAR
6770
/**
6871
* Avatar props object
@@ -115,6 +118,8 @@ export type ChipProps = ViewProps & TouchableOpacityProps & {
115118
}
116119
export type ChipPropTypes = ChipProps; //TODO: remove after ComponentPropTypes deprecation;
117120

121+
const DEFAULT_SIZE = 26;
122+
118123
/**
119124
* @description: Chip component
120125
* @extends: TouchableOpacity
@@ -125,11 +130,12 @@ const Chip = ({
125130
avatarProps,
126131
backgroundColor,
127132
badgeProps,
128-
borderRadius,
133+
useCounter,
134+
borderRadius = BorderRadiuses.br100,
129135
containerStyle,
130136
onDismiss,
131137
dismissColor,
132-
dismissIcon,
138+
dismissIcon = Assets.icons.x,
133139
dismissIconStyle,
134140
dismissContainerStyle,
135141
iconProps,
@@ -140,8 +146,8 @@ const Chip = ({
140146
labelStyle,
141147
onPress,
142148
resetSpacings,
143-
size,
144-
useSizeAsMinimum,
149+
size = DEFAULT_SIZE,
150+
useSizeAsMinimum = true,
145151
testID,
146152
...others
147153
}: ChipProps) => {
@@ -166,6 +172,7 @@ const Chip = ({
166172
<Badge
167173
size={BADGE_SIZES.default}
168174
testID={`${testID}.counter`}
175+
backgroundColor={useCounter ? 'transparent' : undefined}
169176
{...badgeProps}
170177
// @ts-ignore
171178
containerStyle={[getMargins('badge'), badgeProps.containerStyle]}
@@ -306,12 +313,6 @@ const Chip = ({
306313
};
307314

308315
Chip.displayName = 'Chip';
309-
Chip.defaultProps = {
310-
borderRadius: BorderRadiuses.br100,
311-
dismissIcon: Assets.icons.x,
312-
useSizeAsMinimum: true,
313-
size: 26
314-
};
315316

316317
const styles = StyleSheet.create({
317318
container: {

0 commit comments

Comments
 (0)