Skip to content
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

Custom validatable icon rendering #135

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
44 changes: 41 additions & 3 deletions mixins/WidgetMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = {
formStyles: PropTypes.object,
validationImage: PropTypes.bool,
openModal: PropTypes.func,
customValidationView: PropTypes.func,
// navigator: ,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
Expand All @@ -43,6 +44,7 @@ module.exports = {
navigator: null,
onFocus: () => {},
onBlur: () => {},
customValidationView: () => {},
validateOnEmpty: false,
};
},
Expand Down Expand Up @@ -215,11 +217,10 @@ module.exports = {

if (hasValue && hasImageProp && !isOptionWidget && shouldShowValidationImage && toValidate) {
const imageSrc = hasValidationErrors ? require('../icons/delete_sign.png'):require('../icons/checkmark.png');

return (
<Image
style={this.getStyle('rowImage')}
resizeMode={Image.resizeMode.contain}
resizeMode="contain"
source={imageSrc}
/>
);
Expand All @@ -231,12 +232,49 @@ module.exports = {
return (
<Image
style={this.getStyle('rowImage')}
resizeMode={Image.resizeMode.contain}
resizeMode="contain"
source={this.props.image}
/>
);
}

return null;
},

_renderCustomValidationView() {
var validators = null;
if (this.props.displayValue) {
// in case of modal widget
validators = GiftedFormManager.getValidators(this.props.formName, this.props.displayValue);
} else {
validators = GiftedFormManager.getValidators(this.props.formName, this.props.name);
}

let toValidate = false;
if (Array.isArray(validators.validate)) {
if (validators.validate.length > 0) {
toValidate = true;
}
}

// @todo image delete_sign / checkmark should be editable via option
// @todo options enable live validation

let hasValue = typeof this.state.value !== 'undefined' && this.state.value !== '';

if (this.props.validateOnEmpty) {
hasValue = true;
}

const hasValidationErrors = this.state.validationErrorMessage !== null;
const isOptionWidget = this.props.type === 'OptionWidget';
const shouldShowValidationImage = this.props.validationImage === true;

if (this.props.customValidationView) {
const showValidation = hasValue && !isOptionWidget && shouldShowValidationImage && toValidate;
const isValid = !hasValidationErrors;
return this.props.customValidationView(showValidation, isValid)
}
return null;
},
};
8 changes: 5 additions & 3 deletions widgets/ModalWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports = createReactClass({
return (
<Image
style={this.getStyle('disclosure')}
resizeMode={Image.resizeMode.contain}
resizeMode="contain"
source={require('../icons/disclosure.png')}
/>
);
Expand Down Expand Up @@ -114,7 +114,7 @@ module.exports = createReactClass({
marginLeft: 10,
tintColor: '#097881',
}}
resizeMode={Image.resizeMode.contain}
resizeMode="contain"
source={require('../icons/close.png')}
/>
</TouchableOpacity>
Expand All @@ -139,7 +139,7 @@ module.exports = createReactClass({
marginRight: 10,
tintColor: '#097881',
}}
resizeMode={Image.resizeMode.contain}
resizeMode="contain"
source={require('../icons/check.png')}
/>
</TouchableOpacity>
Expand Down Expand Up @@ -276,6 +276,8 @@ module.exports = createReactClass({
>
<View style={this.getStyle('row')}>
{this._renderImage()}
{this._renderCustomValidationView()}

<Text numberOfLines={1} style={this.getStyle('modalTitle')}>{this.props.title}</Text>
<View style={this.getStyle('alignRight')}>
<Text numberOfLines={1} style={this.getStyle('modalValue')}>{this.state.value}</Text>
Expand Down
3 changes: 2 additions & 1 deletion widgets/OptionWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = createReactClass({
return (
<Image
style={this.getStyle('checkmark')}
resizeMode={Image.resizeMode.contain}
resizeMode="contain"
source={require('../icons/check.png')}
/>
);
Expand Down Expand Up @@ -63,6 +63,7 @@ module.exports = createReactClass({
>
<View style={this.getStyle('row')}>
{this._renderImage()}
{this._renderCustomValidationView()}
<Text numberOfLines={1} style={this.getStyle('switchTitle')}>
{this.props.title}
</Text>
Expand Down
2 changes: 1 addition & 1 deletion widgets/RowWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = createReactClass({
return (
<Image
style={this.getStyle('disclosure')}
resizeMode={Image.resizeMode.contain}
resizeMode="contain"
source={require('../icons/disclosure.png')}
/>
);
Expand Down
2 changes: 1 addition & 1 deletion widgets/SelectCountryWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -1857,7 +1857,7 @@ module.exports = createReactClass({
}}>
<Image
key={rowData.alpha2+'Image'}
resizeMode={Image.resizeMode.contain}
resizeMode="contain"
source={image}
style={{
height: 30,
Expand Down
1 change: 1 addition & 0 deletions widgets/SwitchWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module.exports = createReactClass({
<View style={this.getStyle('rowContainer')}>
<View style={this.getStyle('row')}>
{this._renderImage()}
{this._renderCustomValidationView()}

<Text numberOfLines={1} style={this.getStyle('switchTitle')}>{this.props.title}</Text>
<View style={this.getStyle('switchAlignRight')}>
Expand Down
1 change: 1 addition & 0 deletions widgets/TextAreaWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module.exports = createReactClass({
render() {
return (
<View style={this.getStyle('textAreaRow')}>
{this._renderCustomValidationView()}
<TextInput
style={this.getStyle('textArea')}
multiline={true}
Expand Down
2 changes: 2 additions & 0 deletions widgets/TextInputWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ module.exports = createReactClass({
<View style={this.getStyle(['rowContainer'])}>
<View style={this.getStyle(['titleContainer'])}>
{this._renderImage()}
{this._renderCustomValidationView()}
<Text numberOfLines={1} style={this.getStyle(['textInputTitle'])}>{this.props.title}</Text>
</View>

Expand All @@ -78,6 +79,7 @@ module.exports = createReactClass({
<View style={this.getStyle(['rowContainer'])}>
<View style={this.getStyle(['row'])}>
{this._renderImage()}
{this._renderCustomValidationView()}
{this._renderTitle()}
<TextInput
ref='input'
Expand Down