Skip to content

Commit 6ed014a

Browse files
authored
Checkbox - add validation state invoked by validate() (#2672)
* Checkbox - add validation state invoked by validate() * fix error colors * change conditions * Add comment * Adding 'isValid' method * add 'required' and 'onChangeValidity' props * fix isValid() * tests * isValid initial value and adding tests * eslint ignore unused driver * Move 'validationState' out of state. isValid - clearer condition * Revert "Move 'validationState' out of state. isValid - clearer condition" This reverts commit 27cecd2. * format tests file * removing driver unused const * moving validationState out of state * isValid - clear condition. onValidityChange - invoke if required, but only if validate invoked * fix pr comments * set validation on value prop change
1 parent f0662b0 commit 6ed014a

File tree

5 files changed

+371
-59
lines changed

5 files changed

+371
-59
lines changed

demo/src/screens/componentScreens/CheckboxScreen.tsx

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, {Component} from 'react';
22
import {StyleSheet} from 'react-native';
3-
import {Assets, Colors, View, Text, Checkbox} from 'react-native-ui-lib'; //eslint-disable-line
3+
import {Assets, Colors, View, Text, Button, Checkbox, CheckboxRef} from 'react-native-ui-lib'; //eslint-disable-line
44

55
export default class CheckboxScreen extends Component {
66
state = {
@@ -10,7 +10,28 @@ export default class CheckboxScreen extends Component {
1010
value4: true,
1111
value5: false,
1212
value6: false,
13-
value7: true
13+
value7: false,
14+
validationText: '',
15+
validationColor: Colors.$textDefault
16+
};
17+
18+
checkbox = React.createRef<CheckboxRef>();
19+
20+
onPress = () => {
21+
this.checkbox.current?.validate();
22+
};
23+
24+
onValueChange = (value: boolean) => {
25+
this.setState({value7: value}, () => {
26+
console.log('onValueChange: ', value);
27+
});
28+
};
29+
30+
onChangeValidity = (value?: boolean) => {
31+
this.setState({
32+
validationText: String(value),
33+
validationColor: value === true ? Colors.$textSuccess : Colors.$textDangerLight
34+
});
1435
};
1536

1637
render() {
@@ -77,6 +98,24 @@ export default class CheckboxScreen extends Component {
7798
iconColor={Colors.green10}
7899
/>
79100
</View>
101+
102+
<View marginT-20>
103+
<Text text60 $textDefault marginB-10>
104+
Validation
105+
</Text>
106+
<Text marginB-4 color={this.state.validationColor}>{this.state.validationText}</Text>
107+
<View row centerV spread marginB-10>
108+
<Checkbox
109+
required
110+
onChangeValidity={this.onChangeValidity}
111+
ref={this.checkbox}
112+
value={this.state.value7}
113+
onValueChange={this.onValueChange}
114+
label={'This is a checkbox'}
115+
/>
116+
<Button size={'small'} label={'Validate'} onPress={this.onPress}/>
117+
</View>
118+
</View>
80119
</View>
81120
);
82121
}

0 commit comments

Comments
 (0)