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 5 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
33 changes: 31 additions & 2 deletions demo/src/screens/componentScreens/CheckboxScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {Component} from 'react';
import {StyleSheet} from 'react-native';
import {Checkbox, Assets, Text, View, Colors} from 'react-native-ui-lib'; //eslint-disable-line
import {Assets, Colors, View, Text, Button, Checkbox, CheckboxRef} from 'react-native-ui-lib'; //eslint-disable-line

export default class CheckboxScreen extends Component {
state = {
Expand All @@ -9,8 +9,11 @@ export default class CheckboxScreen extends Component {
value3: true,
value4: true,
value5: false,
value6: false
value6: false,
value7: false
};
checkbox = React.createRef<CheckboxRef>();
checkbox1 = React.createRef<CheckboxRef>();

render() {
return (
Expand Down Expand Up @@ -67,6 +70,32 @@ export default class CheckboxScreen extends Component {
iconColor={Colors.green10}
/>
</View>

<View marginT-20>
<Text text60 $textDefault marginB-10>
Validation
</Text>
<View row spread marginB-10>
<Checkbox
ref={this.checkbox}
value={this.state.value7}
onValueChange={value7 => this.setState({value7})}
label={'This is a checkbox'}
/>
<Button size={'small'} label={'Validate'} onPress={() => this.checkbox.current?.validate()}/>
</View>
<View row spread>
<Checkbox
disabled
ref={this.checkbox1}
value={this.state.value7}
onValueChange={value7 => this.setState({value7})}
iconColor={Colors.green10}
label={'This is disabled checkbox'}
/>
<Button disabled size={'small'} label={'Validate'} onPress={() => this.checkbox1.current?.validate()}/>
</View>
</View>
</View>
);
}
Expand Down
41 changes: 36 additions & 5 deletions src/components/checkbox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,16 @@ export interface CheckboxProps extends TouchableOpacityProps {
containerStyle?: StyleProp<ViewStyle>;
}

interface CheckboxMethods {
validate: () => void;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see in TextField there's a onValidationFailed method that is called when the validation fails, WDYT about adding it? Unlike onChangeValidity it is called if validate fails.
It can be another PR...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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;
}

/**
Expand Down Expand Up @@ -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);
Expand All @@ -139,6 +148,7 @@ class Checkbox extends Component<CheckboxProps, CheckboxState> {
]
};
}
validationState = false;

componentDidUpdate(prevProps: CheckboxProps) {
const {value} = this.props;
Expand Down Expand Up @@ -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);
}
};
Expand All @@ -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 (
Expand Down Expand Up @@ -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}>
{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;
}
}

function createStyles(props: CheckboxProps) {
Expand All @@ -275,8 +307,7 @@ function createStyles(props: CheckboxProps) {
},
checkboxLabel: {
marginLeft: Spacings.s3,
alignSelf: 'center',
color: Colors.$textDefault
alignSelf: 'center'
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export {default as BaseInput} from './components/baseInput';
export {default as Button, ButtonProps, ButtonSize, ButtonAnimationDirection} from './components/button';
export {default as Card, CardProps, CardSectionProps, CardSelectionOptions} from './components/card';
export {default as Carousel, CarouselProps, PageControlPosition} from './components/carousel';
export {default as Checkbox, CheckboxProps} from './components/checkbox';
export {default as Checkbox, CheckboxProps, CheckboxRef} from './components/checkbox';
export {default as ChipsInput, ChipsInputProps, ChipsInputChipProps} from './components/chipsInput';
export {default as Chip, ChipProps} from './components/chip';
export {default as ColorPicker, ColorPickerProps} from './components/colorPicker';
Expand Down