Skip to content

Commit b67f07a

Browse files
committed
fix: fix select list item multi and single select
1 parent 3f3a059 commit b67f07a

9 files changed

Lines changed: 3130 additions & 121 deletions

File tree

src/components/Checkbox/Checkbox.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,50 @@ import {
99
import { Icon } from '../../icons';
1010
import { Theme, withTheme } from '../../theme';
1111

12+
export type CheckboxShape = 'circle' | 'square';
13+
1214
export interface CheckboxProps {
1315
theme: Theme;
1416
isChecked?: boolean;
1517
isDisabled?: boolean;
18+
/** Sometimes we just want the display of the checkbox */
19+
isInteractive?: boolean;
1620
checkedIcon?: React.ReactNode;
21+
/** @default square */
22+
shape?: CheckboxShape;
1723
onChange?: (e: GestureResponderEvent) => void | undefined;
1824
}
1925

2026
const CheckboxBase = (props: CheckboxProps & TouchableHighlightProps) => {
2127
const {
2228
isChecked = false,
2329
isDisabled = false,
30+
isInteractive = true,
2431
checkedIcon,
2532
onChange = () => null,
33+
shape = 'square',
2634
theme,
2735
...touchableHighlightProps
2836
} = props;
2937

3038
const {
3139
checkboxStyle,
3240
checkboxFocusBackgroundColor,
33-
} = theme.getCheckboxStyles(isChecked, isDisabled);
41+
} = theme.getCheckboxStyles(isChecked, isDisabled, shape);
3442

3543
return (
3644
<TouchableHighlight
3745
accessible
3846
style={checkboxStyle}
3947
underlayColor={checkboxFocusBackgroundColor}
40-
onPress={onChange}
41-
disabled={isDisabled}
48+
{...(isInteractive
49+
? {
50+
disabled: isDisabled,
51+
onPress: onChange,
52+
}
53+
: {
54+
disabled: true,
55+
})}
4256
{...touchableHighlightProps}
4357
>
4458
<View

src/components/SelectList/SelectList.mdx

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,53 @@ menu: Components
66
import { Playground, PropsTable } from 'docz';
77
import SelectList from './SelectList';
88
import SelectListItem from './SelectListItem';
9+
import { State } from 'react-powerplug';
910

1011
## Usage
1112

12-
### Default
13+
### Multi select
1314

1415
<Playground>
15-
<SelectList
16-
selectedValue="js"
17-
onValueChange={(itemValue, itemIndex) =>
18-
console.log('itemValue', itemValue)
19-
}
20-
>
21-
<SelectListItem label="Java" value="java" />
22-
<SelectListItem label="JavaScript" value="js" />
23-
</SelectList>
16+
<State initial={{ selectedValue: ['js', 'go'] }}>
17+
{({ state, setState }) => (
18+
<SelectList
19+
isMulti
20+
selectedValue={state.selectedValue}
21+
onValueChange={(newValue, itemIndex) => {
22+
setState({ selectedValue: newValue });
23+
}}
24+
>
25+
<SelectListItem label="Java" value="java" />
26+
<SelectListItem label="JavaScript" value="js" />
27+
<SelectListItem label="Elixir" value="elixir" />
28+
<SelectListItem label="Haskell" value="haskell" />
29+
<SelectListItem label="Rust" value="rust" />
30+
<SelectListItem label="Go" value="go" />
31+
</SelectList>
32+
)}
33+
</State>
34+
</Playground>
35+
36+
### Single select
37+
38+
<Playground>
39+
<State initial={{ selectedValue: 'go' }}>
40+
{({ state, setState }) => (
41+
<SelectList
42+
selectedValue={state.selectedValue}
43+
onValueChange={(newValue, itemIndex) => {
44+
setState({ selectedValue: newValue });
45+
}}
46+
>
47+
<SelectListItem label="Java" value="java" />
48+
<SelectListItem label="JavaScript" value="js" />
49+
<SelectListItem label="Elixir" value="elixir" />
50+
<SelectListItem label="Haskell" value="haskell" />
51+
<SelectListItem label="Rust" value="rust" />
52+
<SelectListItem label="Go" value="go" />
53+
</SelectList>
54+
)}
55+
</State>
2456
</Playground>
2557

2658
## Props

src/components/SelectList/SelectList.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const SelectListBase = (props: SelectListProps): any => {
2222
if (isMulti && Array.isArray(selectedValue)) {
2323
if (isSelected) {
2424
onValueChange(
25-
selectedValue.filter(val => val === itemValue),
25+
selectedValue.filter(val => val !== itemValue),
2626
itemIndex,
2727
);
2828
} else {

src/components/SelectList/SelectListItem.tsx

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,45 @@
11
import * as React from 'react';
22
import {
3-
TextStyle,
43
TouchableHighlight,
54
TouchableHighlightProps,
65
View,
7-
ViewStyle,
86
} from 'react-native';
97

10-
import { Icon } from '../../icons';
118
import { Theme, withTheme } from '../../theme';
129
import { SelectListSize } from '../../theme/component-variables/selectListVariables';
13-
import { Box } from '../Layout';
10+
import { SelectListStyles } from '../../theme/style-getters/getSelectListStyles';
11+
import { Checkbox } from '../Checkbox';
1412
import { Text } from '../Typography';
1513

16-
export interface SelectListItemProps extends TouchableHighlightProps {
14+
export interface SelectListItemBaseProps {
15+
index?: number;
16+
isSelected?: boolean;
17+
onSelect?: (value: string, index: number, isSelected: boolean) => void;
18+
}
19+
20+
export interface SelectListItemProps
21+
extends SelectListItemBaseProps,
22+
TouchableHighlightProps {
1723
theme: Theme;
1824
size?: SelectListSize;
1925
isDisabled?: boolean;
20-
onSelect?: (value: string, index: number, isSelected: boolean) => void;
21-
testID?: string;
2226
label: string;
23-
isSelected?: boolean;
24-
index?: number;
2527
value: string;
26-
checkedIcon?: React.ReactNode;
2728
/**
2829
* Inline styles for components
2930
*/
30-
dangerouslySetInlineStyle?: {
31-
containerStyle?: ViewStyle;
32-
wrapperStyle?: ViewStyle;
33-
textStyle?: TextStyle;
34-
};
31+
dangerouslySetInlineStyle?: Partial<SelectListStyles>;
3532
}
3633

3734
const SelectListItemBase = (props: SelectListItemProps) => {
3835
const {
39-
checkedIcon,
4036
dangerouslySetInlineStyle,
4137
index = 0,
4238
isDisabled = false,
4339
isSelected = false,
4440
label,
4541
onSelect = () => null,
4642
size = 'medium',
47-
testID,
4843
theme,
4944
value,
5045
...touchableHighlightProps
@@ -54,6 +49,7 @@ const SelectListItemBase = (props: SelectListItemProps) => {
5449
containerStyle,
5550
textStyle,
5651
focusBackgroundColor,
52+
wrapperStyle,
5753
} = theme.getSelectListStyles(size, isDisabled, isSelected);
5854

5955
return (
@@ -66,11 +62,11 @@ const SelectListItemBase = (props: SelectListItemProps) => {
6662
...(dangerouslySetInlineStyle &&
6763
dangerouslySetInlineStyle.containerStyle),
6864
}}
69-
testID={testID}
7065
{...touchableHighlightProps}
7166
>
7267
<View
7368
style={{
69+
...wrapperStyle,
7470
...(dangerouslySetInlineStyle &&
7571
dangerouslySetInlineStyle.wrapperStyle),
7672
}}
@@ -86,17 +82,7 @@ const SelectListItemBase = (props: SelectListItemProps) => {
8682
>
8783
{label}
8884
</Text>
89-
{isSelected && (
90-
<Box position="absolute" right={0} marginRight={4}>
91-
{checkedIcon || (
92-
<Icon
93-
name="check"
94-
size={22}
95-
color={theme.themeVariables.colors.text.success}
96-
/>
97-
)}
98-
</Box>
99-
)}
85+
<Checkbox isInteractive={false} shape="circle" isChecked={isSelected} />
10086
</View>
10187
</TouchableHighlight>
10288
);

src/theme/component-variables/checkboxVariables.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,18 @@ import { ViewStyle } from 'react-native';
22

33
import { ThemeVariables } from '../ThemeInterface';
44

5-
export type CheckboxAppearanceStyles = ViewStyle & {
6-
backgroundColor: string;
7-
borderColor: string;
8-
};
9-
10-
export type BaseState = CheckboxAppearanceStyles;
11-
export type DisabledState = Partial<CheckboxAppearanceStyles>;
12-
export type FocusState = Partial<CheckboxAppearanceStyles>;
13-
export type CheckedState = Partial<CheckboxAppearanceStyles>;
5+
export interface ShapeStyle {
6+
circle: ViewStyle;
7+
square: ViewStyle;
8+
}
149

1510
export interface CheckboxVariables {
16-
base: BaseState;
17-
disabled: DisabledState;
18-
checked: CheckedState;
19-
checkedFocus: CheckedState;
20-
uncheckedFocus: CheckedState;
11+
base: ViewStyle;
12+
disabled: ViewStyle;
13+
checked: ViewStyle;
14+
checkedFocus: ViewStyle;
15+
uncheckedFocus: ViewStyle;
16+
shape: ShapeStyle;
2117
}
2218

2319
export const getCheckboxVariables = (
@@ -27,14 +23,13 @@ export const getCheckboxVariables = (
2723
base: {
2824
backgroundColor: themeVariables.colors.background.plain,
2925
borderColor: themeVariables.colors.border.default,
30-
borderRadius: themeVariables.controlBorderRadius.small,
3126
borderWidth: 1,
3227
height: 32,
3328
width: 32,
3429
},
3530
checked: {
3631
backgroundColor: themeVariables.colors.background.primary.default,
37-
borderColor: themeVariables.colors.border.primary,
32+
borderColor: 'transparent',
3833
},
3934
checkedFocus: {
4035
backgroundColor: themeVariables.colors.background.primary.focus,
@@ -43,6 +38,14 @@ export const getCheckboxVariables = (
4338
backgroundColor: themeVariables.colors.background.disabled,
4439
borderColor: themeVariables.colors.border.default,
4540
},
41+
shape: {
42+
circle: {
43+
borderRadius: 999,
44+
},
45+
square: {
46+
borderRadius: themeVariables.controlBorderRadius.small,
47+
},
48+
},
4649
uncheckedFocus: {
4750
backgroundColor: themeVariables.colors.background.tint1,
4851
},

src/theme/component-variables/selectListVariables.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface TextSizes {
3535
export type SelectListSize = keyof SelectListSizes;
3636

3737
export interface SelectListVariables {
38+
wrapper: ViewStyle;
3839
base: BaseState;
3940
disabled: DisabledState;
4041
selected: SelectedState;
@@ -82,5 +83,10 @@ export const getSelectListVariables = (
8283
},
8384
},
8485
textSizes: textSizes.size,
86+
wrapper: {
87+
alignItems: 'center',
88+
flexDirection: 'row',
89+
justifyContent: 'space-between',
90+
},
8591
};
8692
};
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import { ViewStyle } from 'react-native';
22

3+
import { CheckboxShape } from '../../components/Checkbox/Checkbox';
34
import { CheckboxVariables } from '../component-variables/checkboxVariables';
45

56
export type GetCheckboxStyles = (
67
isChecked: boolean,
78
isDisabled: boolean,
9+
shape: CheckboxShape,
810
) => {
911
checkboxStyle: ViewStyle;
1012
checkboxFocusBackgroundColor?: string;
1113
};
1214

1315
export const getCheckboxStyles = (
1416
checkboxVariables: CheckboxVariables,
15-
): GetCheckboxStyles => (isChecked, isDisabled) => {
17+
): GetCheckboxStyles => (isChecked, isDisabled, shape) => {
1618
return {
1719
checkboxFocusBackgroundColor: isChecked
1820
? checkboxVariables.checkedFocus.backgroundColor
@@ -21,6 +23,7 @@ export const getCheckboxStyles = (
2123
...checkboxVariables.base,
2224
...(isChecked ? checkboxVariables.checked : {}),
2325
...(isDisabled ? checkboxVariables.disabled : {}),
26+
...checkboxVariables.shape[shape],
2427
},
2528
};
2629
};

src/theme/style-getters/getSelectListStyles.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from '../component-variables/selectListVariables';
77

88
export interface SelectListStyles {
9+
wrapperStyle: ViewStyle;
910
containerStyle: ViewStyle;
1011
focusBackgroundColor: string;
1112
textStyle: TextStyle;
@@ -40,5 +41,6 @@ export const getSelectListStyles = (
4041
},
4142
focusBackgroundColor,
4243
textStyle: textSizes[size],
44+
wrapperStyle: selectListVariables.wrapper,
4345
};
4446
};

0 commit comments

Comments
 (0)