Skip to content

ChipScreen - adding example for dynamic label #1253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 8, 2021
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
104 changes: 91 additions & 13 deletions demo/src/screens/componentScreens/ChipScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,90 @@
import _ from 'lodash';
import React, {Component} from 'react';
import {Alert} from 'react-native';
import {Chip, Colors, Spacings, Text, Typography, View} from 'react-native-ui-lib';
import {Chip, Colors, Spacings, Text, Typography, View, Dialog, WheelPickerDialog} from 'react-native-ui-lib';

const avatarImage = {
uri: 'https://randomuser.me/api/portraits/women/24.jpg'
};
const checkmark = require('../../assets/icons/check-small.png');
const chevron = require('../../assets/icons/chevronDown.png');


export default class ChipScreen extends Component {

colors = [
{value: Colors.red10, label: 'Red'},
{value: Colors.blue10, label: 'Blue'},
{value: Colors.purple10, label: 'Purple'},
{value: Colors.green10, label: 'Green'},
{value: Colors.yellow10, label: 'Yellow'}
];

state = {
showDialog: false,
selectedValue: this.colors[2].label
};

toggleDialog = (showDialog: boolean) => {
this.setState({showDialog});
};

openDialog = () => {
this.toggleDialog(true);
}

closeDialog = () => {
this.toggleDialog(false);
};

onSelect = (itemValue: string) => {
const values = _.filter(this.colors, {value: itemValue});
if (values.length > 0) {
this.setState({selectedValue: values[0].label});
}
this.closeDialog();
};

renderItem = ({item: color}) => {
return (
<Text text50 margin-20 color={color.value}>
{color.label}
</Text>
);
};

renderContent = () => {
const {selectedValue} = this.state;

return (
<WheelPickerDialog
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you add a picker to the chip example screen?
Don't you think it's making the example screen a bit more complicated than needed?

What do you want to demonstrate here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was a request by ux

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh ok :/

items={this.colors}
selectedValue={selectedValue}
onSelect={this.onSelect}
onCancel={this.closeDialog}
wheelPickerProps={{
style: {width: 200}
}}
/>
);
};

renderPickerDialog = () => {
const {showDialog} = this.state;

return (
<Dialog
migrate
visible={showDialog}
useSafeArea
bottom
onDismiss={this.closeDialog}
>
{this.renderContent()}
</Dialog>
);
};

renderExample = (text: string, chip: JSX.Element) => {
return (
<View row spread marginB-12>
Expand All @@ -20,22 +97,23 @@ export default class ChipScreen extends Component {
render() {
return (
<View style={{padding: 20}}>
{this.state.showDialog && this.renderPickerDialog()}
<Text marginB-20 text40>
Chip
</Text>
<Text marginB-10 text70BO>
Default
</Text>
{this.renderExample(
'label',
'Label',
<Chip label={'Chip'}/>
)}
{this.renderExample(
'label with onPress',
'Label + onPress',
<Chip label={'Chip'} onPress={() => Alert.alert('onPress')}/>
)}
{this.renderExample(
'label + onDismiss',
'Label + onDismiss',
<Chip
label={'Chip'}
iconColor={Colors.black}
Expand All @@ -62,31 +140,31 @@ export default class ChipScreen extends Component {
/>
)}
{this.renderExample(
'Right icon',
'Right icon + onPress + dynamic label',
<Chip
label={'Chip'}
rightIconSource={checkmark}
iconStyle={{width: 24, height: 24}}
iconProps={{tintColor: Colors.black}}
label={this.state.selectedValue}
rightIconSource={chevron}
iconStyle={{margin: 8}}
onPress={this.openDialog}
/>
)}
{this.renderExample(
'label + Avatar',
'Label + Avatar',
<Chip
label={'Chip'}
avatarProps={{source: avatarImage, size: 20}}
/>
)}
{this.renderExample(
'label + Badge',
'Label + Counter',
<Chip
label={'Chip'}
labelStyle={{
marginRight: undefined
}}
useCounter
badgeProps={{
label: '4',
backgroundColor: 'transparent',
labelStyle: {
...Typography.text80R,
color: Colors.grey20
Expand All @@ -95,7 +173,7 @@ export default class ChipScreen extends Component {
/>
)}
{this.renderExample(
'label + Badge',
'Label + Badge',
<Chip
label={'Chip'}
badgeProps={{
Expand Down
8 changes: 8 additions & 0 deletions generatedTypes/components/chip/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export declare type ChipProps = ViewProps & TouchableOpacityProps & {
* Badge props object
*/
badgeProps?: BadgeProps;
/**
* Display badge as counter (no background)
*/
useCounter?: boolean;
/**
* Avatar props object
*/
Expand Down Expand Up @@ -142,6 +146,10 @@ declare const _default: React.ComponentClass<ViewProps & TouchableOpacityProps &
* Badge props object
*/
badgeProps?: BadgeProps | undefined;
/**
* Display badge as counter (no background)
*/
useCounter?: boolean | undefined;
/**
* Avatar props object
*/
Expand Down
99 changes: 46 additions & 53 deletions generatedTypes/incubator/WheelPicker/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,49 @@
/// <reference types="react" />
import {TextStyle} from 'react-native';
import {ItemProps} from './Item';
import React from 'react';
import { TextStyle, ViewStyle } from 'react-native';
import { ItemProps } from './Item';
export interface WheelPickerProps {
/**
* Data source for WheelPicker
*/
items?: ItemProps[];
/**
* Describe the height of each item in the WheelPicker
* default value: 44
*/
itemHeight?: number;
/**
* Describe the number of rows visible
* default value: 5
*/
numberOfVisibleRows?: number;
/**
* Text color for the focused row
*/
activeTextColor?: string;
/**
* Text color for other, non-focused rows
*/
inactiveTextColor?: string;
/**
* Row text style
*/
textStyle?: TextStyle;
/**
* Event, on active row change
*/
onChange?: (item: string | undefined, index: number) => void;
/**
* Container's ViewStyle, height is computed according to itemHeight * numberOfVisibleRows
*/
style?: Omit<ViewStyle, 'height'>;
/**
* Support passing items as children props
*/
children?: JSX.Element | JSX.Element[];
/**
* WheelPicker initial value, can be ItemProps.value, number as index
*/
selectedValue?: ItemProps | string | number;
/**
* Data source for WheelPicker
*/
items?: ItemProps[];
/**
* Describe the height of each item in the WheelPicker
* default value: 44
*/
itemHeight?: number;
/**
* Describe the number of rows visible
* default value: 5
*/
numberOfVisibleRows?: number;
/**
* Text color for the focused row
*/
activeTextColor?: string;
/**
* Text color for other, non-focused rows
*/
inactiveTextColor?: string;
/**
* Row text style
*/
textStyle?: TextStyle;
/**
* Event, on active row change
*/
onChange?: (item: string | number, index: number) => void;
/**
* Container's ViewStyle, height is computed according to itemHeight * numberOfVisibleRows
*/
style?: Omit<ViewStyle, 'height'>;
/**
* Support passing items as children props
*/
children?: JSX.Element | JSX.Element[];
/**
* WheelPicker initial value, can be ItemProps.value, number as index
*/
selectedValue: ItemProps | number | string;
}
declare const WheelPicker: ({
items,
itemHeight,
activeTextColor,
inactiveTextColor,
textStyle,
onChange: onChangeEvent
}: WheelPickerProps) => JSX.Element;
declare const WheelPicker: React.MemoExoticComponent<({ items: propItems, itemHeight, numberOfVisibleRows, activeTextColor, inactiveTextColor, textStyle, onChange, style, children, selectedValue }: WheelPickerProps) => JSX.Element>;
export default WheelPicker;
3 changes: 1 addition & 2 deletions generatedTypes/incubator/WheelPicker/usePresenter.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/// <reference types="react" />
import { ItemProps } from './Item';
declare type ItemValueTypes = ItemProps | number | string;
export { ItemValueTypes };
export declare type ItemValueTypes = ItemProps | number | string;
declare type PropTypes = {
selectedValue: ItemValueTypes;
children?: JSX.Element | JSX.Element[];
Expand Down
23 changes: 12 additions & 11 deletions src/components/chip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ export type ChipProps = ViewProps & TouchableOpacityProps & {
* Badge props object
*/
badgeProps?: BadgeProps;

/**
* Display badge as counter (no background)
*/
useCounter?: boolean;
//AVATAR
/**
* Avatar props object
Expand Down Expand Up @@ -115,6 +118,8 @@ export type ChipProps = ViewProps & TouchableOpacityProps & {
}
export type ChipPropTypes = ChipProps; //TODO: remove after ComponentPropTypes deprecation;

const DEFAULT_SIZE = 26;

/**
* @description: Chip component
* @extends: TouchableOpacity
Expand All @@ -125,11 +130,12 @@ const Chip = ({
avatarProps,
backgroundColor,
badgeProps,
borderRadius,
useCounter,
borderRadius = BorderRadiuses.br100,
containerStyle,
onDismiss,
dismissColor,
dismissIcon,
dismissIcon = Assets.icons.x,
dismissIconStyle,
dismissContainerStyle,
iconProps,
Expand All @@ -140,8 +146,8 @@ const Chip = ({
labelStyle,
onPress,
resetSpacings,
size,
useSizeAsMinimum,
size = DEFAULT_SIZE,
useSizeAsMinimum = true,
testID,
...others
}: ChipProps) => {
Expand All @@ -166,6 +172,7 @@ const Chip = ({
<Badge
size={BADGE_SIZES.default}
testID={`${testID}.counter`}
backgroundColor={useCounter ? 'transparent' : undefined}
{...badgeProps}
// @ts-ignore
containerStyle={[getMargins('badge'), badgeProps.containerStyle]}
Expand Down Expand Up @@ -306,12 +313,6 @@ const Chip = ({
};

Chip.displayName = 'Chip';
Chip.defaultProps = {
borderRadius: BorderRadiuses.br100,
dismissIcon: Assets.icons.x,
useSizeAsMinimum: true,
size: 26
};

const styles = StyleSheet.create({
container: {
Expand Down
Loading