-
Notifications
You must be signed in to change notification settings - Fork 734
Added validation error feature to TagsInput #749
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
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -30,9 +30,9 @@ export default class TagsInput extends BaseComponent { | |
|
||
static propTypes = { | ||
/** | ||
* list of tags. can be string or custom object when implementing getLabel | ||
* list of tags. can be string boolean or custom object when implementing getLabel | ||
*/ | ||
tags: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.object, PropTypes.string])), | ||
tags: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.object, PropTypes.string, PropTypes.bool])), | ||
/** | ||
* callback for extracting the label out of the tag item | ||
*/ | ||
|
@@ -53,6 +53,10 @@ export default class TagsInput extends BaseComponent { | |
* callback for when pressing a tag in the following format (tagIndex, markedTagIndex) => {...} | ||
*/ | ||
onTagPress: PropTypes.func, | ||
/** | ||
* validation message error appears when tag isn't validate | ||
*/ | ||
onTagNotValidate: PropTypes.string, | ||
ethanshar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* if true, tags *removal* Ux won't be available | ||
*/ | ||
|
@@ -98,11 +102,14 @@ export default class TagsInput extends BaseComponent { | |
this.state = { | ||
value: props.value, | ||
tags: _.cloneDeep(props.tags) || [], | ||
tagIndexToRemove: undefined | ||
tagIndexToRemove: undefined, | ||
isTagNotValidate: false | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
const {tags} = this.state; | ||
this.updateInvalidTags(tags); | ||
if (Constants.isAndroid) { | ||
const textInputHandle = ReactNative.findNodeHandle(this.input); | ||
if (textInputHandle && NativeModules.TextInputDelKeyHandler) { | ||
|
@@ -140,6 +147,7 @@ export default class TagsInput extends BaseComponent { | |
const newTag = _.isFunction(onCreateTag) ? onCreateTag(value) : value; | ||
const newTags = [...tags, newTag]; | ||
|
||
this.updateInvalidTags(newTags); | ||
this.setState({ | ||
value: '', | ||
tags: newTags | ||
|
@@ -155,6 +163,7 @@ export default class TagsInput extends BaseComponent { | |
const removedTag = tags[tagIndexToRemove]; | ||
|
||
tags.splice(tagIndexToRemove, 1); | ||
this.updateInvalidTags(tags); | ||
this.setState({ | ||
tags, | ||
tagIndexToRemove: undefined | ||
|
@@ -198,6 +207,14 @@ export default class TagsInput extends BaseComponent { | |
return isLastTagMarked; | ||
} | ||
|
||
updateInvalidTags(tags) { | ||
ethanshar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return tags.filter((tag) => { | ||
return tag.invalid; | ||
}).length > 0 ? | ||
this.setState({isTagNotValidate: true}) : | ||
this.setState({isTagNotValidate: false}); | ||
} | ||
|
||
onKeyPress(event) { | ||
_.invoke(this.props, 'onKeyPress', event); | ||
|
||
|
@@ -236,24 +253,48 @@ export default class TagsInput extends BaseComponent { | |
return _.get(item, 'label'); | ||
} | ||
|
||
renderLabel(tag, shouldMarkTag) { | ||
onTagNotValidate(tag, onTagNotValidate) { | ||
return onTagNotValidate ? tag.invalid : false; | ||
} | ||
|
||
renderLabel(tag, shouldMarkTag, isTagNotValidate) { | ||
ethanshar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const typography = this.extractTypographyValue(); | ||
const label = this.getLabel(tag); | ||
|
||
return ( | ||
<View row centerV> | ||
{shouldMarkTag && <Image style={styles.removeIcon} source={Assets.icons.x}/>} | ||
<Text style={[styles.tagLabel, typography]} accessibilityLabel={`${label} tag`}> | ||
{shouldMarkTag ? 'Remove' : label} | ||
{shouldMarkTag && ( | ||
<Image | ||
style={[isTagNotValidate ? styles.inValidTagRemoveIcon : styles.removeIcon]} | ||
source={Assets.icons.x} | ||
/>) | ||
} | ||
<Text | ||
style={[isTagNotValidate ? (shouldMarkTag ? styles.inValidMarkedTagLabel : styles.inValidTagLabel) | ||
: styles.tagLabel, typography]} accessibilityLabel={`${label} tag`} | ||
> | ||
{isTagNotValidate ? label : (shouldMarkTag ? 'Remove' : label)} | ||
ethanshar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</Text> | ||
</View> | ||
); | ||
} | ||
|
||
renderTag(tag, index) { | ||
const {tagStyle, renderTag} = this.getThemeProps(); | ||
const {tagStyle, renderTag, onTagNotValidate} = this.getThemeProps(); | ||
const {tagIndexToRemove} = this.state; | ||
const shouldMarkTag = tagIndexToRemove === index; | ||
const isTagNotValidate = this.onTagNotValidate(tag, onTagNotValidate); | ||
|
||
if (isTagNotValidate) { | ||
return ( | ||
<View | ||
key={index} | ||
style={[styles.inValidTag, tagStyle, shouldMarkTag && styles.inValidMarkedTag]} | ||
> | ||
{this.renderLabel(tag, shouldMarkTag, isTagNotValidate)} | ||
</View> | ||
); | ||
} | ||
|
||
if (_.isFunction(renderTag)) { | ||
return renderTag(tag, index, shouldMarkTag, this.getLabel(tag)); | ||
|
@@ -310,16 +351,21 @@ export default class TagsInput extends BaseComponent { | |
} | ||
|
||
render() { | ||
const {disableTagRemoval, containerStyle, hideUnderline} = this.getThemeProps(); | ||
const {disableTagRemoval, containerStyle, hideUnderline, onTagNotValidate} = this.getThemeProps(); | ||
const tagRenderFn = disableTagRemoval ? this.renderTag : this.renderTagWrapper; | ||
const {tags} = this.state; | ||
const {tags, isTagNotValidate, tagIndexToRemove} = this.state; | ||
|
||
return ( | ||
<View style={[!hideUnderline && styles.withUnderline, containerStyle]}> | ||
<View style={styles.tagsList}> | ||
{_.map(tags, tagRenderFn)} | ||
{this.renderTextInput()} | ||
</View> | ||
<View> | ||
ethanshar marked this conversation as resolved.
Show resolved
Hide resolved
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. add condition to the error message so it will render it only if the validationErrorMessage is not undefined 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. done, not sure if that's what you meant |
||
<Text style={[isTagNotValidate && tagIndexToRemove ? styles.inValidMarkedTagLabel : styles.inValidTagLabel]}> | ||
{isTagNotValidate ? onTagNotValidate : null} | ||
</Text> | ||
</View> | ||
</View> | ||
); | ||
} | ||
|
@@ -336,6 +382,19 @@ export default class TagsInput extends BaseComponent { | |
this.input.clear(); | ||
} | ||
} | ||
const basicTagStyle = { | ||
borderRadius: BorderRadiuses.br100, | ||
paddingVertical: 4.5, | ||
paddingHorizontal: 12, | ||
marginRight: GUTTER_SPACING, | ||
marginVertical: GUTTER_SPACING / 2 | ||
}; | ||
|
||
const basicIconStyle = { | ||
width: 10, | ||
height: 10, | ||
marginRight: 6 | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
withUnderline: { | ||
|
@@ -353,23 +412,39 @@ const styles = StyleSheet.create({ | |
}, | ||
tag: { | ||
backgroundColor: Colors.blue30, | ||
borderRadius: BorderRadiuses.br100, | ||
paddingVertical: 4.5, | ||
paddingHorizontal: 12, | ||
marginRight: GUTTER_SPACING, | ||
marginVertical: GUTTER_SPACING / 2 | ||
...basicTagStyle | ||
}, | ||
inValidTag: { | ||
borderWidth: 1, | ||
borderColor: Colors.red30, | ||
...basicTagStyle | ||
}, | ||
inValidMarkedTag: { | ||
borderWidth: 1, | ||
borderColor: Colors.red10, | ||
...basicTagStyle | ||
}, | ||
tagMarked: { | ||
backgroundColor: Colors.dark10 | ||
}, | ||
removeIcon: { | ||
tintColor: Colors.white, | ||
width: 10, | ||
height: 10, | ||
marginRight: 6 | ||
...basicIconStyle | ||
}, | ||
inValidTagRemoveIcon: { | ||
tintColor: Colors.red10, | ||
...basicIconStyle | ||
}, | ||
tagLabel: { | ||
...Typography.text80, | ||
color: Colors.white | ||
}, | ||
inValidTagLabel: { | ||
...Typography.text80, | ||
color: Colors.red30 | ||
}, | ||
inValidMarkedTagLabel: { | ||
...Typography.text80, | ||
color: Colors.red10 | ||
} | ||
}); |
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.
Uh oh!
There was an error while loading. Please reload this page.