Skip to content

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 5 commits into from
May 4, 2020
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
5 changes: 4 additions & 1 deletion demo/src/screens/componentScreens/TagsInputScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class TagsInputScreen extends Component {
super(props);

this.state = {
tags: [{label: 'Amit'}, {label: 'Ethan'}],
tags: [{label: 'Amit'}, {label: 'Ethan', invalid: true}],
tags2: ['Tags', 'Input'],
tags3: ['Non', 'Removable', 'Tags']
};
Expand Down Expand Up @@ -36,6 +36,9 @@ export default class TagsInputScreen extends Component {

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

<TagsInput containerStyle={{marginBottom: 25}} placeholder="Enter Tags" tags={this.state.tags}
validationErrorMessage="error validation message" />

<TagsInput
containerStyle={{marginBottom: 25}}
placeholder="Editing disabled"
Expand Down
85 changes: 70 additions & 15 deletions src/components/tagsInput/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ 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])),
/**
Expand All @@ -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
*/
validationErrorMessage: PropTypes.string,
/**
* if true, tags *removal* Ux won't be available
*/
Expand Down Expand Up @@ -139,7 +143,6 @@ export default class TagsInput extends BaseComponent {

const newTag = _.isFunction(onCreateTag) ? onCreateTag(value) : value;
const newTags = [...tags, newTag];

this.setState({
value: '',
tags: newTags
Expand Down Expand Up @@ -242,9 +245,17 @@ export default class TagsInput extends BaseComponent {

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={[styles.removeIcon, tag.invalid && styles.inValidTagRemoveIcon]}
source={Assets.icons.x}
/>)
}
<Text
style={[tag.invalid ? (shouldMarkTag ? styles.errorMessageWhileMarked : styles.errorMessage)
: styles.tagLabel, typography]} accessibilityLabel={`${label} tag`}
>
{!tag.invalid && shouldMarkTag ? 'Remove' : label}
</Text>
</View>
);
Expand All @@ -255,6 +266,17 @@ export default class TagsInput extends BaseComponent {
const {tagIndexToRemove} = this.state;
const shouldMarkTag = tagIndexToRemove === index;

if (tag.invalid) {
return (
<View
key={index}
style={[styles.inValidTag, tagStyle, shouldMarkTag && styles.inValidMarkedTag]}
>
{this.renderLabel(tag, shouldMarkTag)}
</View>
);
}

if (_.isFunction(renderTag)) {
return renderTag(tag, index, shouldMarkTag, this.getLabel(tag));
}
Expand Down Expand Up @@ -310,16 +332,24 @@ export default class TagsInput extends BaseComponent {
}

render() {
const {disableTagRemoval, containerStyle, hideUnderline} = this.getThemeProps();
const {disableTagRemoval, containerStyle, hideUnderline, validationErrorMessage} = this.getThemeProps();
const tagRenderFn = disableTagRemoval ? this.renderTag : this.renderTagWrapper;
const {tags} = this.state;
const {tags, tagIndexToRemove} = this.state;

return (
<View style={[!hideUnderline && styles.withUnderline, containerStyle]}>
<View style={styles.tagsList}>
{_.map(tags, tagRenderFn)}
{this.renderTextInput()}
</View>
{validationErrorMessage ?
(
<View>
<Text style={[styles.errorMessage, tagIndexToRemove && styles.errorMessageWhileMarked]}>
{validationErrorMessage}
</Text>
</View>
) : null}
</View>
);
}
Expand All @@ -336,6 +366,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: {
Expand All @@ -353,23 +396,35 @@ 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: {
borderColor: Colors.red10
},
tagMarked: {
backgroundColor: Colors.dark10
},
removeIcon: {
tintColor: Colors.white,
width: 10,
height: 10,
marginRight: 6
...basicIconStyle
},
inValidTagRemoveIcon: {
tintColor: Colors.red10
},
tagLabel: {
...Typography.text80,
color: Colors.white
},
errorMessage: {
...Typography.text80,
color: Colors.red30
},
errorMessageWhileMarked: {
color: Colors.red10
}
});