-
Notifications
You must be signed in to change notification settings - Fork 734
Checkbox - add validation state invoked by validate() #2672
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
Changes from 5 commits
9bc8b6e
1156d4e
17dac51
ebea130
3bb190e
9aa54fb
a7a3804
b88aa31
3911144
476c264
5a87002
27cecd2
d4a2a6b
4122b44
68fec85
59abb9d
a7fc0cc
70cb3fe
91bcf05
87ad122
6bb9278
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,8 +85,16 @@ export interface CheckboxProps extends TouchableOpacityProps { | |
containerStyle?: StyleProp<ViewStyle>; | ||
} | ||
|
||
interface CheckboxMethods { | ||
validate: () => void; | ||
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 see in 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'll keep it simple until we'll get such a request |
||
isValid: () => boolean; | ||
} | ||
|
||
export type CheckboxRef = Checkbox & CheckboxMethods; | ||
|
||
interface CheckboxState { | ||
isChecked: Animated.Value; | ||
valid?: boolean; | ||
} | ||
|
||
/** | ||
|
@@ -122,7 +130,8 @@ class Checkbox extends Component<CheckboxProps, CheckboxState> { | |
super(props); | ||
|
||
this.state = { | ||
isChecked: new Animated.Value(this.props.value ? 1 : 0) | ||
isChecked: new Animated.Value(this.props.value ? 1 : 0), | ||
valid: true | ||
}; | ||
|
||
this.styles = createStyles(props); | ||
|
@@ -139,6 +148,7 @@ class Checkbox extends Component<CheckboxProps, CheckboxState> { | |
] | ||
}; | ||
} | ||
validationState = false; | ||
M-i-k-e-l marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
componentDidUpdate(prevProps: CheckboxProps) { | ||
const {value} = this.props; | ||
|
@@ -174,6 +184,9 @@ class Checkbox extends Component<CheckboxProps, CheckboxState> { | |
const {disabled} = this.props; | ||
|
||
if (!disabled) { | ||
if (this.validationState) { | ||
this.setState({valid: !this.props.value}); | ||
} | ||
this.props.onValueChange?.(!this.props.value); | ||
} | ||
}; | ||
|
@@ -198,12 +211,19 @@ class Checkbox extends Component<CheckboxProps, CheckboxState> { | |
}; | ||
|
||
getBorderStyle() { | ||
const borderColor = {borderColor: this.getColor()}; | ||
const borderColor = {borderColor: this.state.valid ? this.getColor() : Colors.$outlineDanger}; | ||
const borderStyle = [this.styles.container, {borderWidth: DEFAULT_BORDER_WIDTH}, borderColor]; | ||
|
||
return borderStyle; | ||
} | ||
|
||
getLabelStyle = () => { | ||
return { | ||
color: | ||
this.props.disabled ? Colors.$textDisabled : this.state.valid ? Colors.$textDefault : Colors.$textDangerLight | ||
}; | ||
}; | ||
|
||
renderCheckbox() { | ||
const {selectedIcon, label, testID, style, containerStyle, ...others} = this.props; | ||
return ( | ||
|
@@ -241,14 +261,26 @@ class Checkbox extends Component<CheckboxProps, CheckboxState> { | |
return label ? ( | ||
<View row centerV style={containerStyle}> | ||
{this.renderCheckbox()} | ||
<Text flexS style={[this.styles.checkboxLabel, labelStyle]} recorderTag={'unmask'} {...labelProps} onPress={this.onPress}> | ||
<Text flexS style={[this.styles.checkboxLabel, this.getLabelStyle(), labelStyle]} recorderTag={'unmask'} {...labelProps} onPress={this.onPress}> | ||
M-i-k-e-l marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{label} | ||
</Text> | ||
</View> | ||
) : ( | ||
this.renderCheckbox() | ||
); | ||
} | ||
|
||
validate() { | ||
this.validationState = true; | ||
// Validation: value must be true (checked) | ||
if (!this.props.disabled && !this.props.value) { | ||
this.setState({valid: false}); | ||
} | ||
} | ||
|
||
isValid() { | ||
return this.state.valid; | ||
M-i-k-e-l marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
function createStyles(props: CheckboxProps) { | ||
|
@@ -275,8 +307,7 @@ function createStyles(props: CheckboxProps) { | |
}, | ||
checkboxLabel: { | ||
marginLeft: Spacings.s3, | ||
alignSelf: 'center', | ||
color: Colors.$textDefault | ||
alignSelf: 'center' | ||
} | ||
}); | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.