Skip to content

Feat/chips input v2 #1490

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 38 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
0468453
index.js -> index.tsx
mendyEdri Aug 17, 2021
ed9235c
added chips-input presenter
mendyEdri Aug 23, 2021
dec823a
set disable color logic code
mendyEdri Aug 23, 2021
56f253e
demo screen changes
mendyEdri Aug 23, 2021
6a66baf
migrate to typescript, added new chip usage supporting code
mendyEdri Aug 23, 2021
784001b
Merge branch 'master' into feat/chips-input-v2
mendyEdri Aug 23, 2021
eefbbc2
Fixed namings on tests
mendyEdri Aug 23, 2021
d4487db
typings
mendyEdri Aug 24, 2021
3cc26e2
build ts
mendyEdri Sep 5, 2021
3f7f91a
Merge branch 'master' into feat/chips-input-v2
mendyEdri Sep 5, 2021
6dbb8fa
build ts
mendyEdri Sep 5, 2021
1c8e43a
remove old typings
mendyEdri Sep 5, 2021
17944ea
rename chipsProps to chips
mendyEdri Sep 5, 2021
a6c176e
export new typings
mendyEdri Sep 5, 2021
3cf3ced
renaming
mendyEdri Sep 5, 2021
93123d0
export from generatedTypes
mendyEdri Sep 5, 2021
01ca150
fixed export path
mendyEdri Sep 5, 2021
a44e00e
added key for performance
mendyEdri Sep 9, 2021
fbffefa
Merge branch 'master' into feat/chips-input-v2
mendyEdri Oct 4, 2021
9585e78
rename method
mendyEdri Oct 20, 2021
650e717
missing import
mendyEdri Oct 20, 2021
38c6608
Merge branch 'master' into feat/chips-input-v2
mendyEdri Oct 20, 2021
d8537cf
rename method name in demo screen
mendyEdri Oct 20, 2021
ce1915a
ChipsInputScreen.js -> ChipsInputScreen.tsx
mendyEdri Oct 20, 2021
9fb87b1
migrate ChipsInputScreen to typescript
mendyEdri Oct 20, 2021
61689ca
add missing export prop
mendyEdri Oct 20, 2021
e098955
change spread position to override the default labelStyle
mendyEdri Oct 21, 2021
85aa6f2
Export chipsProps
mendyEdri Oct 21, 2021
532f470
Kept old usage for later reference, export new prop that contains `va…
mendyEdri Oct 24, 2021
71b9449
added deprecation note
mendyEdri Oct 24, 2021
2bb2ffa
use ChipsInputChipProp to have `invalid` prop in this type
mendyEdri Oct 24, 2021
d04fb89
Extract inline style
mendyEdri Oct 25, 2021
bfb8309
change array decleration to transpile with typescript
mendyEdri Oct 28, 2021
a6f9242
PR Comments
mendyEdri Nov 2, 2021
5d385b8
removed ref inline function
mendyEdri Nov 2, 2021
e0f0db0
move code inside other function
mendyEdri Nov 2, 2021
3497126
make CI great again
mendyEdri Nov 2, 2021
d5b3bc6
Update src/components/chipsInput/index.tsx
Inbal-Tish Nov 3, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,82 @@ import React, {Component} from 'react';
import {StyleSheet, ScrollView} from 'react-native';
import {View, Colors, Text, Typography, ChipsInput} from 'react-native-ui-lib'; // eslint-disable-line

interface State {
chips: Array<any>;
tags: Array<any>;
tags2: Array<string>;
tags3: Array<string>;
tags4: Array<string>;
}

export default class ChipsInputScreen extends Component<{}, State> {
customChipsInput: typeof ChipsInput;

export default class ChipsInputScreen extends Component {
constructor(props) {
constructor(props: any) {
super(props);

this.state = {
chips: [{label: 'Falcon 9'}, {label: 'Enterprise'}, {label: 'Challenger'}, {label: 'Coca Cola', invalid: true}],
tags: [{label: 'Amit'}, {label: 'Ethan', invalid: true}],
tags2: ['Chips', 'Input'],
tags3: ['Non', 'Removable', 'Tags'],
tags4: ['Change', 'Typography']
};
}

onTagPress = (tagIndex, markedTagIndex) => {
onTagPress = (tagIndex: number, markedTagIndex: number) => {
this.customChipsInput.markTagIndex(tagIndex === markedTagIndex ? undefined : tagIndex);
};

renderCustomTag(tag, index, shouldMarkToRemove) {
renderCustomTag(tag: any, _: any, shouldMarkToRemove: boolean) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

index param is of type number, not any

return (
<View style={[styles.customTag, shouldMarkToRemove && {backgroundColor: Colors.purple70}]}>
<Text white>{tag.label}</Text>
</View>
);
}

renderLeftElement = () => {
return (
<View center height={40} marginR-s2 style={{alignItems: 'flex-start'}}>
<Text grey30 text70M>
To:
</Text>
</View>
);
};

renderSearchTypeInput = () => {
return (
<>
<Text marginB-10 text60>Search Type</Text>
<View bg-grey60>
<ChipsInput
placeholder={'Enter Tags'}
chips={this.state.chips}
leftElement={this.renderLeftElement()}
hideUnderline
maxHeight={100}
/>
</View>
</>
);
};

renderFormTypeInput = () => {
return (
<View marginT-40>
<Text marginB-10 text60>Form Type</Text>
<ChipsInput
placeholder={'Enter Tags'}
title={'Mendy'}
chips={this.state.chips}
maxLength={4}
/>
</View>
);
};

render() {
return (
<ScrollView keyboardShouldPersistTaps="never">
Expand All @@ -35,12 +86,12 @@ export default class ChipsInputScreen extends Component {
ChipsInput
</Text>

<ChipsInput
containerStyle={{marginBottom: 25}}
placeholder="Enter Tags"
tags={this.state.tags2}
/>

{this.renderSearchTypeInput()}

{this.renderFormTypeInput()}

<Text text50 marginV-20>Old Usage</Text>
<ChipsInput
containerStyle={{marginBottom: 25}}
placeholder="Enter Tags"
Expand All @@ -57,12 +108,12 @@ export default class ChipsInputScreen extends Component {
/>

<ChipsInput
ref={r => (this.customChipsInput = r)}
ref={(r: typeof ChipsInput) => (this.customChipsInput = r)}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can't you use React.createRef()?

containerStyle={{marginBottom: 25}}
placeholder="With custom tags"
tags={this.state.tags}
renderTag={this.renderCustomTag}
onCreateTag={value => ({label: value})}
onCreateTag={(value: string) => ({label: value})}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Move to a class method please

onTagPress={this.onTagPress}
inputStyle={{...Typography.text60, color: Colors.blue30}}
/>
Expand Down
3 changes: 1 addition & 2 deletions generatedTypes/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export {default as TouchableOpacity, TouchableOpacityProps} from './src/componen
export {default as Button, ButtonSize, ButtonProps} from './src/components/button';
export {default as Stepper, StepperProps} from './src/components/stepper';
export {default as Checkbox, CheckboxProps} from './src/components/checkbox';
export {default as Chip, ChipProps} from './src/components/chip';
export {default as ChipsInput, ChipsInputProps} from './src/components/chipsInput';
export {default as ColorPicker, ColorPickerProps} from './src/components/colorPicker';
export {default as ColorPalette, ColorPaletteProps} from './src/components/colorPicker/ColorPalette';
export {default as ColorSwatch, ColorSwatchProps} from './src/components/colorPicker/ColorSwatch';
Expand Down Expand Up @@ -93,7 +93,6 @@ export {
TextArea,
MaskedInput,
ColorSliderGroup,
ChipsInput,
SharedTransition,
Toast,
WheelPickerDialog,
Expand Down
7 changes: 7 additions & 0 deletions generatedTypes/src/components/chipsInput/Presenter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ChipProps, ChipsInputProps } from './index';
export declare const isContainsInvalid: (chips: Array<ChipProps>) => boolean;
export declare const getValidationBasedColor: (chips: Array<ChipProps>, defaultChip?: ChipProps | undefined) => string;
export declare const getCounterTextColor: (stateChips: Array<ChipProps>, props: ChipsInputProps) => string;
export declare const getCounterText: (count: number, maxLength: number) => string;
export declare const getChipDismissColor: (chip: ChipProps, isSelected: boolean, defaultChipProps?: ChipProps | undefined) => string;
export declare const isDisabled: (props: ChipsInputProps) => boolean;
149 changes: 149 additions & 0 deletions generatedTypes/src/components/chipsInput/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import _ from 'lodash';
import React, { Component } from 'react';
import { ViewStyle, TextInput, NativeSyntheticEvent, TextInputKeyPressEventData, ScrollView, ScrollViewProps, TextInputProps as RNTextInputProps } from 'react-native';
import { BaseComponentInjectedProps, TypographyModifiers } from '../../commons/new';
import { ChipProps as ExternalChipProp } from '../chip';
import { TextFieldProps } from '../../../typings/components/Inputs';
declare type ChipType = string | boolean | any;
export declare type ChipProps = ExternalChipProp & {
invalid?: boolean;
};
export declare type ChipsInputProps = TypographyModifiers & TextFieldProps & {
/**
* DEPRECATED: use chips instead. list of tags. can be string boolean or custom object when implementing getLabel
*/
tags?: Array<ChipType>;
/**
* list of tags. can be string boolean or custom object when implementing getLabel
*/
chips?: Array<ChipProps>;
/**
* Style your chips
*/
defaultChipProps?: ChipProps;
/**
* callback for extracting the label out of the tag item
*/
getLabel?: (tag: ChipType) => any;
/**
* callback for custom rendering tag item
*/
renderTag?: (tag: ChipType, index: number, shouldMarkTag: boolean, label: string) => React.ReactElement;
/**
* callback for onChangeTags event
*/
onChangeTags?: () => void;
/**
* callback for creating new tag out of input value (good for composing tag object)
*/
onCreateTag?: (value: any) => void;
/**
* callback for when pressing a tag in the following format (tagIndex, markedTagIndex) => {...}
*/
onTagPress?: (index: number, toRemove?: number) => void;
/**
* validation message error appears when tag isn't validate
*/
validationErrorMessage?: string;
/**
* if true, tags *removal* Ux won't be available
*/
disableTagRemoval?: boolean;
/**
* if true, tags *adding* Ux (i.e. by 'submitting' the input text) won't be available
*/
disableTagAdding?: boolean;
/**
* custom styling for the component container
*/
containerStyle?: ViewStyle;
/**
* custom styling for the tag item
*/
tagStyle?: ViewStyle;
/**
* custom styling for the text input
*/
inputStyle?: RNTextInputProps['style'];
/**
* should hide input underline
*/
hideUnderline?: boolean;
/**
* Maximum numbers of chips
*/
maxLength?: number;
/**
* Chips with maxHeigh is inside a scrollView
*/
scrollViewProps?: ScrollViewProps;
/**
* Chips inside a ScrollView
*/
maxHeight?: number;
/**
* Custom element before the chips, for example 'search' icon, 'To:' label etc'
*/
leftElement?: JSX.Element | JSX.Element[];
value?: any;
selectionColor?: string | number;
};
declare type State = {
value: any;
chips: Array<ChipType>;
chipIndexToRemove?: number;
initialChips?: Array<ChipType>;
isFocused: boolean;
};
declare type OwnProps = ChipsInputProps & BaseComponentInjectedProps;
/**
* @description: Tags input component (chips)
* @modifiers: Typography
* @gif: https://github.com/wix/react-native-ui-lib/blob/master/demo/showcase/ChipsInput/ChipsInput.gif?raw=true
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/ChipsInputScreen.js
* @extends: TextField
*/
declare class ChipsInput extends Component<OwnProps, State> {
static displayName: string;
static onChangeTagsActions: {
ADDED: string;
REMOVED: string;
};
input: React.RefObject<TextInput>;
scrollRef: React.RefObject<ScrollView>;
constructor(props: OwnProps);
componentDidMount(): void;
static getDerivedStateFromProps(nextProps: Readonly<OwnProps>, prevState: State): {
initialChips: any[] | undefined;
chips: any[] | undefined;
} | null;
addTag: () => void;
removeMarkedTag(): void;
markTagIndex: (chipIndex: number) => void;
onChangeText: _.DebouncedFunc<(value: any) => void>;
onTagPress(index: number): void;
isLastTagMarked(): boolean;
onKeyPress: (event: NativeSyntheticEvent<TextInputKeyPressEventData>) => void;
getLabel: (item: ChipType) => any;
onFocus: () => void;
onBlur: () => void;
renderLabel(tag: ChipType, shouldMarkTag: boolean): JSX.Element;
renderTag: (tag: ChipType, index: number) => JSX.Element;
renderTagWrapper: (tag: ChipType, index: number) => JSX.Element;
renderNewChip: () => JSX.Element[];
renderTitleText: () => "" | JSX.Element | undefined;
renderChips: () => JSX.Element[];
renderCharCounter(): JSX.Element | undefined;
renderUnderline: () => JSX.Element;
renderTextInput(): JSX.Element;
renderChipsContainer: () => JSX.Element;
render(): JSX.Element;
blur(): void;
focus(): void;
clear(): void;
}
export { ChipsInput };
declare const _default: React.ComponentClass<ChipsInputProps & {
useCustomTheme?: boolean | undefined;
}, any> & typeof ChipsInput;
export default _default;
12 changes: 6 additions & 6 deletions generatedTypes/src/incubator/TextField/usePreset.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default function usePreset({ preset, ...props }: InternalTextFieldProps):
textAlign?: "left" | "right" | "center" | undefined;
textAlignVertical?: "auto" | "center" | "top" | "bottom" | undefined;
value?: string | undefined;
autoCapitalize?: "none" | "characters" | "sentences" | "words" | undefined;
autoCapitalize?: "none" | "sentences" | "words" | "characters" | undefined;
autoCorrect?: boolean | undefined;
autoFocus?: boolean | undefined;
blurOnSubmit?: boolean | undefined;
Expand Down Expand Up @@ -127,7 +127,7 @@ export default function usePreset({ preset, ...props }: InternalTextFieldProps):
selectionState?: import("react-native").DocumentSelectionState | undefined;
spellCheck?: boolean | undefined;
textContentType?: "none" | "name" | "password" | "username" | "URL" | "addressCity" | "addressCityAndState" | "addressState" | "countryName" | "creditCardNumber" | "emailAddress" | "familyName" | "fullStreetAddress" | "givenName" | "jobTitle" | "location" | "middleName" | "namePrefix" | "nameSuffix" | "nickname" | "organizationName" | "postalCode" | "streetAddressLine1" | "streetAddressLine2" | "sublocality" | "telephoneNumber" | "newPassword" | "oneTimeCode" | undefined;
autoCompleteType?: "name" | "password" | "username" | "email" | "off" | "cc-csc" | "cc-exp" | "cc-exp-month" | "cc-exp-year" | "cc-number" | "postal-code" | "street-address" | "tel" | undefined;
autoCompleteType?: "name" | "password" | "username" | "email" | "cc-csc" | "cc-exp" | "cc-exp-month" | "cc-exp-year" | "cc-number" | "postal-code" | "street-address" | "tel" | "off" | undefined;
importantForAutofill?: "auto" | "yes" | "no" | "noExcludeDescendants" | "yesExcludeDescendants" | undefined;
disableFullscreenUI?: boolean | undefined;
inlineImageLeft?: string | undefined;
Expand Down Expand Up @@ -416,7 +416,7 @@ export default function usePreset({ preset, ...props }: InternalTextFieldProps):
textAlign?: "left" | "right" | "center" | undefined;
textAlignVertical?: "auto" | "center" | "top" | "bottom" | undefined;
value?: string | undefined;
autoCapitalize?: "none" | "characters" | "sentences" | "words" | undefined;
autoCapitalize?: "none" | "sentences" | "words" | "characters" | undefined;
autoCorrect?: boolean | undefined;
autoFocus?: boolean | undefined;
blurOnSubmit?: boolean | undefined;
Expand Down Expand Up @@ -453,7 +453,7 @@ export default function usePreset({ preset, ...props }: InternalTextFieldProps):
selectionState?: import("react-native").DocumentSelectionState | undefined;
spellCheck?: boolean | undefined;
textContentType?: "none" | "name" | "password" | "username" | "URL" | "addressCity" | "addressCityAndState" | "addressState" | "countryName" | "creditCardNumber" | "emailAddress" | "familyName" | "fullStreetAddress" | "givenName" | "jobTitle" | "location" | "middleName" | "namePrefix" | "nameSuffix" | "nickname" | "organizationName" | "postalCode" | "streetAddressLine1" | "streetAddressLine2" | "sublocality" | "telephoneNumber" | "newPassword" | "oneTimeCode" | undefined;
autoCompleteType?: "name" | "password" | "username" | "email" | "off" | "cc-csc" | "cc-exp" | "cc-exp-month" | "cc-exp-year" | "cc-number" | "postal-code" | "street-address" | "tel" | undefined;
autoCompleteType?: "name" | "password" | "username" | "email" | "cc-csc" | "cc-exp" | "cc-exp-month" | "cc-exp-year" | "cc-number" | "postal-code" | "street-address" | "tel" | "off" | undefined;
importantForAutofill?: "auto" | "yes" | "no" | "noExcludeDescendants" | "yesExcludeDescendants" | undefined;
disableFullscreenUI?: boolean | undefined;
inlineImageLeft?: string | undefined;
Expand Down Expand Up @@ -854,7 +854,7 @@ export default function usePreset({ preset, ...props }: InternalTextFieldProps):
textAlign?: "left" | "right" | "center" | undefined;
textAlignVertical?: "auto" | "center" | "top" | "bottom" | undefined;
value?: string | undefined;
autoCapitalize?: "none" | "characters" | "sentences" | "words" | undefined;
autoCapitalize?: "none" | "sentences" | "words" | "characters" | undefined;
autoCorrect?: boolean | undefined;
autoFocus?: boolean | undefined;
blurOnSubmit?: boolean | undefined;
Expand Down Expand Up @@ -891,7 +891,7 @@ export default function usePreset({ preset, ...props }: InternalTextFieldProps):
selectionState?: import("react-native").DocumentSelectionState | undefined;
spellCheck?: boolean | undefined;
textContentType?: "none" | "name" | "password" | "username" | "URL" | "addressCity" | "addressCityAndState" | "addressState" | "countryName" | "creditCardNumber" | "emailAddress" | "familyName" | "fullStreetAddress" | "givenName" | "jobTitle" | "location" | "middleName" | "namePrefix" | "nameSuffix" | "nickname" | "organizationName" | "postalCode" | "streetAddressLine1" | "streetAddressLine2" | "sublocality" | "telephoneNumber" | "newPassword" | "oneTimeCode" | undefined;
autoCompleteType?: "name" | "password" | "username" | "email" | "off" | "cc-csc" | "cc-exp" | "cc-exp-month" | "cc-exp-year" | "cc-number" | "postal-code" | "street-address" | "tel" | undefined;
autoCompleteType?: "name" | "password" | "username" | "email" | "cc-csc" | "cc-exp" | "cc-exp-month" | "cc-exp-year" | "cc-number" | "postal-code" | "street-address" | "tel" | "off" | undefined;
importantForAutofill?: "auto" | "yes" | "no" | "noExcludeDescendants" | "yesExcludeDescendants" | undefined;
disableFullscreenUI?: boolean | undefined;
inlineImageLeft?: string | undefined;
Expand Down
35 changes: 35 additions & 0 deletions src/components/chipsInput/Presenter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {Colors} from '../../style';
import {ChipProps, ChipsInputProps} from './index';

export const hasInvalidChip = (chips: Array<ChipProps>) => {
return chips.filter((chip) => chip.invalid === true)[0] !== undefined;
};

export const getValidationBasedColor = (chips: Array<ChipProps>, defaultChip?: ChipProps) => {
const dismissColor = defaultChip?.dismissColor || Colors.red30;

return hasInvalidChip(chips) ? dismissColor : Colors.primary;
};

export const getCounterTextColor = (stateChips: Array<ChipProps>, props: ChipsInputProps) => {
const {maxLength} = props;
if (isDisabled(props)) {
return Colors.grey50;
}
return maxLength && stateChips.length >= maxLength ? Colors.red30 : Colors.grey30;
};

export const getCounterText = (count: number, maxLength: number) => {
return `${Math.min(count, maxLength)} / ${maxLength}`;
};

export const getChipDismissColor = (chip: ChipProps, isSelected: boolean, defaultChipProps?: ChipProps) => {
const dismissColor = defaultChipProps?.dismissColor || Colors.white;
return !chip.invalid ? dismissColor : isSelected ? Colors.red10 : Colors.red30;
};

export const isDisabled = (props: ChipsInputProps) => {
const {disableTagRemoval, editable} = props;
return disableTagRemoval || editable === false;
};

Loading