-
Notifications
You must be signed in to change notification settings - Fork 734
Feat/new chips input #1681
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
Feat/new chips input #1681
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
24311c1
Remove redundant internal view wrapper in Incubator.TextField to allo…
ethanshar 16debbc
Create new ChipsInput component under Incubator
ethanshar 699c8c7
Add Incubator.ChipsInput example screen
ethanshar d4ad8ec
Fix issue with chip is still marked while entering new text
ethanshar ab713a1
Change defaultChipProps prop to be optional
ethanshar a37af8d
Merge branch 'master' into feat/NewChipsInput
ethanshar e43fe63
Remove ts-expect-error and use stylesheet
ethanshar 1b59da7
Support maximum chips in ChipsInput
ethanshar 42513f6
Forward ref in Incubator.ChipsInput
ethanshar b8de1d0
Merge branch 'master' into feat/NewChipsInput
ethanshar f909923
Support change reason in onChange callback
ethanshar e214f6a
Add api json to ChipsInput
ethanshar bc98731
Merge branch 'master' into feat/NewChipsInput
ethanshar 9429fd9
Remove unused styles
ethanshar cb9d460
Add missing prop descriptions
ethanshar 39b875a
Add TODOs
ethanshar fe8b59b
Cleanup comments
ethanshar ddd8a9d
Remove redundant imports
ethanshar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
demo/src/screens/incubatorScreens/IncubatorChipsInputScreen.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import _ from 'lodash'; | ||
import React, {Component} from 'react'; | ||
import {StyleSheet} from 'react-native'; | ||
import {View, Text, Card, TextField, Button, Colors, Incubator} from 'react-native-ui-lib'; //eslint-disable-line | ||
|
||
export default class ChipsInputScreen extends Component { | ||
state = { | ||
chips: [{label: 'one'}, {label: 'two'}], | ||
chips2: [] | ||
}; | ||
|
||
render() { | ||
return ( | ||
<View flex padding-20> | ||
<Text h1 marginB-s4> | ||
ChipsInput | ||
</Text> | ||
<Incubator.ChipsInput | ||
placeholder="Enter chips" | ||
defaultChipProps={{ | ||
backgroundColor: Colors.primary, | ||
labelStyle: {color: Colors.white}, | ||
containerStyle: {borderWidth: 0}, | ||
dismissColor: Colors.white | ||
}} | ||
chips={this.state.chips} | ||
leadingAccessory={<Text>TO: </Text>} | ||
onChange={newChips => { | ||
this.setState({chips: newChips}); | ||
}} | ||
/> | ||
|
||
<Incubator.ChipsInput | ||
label="Max 3 chips" | ||
placeholder="Enter chips..." | ||
chips={this.state.chips2} | ||
onChange={newChips => this.setState({chips2: newChips})} | ||
maxChips={3} | ||
/> | ||
</View> | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from 'react'; | ||
import { TextFieldProps } from '../TextField'; | ||
import { ChipProps } from '../../components/chip'; | ||
export declare enum ChipsInputChangeReason { | ||
Added = "added", | ||
Removed = "removed" | ||
} | ||
export declare type ChipsInputProps = Omit<TextFieldProps, 'ref'> & { | ||
/** | ||
* Chip items to render in the input | ||
*/ | ||
chips?: ChipProps[]; | ||
/** | ||
* A default set of chip props to pass to all chips | ||
*/ | ||
defaultChipProps?: ChipProps; | ||
/** | ||
* Change callback for when chips changed (either added or removed) | ||
*/ | ||
onChange?: (chips: ChipProps[], changeReason: ChipsInputChangeReason, updatedChip: ChipProps) => void; | ||
/** | ||
* Maximum chips | ||
*/ | ||
maxChips?: number; | ||
}; | ||
declare const _default: React.ForwardRefExoticComponent<Omit<TextFieldProps, "ref"> & { | ||
/** | ||
* Chip items to render in the input | ||
*/ | ||
chips?: ChipProps[] | undefined; | ||
/** | ||
* A default set of chip props to pass to all chips | ||
*/ | ||
defaultChipProps?: ChipProps | undefined; | ||
/** | ||
* Change callback for when chips changed (either added or removed) | ||
*/ | ||
onChange?: ((chips: ChipProps[], changeReason: ChipsInputChangeReason, updatedChip: ChipProps) => void) | undefined; | ||
/** | ||
* Maximum chips | ||
*/ | ||
maxChips?: number | undefined; | ||
} & React.RefAttributes<any>>; | ||
export default _default; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "ChipsInput", | ||
"category": "incubator", | ||
"description": "A chips input", | ||
"extends": ["TextField"], | ||
"modifiers": ["margin", "color", "typography"], | ||
"example": "https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/incubatorScreens/IncubatorChipsInputScreen.tsx", | ||
"images": [], | ||
"props": [ | ||
{"name": "chips", "type": "ChipProps[]", "description": "List of chips to render"}, | ||
{ | ||
"name": "defaultChipProps", | ||
"type": "ChipProps", | ||
"description": "Default set of props to pass by default to all chips" | ||
}, | ||
{ | ||
"name": "onChange", | ||
"type": "(newChips, changeReason, updatedChip) => void", | ||
"description": "Callback for chips change (adding or removing chip)" | ||
}, | ||
{"name": "maxChips", "type": "number", "description": "The maximum chips to allow adding"} | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import React, {useCallback, useMemo, useRef, useState, forwardRef} from 'react'; | ||
import {StyleSheet, NativeSyntheticEvent, TextInputKeyPressEventData} from 'react-native'; | ||
import {isUndefined, map} from 'lodash'; | ||
import {Constants} from '../../helpers'; | ||
import {useCombinedRefs} from '../../hooks'; | ||
import TextField, {TextFieldProps} from '../TextField'; | ||
import Chip, {ChipProps} from '../../components/chip'; | ||
|
||
const removeIcon = require('./assets/xSmall.png'); | ||
|
||
export enum ChipsInputChangeReason { | ||
Added = 'added', | ||
Removed = 'removed' | ||
} | ||
|
||
export type ChipsInputProps = Omit<TextFieldProps, 'ref'> & { | ||
/** | ||
* Chip items to render in the input | ||
*/ | ||
chips?: ChipProps[]; | ||
/** | ||
* A default set of chip props to pass to all chips | ||
*/ | ||
defaultChipProps?: ChipProps; | ||
/** | ||
* Change callback for when chips changed (either added or removed) | ||
*/ | ||
onChange?: (chips: ChipProps[], changeReason: ChipsInputChangeReason, updatedChip: ChipProps) => void; | ||
/** | ||
* Maximum chips | ||
*/ | ||
maxChips?: number; | ||
}; | ||
|
||
const ChipsInput = (props: ChipsInputProps, refToForward: React.Ref<any>) => { | ||
const fieldRef = useCombinedRefs(refToForward); | ||
const {chips = [], defaultChipProps, leadingAccessory, onChange, fieldStyle, maxChips, ...others} = props; | ||
const [markedForRemoval, setMarkedForRemoval] = useState<number | undefined>(undefined); | ||
const fieldValue = useRef(others.value); | ||
|
||
const addChip = useCallback(() => { | ||
const reachedMaximum = maxChips && chips?.length >= maxChips; | ||
if (fieldValue.current && !reachedMaximum) { | ||
const newChip = {label: fieldValue.current}; | ||
onChange?.([...chips, newChip], ChipsInputChangeReason.Added, newChip); | ||
setMarkedForRemoval(undefined); | ||
// @ts-expect-error | ||
fieldRef.current.clear(); | ||
fieldValue.current = ''; | ||
} | ||
}, [onChange, chips, maxChips]); | ||
|
||
const removeMarkedChip = useCallback(() => { | ||
if (!isUndefined(markedForRemoval)) { | ||
const removedChip = chips?.splice(markedForRemoval, 1); | ||
onChange?.([...chips], ChipsInputChangeReason.Removed, removedChip?.[0]); | ||
setMarkedForRemoval(undefined); | ||
} | ||
}, [chips, markedForRemoval, onChange]); | ||
|
||
const onChipPress = useCallback(({customValue: index}) => { | ||
const selectedChip = chips[index]; | ||
selectedChip?.onPress?.(); | ||
|
||
setMarkedForRemoval(index); | ||
}, | ||
[chips]); | ||
|
||
const onChangeText = useCallback(value => { | ||
fieldValue.current = value; | ||
props.onChangeText?.(value); | ||
|
||
if (!isUndefined(markedForRemoval)) { | ||
setMarkedForRemoval(undefined); | ||
} | ||
}, | ||
[props.onChangeText, markedForRemoval]); | ||
|
||
const onKeyPress = useCallback((event: NativeSyntheticEvent<TextInputKeyPressEventData>) => { | ||
props.onKeyPress?.(event); | ||
const keyCode = event?.nativeEvent?.key; | ||
const pressedBackspace = keyCode === Constants.backspaceKey; | ||
|
||
if (pressedBackspace && !fieldValue.current) { | ||
if (isUndefined(markedForRemoval) || markedForRemoval !== chips.length - 1) { | ||
setMarkedForRemoval(chips.length - 1); | ||
} else { | ||
removeMarkedChip(); | ||
} | ||
} | ||
}, | ||
[chips, props.onKeyPress, markedForRemoval, removeMarkedChip]); | ||
|
||
const chipList = useMemo(() => { | ||
return ( | ||
<> | ||
{leadingAccessory} | ||
{map(chips, (chip, index) => { | ||
const isMarkedForRemoval = index === markedForRemoval; | ||
return ( | ||
<Chip | ||
key={index} | ||
customValue={index} | ||
// resetSpacings | ||
// paddingH-s2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be removed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kept it for now so I'll have a quick reference in case there is something missing while we're migrating. |
||
marginR-s2 | ||
marginB-s2 | ||
dismissIcon={removeIcon} | ||
{...defaultChipProps} | ||
{...chip} | ||
onPress={onChipPress} | ||
onDismiss={isMarkedForRemoval ? removeMarkedChip : undefined} | ||
/> | ||
); | ||
})} | ||
</> | ||
); | ||
}, [chips, leadingAccessory, defaultChipProps, removeMarkedChip, markedForRemoval]); | ||
|
||
return ( | ||
<TextField | ||
// @ts-expect-error | ||
ref={fieldRef} | ||
leadingAccessory={chipList} | ||
blurOnSubmit={false} | ||
{...others} | ||
onChangeText={onChangeText} | ||
onSubmitEditing={addChip} | ||
fieldStyle={[fieldStyle, styles.fieldStyle]} | ||
onKeyPress={onKeyPress} | ||
accessibilityHint={props.editable ? 'press keyboard delete button to remove last tag' : undefined} | ||
/> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
fieldStyle: { | ||
flexWrap: 'wrap' | ||
} | ||
}); | ||
|
||
ChipsInput.changeReasons = { | ||
ADDED: 'added', | ||
REMOVED: 'removed' | ||
}; | ||
lidord-wix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export default forwardRef(ChipsInput); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There're few redundant imports here..