Skip to content

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

Merged
merged 21 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion demo/src/screens/componentScreens/CheckboxScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class CheckboxScreen extends Component {

onChangeValidity = (value?: boolean) => {
this.setState({
validationText: value === true ? 'Valid' : 'Not valid',
validationText: String(value),
validationColor: value === true ? Colors.$textSuccess : Colors.$textDangerLight
});
};
Expand Down
3 changes: 2 additions & 1 deletion src/components/checkbox/__tests__/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ describe('Checkbox renderer test', () => {
//eslint-disable-next-line
new CheckboxDriver({component, testID});

checkboxRef.current?.validate();
//@ts-ignore
checkboxRef.current.validate();

expect(onChangeValidity).not.toHaveBeenCalled();
});
Expand Down
24 changes: 15 additions & 9 deletions src/components/checkbox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ class Checkbox extends Component<CheckboxProps, CheckboxState> {

if (prevProps.value !== value) {
this.animateCheckbox(value);
if (value !== undefined) {
this.setValidation(value);
}
}
}

Expand All @@ -195,20 +198,23 @@ class Checkbox extends Component<CheckboxProps, CheckboxState> {
}).start();
}

setValidation(newValue: boolean) {
const {required, onChangeValidity} = this.props;
if (required) {
const error = required && !newValue;
this.setState({showError: this.validationState ? error : false, isValid: !error}, () => {
onChangeValidity?.(this.state.isValid);
});
}
}

onPress = () => {
const {disabled, value, required, onValueChange, onChangeValidity} = this.props;
const {disabled, value, onValueChange} = this.props;

if (!disabled) {
const newValue = !value;

onValueChange?.(newValue);

if (required) {
const error = required && !newValue;
this.setState({showError: this.validationState ? error : false, isValid: !error}, () => {
onChangeValidity?.(this.state.isValid);
});
}
this.setValidation(newValue);
}
};

Expand Down