Skip to content

Commit 3ef75e0

Browse files
author
Artur Yorsh
committed
BREAKING: refactor CheckBox to new api
1 parent 9a6415a commit 3ef75e0

File tree

3 files changed

+138
-195
lines changed

3 files changed

+138
-195
lines changed

src/components/ui/checkbox/checkbox.component.tsx

Lines changed: 47 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -12,72 +12,73 @@ import {
1212
Platform,
1313
StyleProp,
1414
StyleSheet,
15-
TextStyle,
16-
TouchableOpacity,
1715
TouchableOpacityProps,
1816
View,
1917
ViewStyle,
2018
} from 'react-native';
19+
import { Overwrite } from 'utility-types';
20+
import {
21+
FalsyText,
22+
RenderProp,
23+
TouchableWithoutFeedback,
24+
WebEventResponder,
25+
WebEventResponderCallbacks,
26+
WebEventResponderInstance,
27+
} from '../../devsupport';
2128
import {
2229
Interaction,
2330
styled,
2431
StyledComponentProps,
2532
StyleType,
26-
} from '@kitten/theme';
27-
import {
28-
Text,
29-
TextElement,
30-
} from '../text/text.component';
33+
} from '../../theme';
34+
import { TextProps } from '../text/text.component';
3135
import {
3236
CheckMark,
3337
CheckMarkProps,
34-
CheckMarkElement,
35-
} from '../support/components/checkmark.component';
38+
} from '../shared/checkmark.component';
3639
import {
3740
Minus,
38-
MinusElement,
3941
MinusProps,
40-
} from '../support/components/minus.component';
41-
import {
42-
isValidString,
43-
WebEventResponder,
44-
WebEventResponderCallbacks,
45-
WebEventResponderInstance,
46-
} from '../support/services';
42+
} from '../shared/minus.component';
4743

48-
export interface CheckBoxProps extends StyledComponentProps, TouchableOpacityProps {
49-
textStyle?: StyleProp<TextStyle>;
50-
text?: string;
44+
type CheckBoxStyledProps = Overwrite<StyledComponentProps, {
45+
appearance?: 'default' | string;
46+
}>;
47+
48+
export interface CheckBoxProps extends TouchableOpacityProps, CheckBoxStyledProps {
5149
checked?: boolean;
5250
indeterminate?: boolean;
53-
status?: string;
5451
onChange?: (checked: boolean, indeterminate: boolean) => void;
52+
text?: RenderProp<TextProps> | React.ReactText;
53+
status?: 'basic' | 'primary' | 'success' | 'info' | 'warning' | 'danger' | 'control' | string;
5554
}
5655

5756
export type CheckBoxElement = React.ReactElement<CheckBoxProps>;
5857

59-
type IconElement = CheckMarkElement | MinusElement;
60-
6158
/**
6259
* Styled `CheckBox` component.
6360
*
6461
* @extends React.Component
6562
*
66-
* @property {boolean} checked - Determines whether component is checked.`
63+
* @property {boolean} checked - Determines whether component is checked.
6764
* Default is `false`.
6865
*
69-
* @property {boolean} disabled - Determines whether component is disabled.
70-
* Default is `false.
66+
* @property {boolean} indeterminate - Determines whether checked status is indeterminate.
67+
* Will set indeterminate to false when the checked property is changed.
68+
* Default is `false`.
7169
*
7270
* @property {string} status - Determines the status of the component.
7371
* Can be `basic`, `primary`, `success`, `info`, `warning`, `danger` or `control`.
7472
* Default is `basic`.
7573
*
76-
* @property {string} text - Determines text of the component.
74+
* @property {string | (props: TextProps) => ReactElement} text - A string or a function component
75+
* to render near the checkbox.
76+
* If it is a function, it will be called with props provided by Eva.
77+
* Otherwise, renders a Text styled by Eva.
7778
*
78-
* @property {StyleProp<TextStyle>} textStyle - Customizes text style.
79-
*
80-
* @property {(checked: boolean) => void} onChange - Fires on checkbox value change.
79+
* @property {(checked: boolean, indeterminate: boolean) => void} onChange - Called on checkbox value change.
80+
* Called with `checked` and `indeterminate` values.
81+
* If `indeterminate` was provided in state, it should be changed to the value passed in this function.
8182
*
8283
* @property {TouchableOpacityProps} ...TouchableOpacityProps - Any props applied to TouchableOpacity component.
8384
*
@@ -139,7 +140,7 @@ class CheckBoxComponent extends React.Component<CheckBoxProps> implements WebEve
139140
}
140141
};
141142

142-
private getComponentStyle = (source: StyleType): StyleType => {
143+
private getComponentStyle = (source: StyleType) => {
143144
const {
144145
textMarginHorizontal,
145146
textFontFamily,
@@ -159,8 +160,6 @@ class CheckBoxComponent extends React.Component<CheckBoxProps> implements WebEve
159160
} = source;
160161

161162
return {
162-
container: {},
163-
highlightContainer: {},
164163
selectContainer: containerParameters,
165164
text: {
166165
marginHorizontal: textMarginHorizontal,
@@ -200,65 +199,42 @@ class CheckBoxComponent extends React.Component<CheckBoxProps> implements WebEve
200199
};
201200
};
202201

203-
private renderTextElement = (style: TextStyle): TextElement => {
204-
const { text, textStyle } = this.props;
205-
206-
return (
207-
<Text style={[style, styles.text, textStyle]}>{text}</Text>
208-
);
209-
};
210-
211-
private renderIconElement = (style: SvgProps): IconElement => {
202+
private renderIconElement = (style: SvgProps): React.ReactElement<SvgProps> => {
212203
const Icon: React.ComponentType<MinusProps | CheckMarkProps> = this.props.indeterminate ? Minus : CheckMark;
213204
return (
214205
<Icon {...style} />
215206
);
216207
};
217208

218-
private renderComponentChildren = (style: StyleType): React.ReactNodeArray => {
219-
const { text } = this.props;
220-
221-
return [
222-
this.renderIconElement(style.icon),
223-
isValidString(text) && this.renderTextElement(style.text),
224-
];
225-
};
226-
227209
public render(): React.ReactElement<TouchableOpacityProps> {
228-
const { eva, style, disabled, text, ...derivedProps } = this.props;
210+
const { eva, style, disabled, text, ...touchableProps } = this.props;
229211

230-
const {
231-
container,
232-
highlightContainer,
233-
highlight,
234-
selectContainer,
235-
...componentStyle
236-
} = this.getComponentStyle(eva.style);
212+
const evaStyle = this.getComponentStyle(eva.style);
237213

238-
const selectContainerStyle: StyleProp<ViewStyle> = [selectContainer, styles.selectContainer];
214+
const selectContainerStyle: StyleProp<ViewStyle> = [evaStyle.selectContainer, styles.selectContainer];
239215
const hitSlopInsets: Insets = this.createHitSlopInsets(selectContainerStyle);
240216

241-
const [iconElement, textElement] = this.renderComponentChildren(componentStyle);
242-
243217
return (
244-
<TouchableOpacity
245-
activeOpacity={1.0}
246-
{...derivedProps}
218+
<TouchableWithoutFeedback
219+
{...touchableProps}
247220
{...this.webEventResponder.eventHandlers}
248-
style={[container, styles.container, webStyles.container, style]}
221+
style={[styles.container, webStyles.container, style]}
249222
disabled={disabled}
250223
hitSlop={hitSlopInsets}
251224
onPress={this.onPress}
252225
onPressIn={this.onPressIn}
253226
onPressOut={this.onPressOut}>
254-
<View style={[highlightContainer, styles.highlightContainer]}>
255-
<View style={[highlight, styles.highlight]}/>
227+
<View style={styles.highlightContainer}>
228+
<View style={[evaStyle.highlight, styles.highlight]}/>
256229
<View style={selectContainerStyle}>
257-
{iconElement}
230+
{this.renderIconElement(evaStyle.icon)}
258231
</View>
259232
</View>
260-
{textElement}
261-
</TouchableOpacity>
233+
<FalsyText
234+
style={evaStyle.text}
235+
component={text}
236+
/>
237+
</TouchableWithoutFeedback>
262238
);
263239
}
264240
}
@@ -279,8 +255,6 @@ const styles = StyleSheet.create({
279255
highlight: {
280256
position: 'absolute',
281257
},
282-
icon: {},
283-
text: {},
284258
});
285259

286260
const webStyles = Platform.OS === 'web' && StyleSheet.create({

0 commit comments

Comments
 (0)