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
Changes from 1 commit
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
56 changes: 32 additions & 24 deletions src/components/chipsInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {TextField} from '../inputs';
import View from '../view';
import TouchableOpacity from '../touchableOpacity';
import Text from '../text';
import Chip, {ChipProps as ExternalChipProp} from '../chip';
import Chip, {ChipProps} from '../chip';
import {getValidationBasedColor, getCounterTextColor, getCounterText, getChipDismissColor, isDisabled} from './Presenter';
import {TextFieldProps} from '../../../typings/components/Inputs';

Expand All @@ -20,7 +20,7 @@ import {TextFieldProps} from '../../../typings/components/Inputs';
// TODO: add notes to Docs about the Android fix for onKeyPress

type ChipType = string | boolean | any;
export type ChipProps = ExternalChipProp & {invalid?: boolean}
export type ChipsInputChipProps = ChipProps & {invalid?: boolean}

export type ChipsInputProps = TypographyModifiers & TextFieldProps & {
/**
Expand All @@ -30,29 +30,29 @@ export type ChipsInputProps = TypographyModifiers & TextFieldProps & {
/**
* list of tags. can be string boolean or custom object when implementing getLabel
*/
chips?: Array<ChipProps>;
chips?: Array<ChipsInputChipProps>;
/**
* Style your chips
*/
defaultChipProps?: ChipProps;
defaultChipProps?: ChipsInputChipProps;
/**
* callback for extracting the label out of the tag item
*/
getLabel?: (tag: ChipType) => any;
/**
* callback for custom rendering tag item
* DEPRECATED: use chips instead. 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)
* DEPRECATED: use chips instead. 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) => {...}
* DEPRECATED: use chips instead. callback for when pressing a tag in the following format (tagIndex, markedTagIndex) => {...}
*/
onTagPress?: (index: number, toRemove?: number) => void;
/**
Expand Down Expand Up @@ -254,29 +254,34 @@ class ChipsInput extends Component<OwnProps, State> {
return isLastTagMarked;
}

removeTag = () => {
const {value, chips, chipIndexToRemove} = this.state;
const tagsCount = _.size(chips);
const hasNoValue = _.isEmpty(value);
const hasTags = tagsCount > 0;

if (hasNoValue && hasTags && _.isUndefined(chipIndexToRemove)) {
this.setState({
chipIndexToRemove: tagsCount - 1
});
} else if (!_.isUndefined(chipIndexToRemove)) {
this.removeMarkedTag();
}
}

onKeyPress = (event: NativeSyntheticEvent<TextInputKeyPressEventData>) => {
_.invoke(this.props, 'onKeyPress', event);

const {disableTagRemoval} = this.props;
if (disableTagRemoval) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

this logic should move to removeTag method

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't see any changes here...

return;
}

const {value, chips, chipIndexToRemove} = this.state;
const tagsCount = _.size(chips);

const keyCode = _.get(event, 'nativeEvent.key');
const hasNoValue = _.isEmpty(value);
const pressedBackspace = keyCode === Constants.backspaceKey;
const hasTags = tagsCount > 0;

if (pressedBackspace) {
if (hasNoValue && hasTags && _.isUndefined(chipIndexToRemove)) {
this.setState({
chipIndexToRemove: tagsCount - 1
});
} else if (!_.isUndefined(chipIndexToRemove)) {
this.removeMarkedTag();
}
this.removeTag();
}
}

Expand All @@ -300,15 +305,15 @@ class ChipsInput extends Component<OwnProps, State> {
this.setState({isFocused: false});
}

renderLabel(tag: ChipType, shouldMarkTag: boolean) {
renderLabel(tag: ChipType, shouldMarkTag: boolean) {
const {typography} = this.props.modifiers;
const label = this.getLabel(tag);

return (
<View row centerV>
{shouldMarkTag && (
<Image
style={[styles.removeIcon, tag.invalid && styles.invalidTagRemoveIcon, styles.basicTagStyle]}
style={[styles.removeIcon, tag.invalid && styles.basicTagStyle && styles.invalidTagRemoveIcon]}
source={Assets.icons.x}
/>)
}
Expand Down Expand Up @@ -362,7 +367,7 @@ class ChipsInput extends Component<OwnProps, State> {
const selected = chipIndexToRemove === index;
const dismissColor = getChipDismissColor(chip, selected, defaultChipProps);
return (
<View center flexS>
<View center flexS marginT-2 marginB-2>
<Chip
key={index}
containerStyle={[styles.tag, chip.invalid && styles.invalidTag]}
Expand Down Expand Up @@ -466,7 +471,7 @@ class ChipsInput extends Component<OwnProps, State> {
onBlur={this.onBlur}
hideUnderline
selectionColor={isLastTagMarked ? 'transparent' : selectionColor}
style={[inputStyle, {textAlignVertical: 'center'}]}
style={[inputStyle, styles.alignTextCenter]}
containerStyle={{flexGrow: 0}}
collapsable={false}
accessibilityHint={
Expand Down Expand Up @@ -583,7 +588,7 @@ const styles = StyleSheet.create({
borderColor: Colors.red10
},
tagMarked: {
backgroundColor: Colors.dark10
backgroundColor: Colors.grey10
},
dismissIconStyle: {
width: 10,
Expand Down Expand Up @@ -615,5 +620,8 @@ const styles = StyleSheet.create({
alignSelf: 'flex-end',
height: Typography.text80?.lineHeight,
...Typography.text80
},
alignTextCenter: {
textAlignVertical: 'center'
}
});