@hero-design/rn-work-uikit 1.6.0 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,179 +0,0 @@
1
- import React from 'react';
2
- import { within } from '@testing-library/react-native';
3
- import TextInputGroup from '..';
4
- import renderWithTheme from '../../../../../testUtils/renderWithTheme';
5
- import TextInput from '../..';
6
- import { theme } from '../../../..';
7
-
8
- describe('TextInputGroup', () => {
9
- it('should render', () => {
10
- const { getByText, getByTestId, toJSON } = renderWithTheme(
11
- <TextInputGroup>
12
- <TextInput label="Text Input 1" value="Text Input 1" required />
13
- <TextInput
14
- label="Text Input 2"
15
- value="Text Input 2"
16
- error="This is an error"
17
- testID="text-input-2"
18
- />
19
- <TextInput label="Text Input 3" value="Text Input 3" required />
20
- </TextInputGroup>
21
- );
22
-
23
- expect(toJSON()).toMatchSnapshot('xxx');
24
-
25
- expect(getByText('Text Input 1')).toBeVisible();
26
- expect(getByText('Text Input 2', { exact: false })).toBeVisible();
27
- expect(
28
- within(getByTestId('text-input-2')).getByText('(Optional)', {
29
- exact: false,
30
- })
31
- ).toBeVisible();
32
- expect(getByText('This is an error')).toBeVisible();
33
- expect(getByText('Text Input 3')).toBeVisible();
34
- });
35
-
36
- it('renders with correct border styling', () => {
37
- const { getByTestId } = renderWithTheme(
38
- <TextInputGroup>
39
- <TextInput value="Text Input 1" testID="text-input-1" />
40
- <TextInput value="Text Input 2" testID="text-input-2" />
41
- <TextInput value="Text Input 3" testID="text-input-3" />
42
- </TextInputGroup>
43
- );
44
-
45
- expect(
46
- within(getByTestId('text-input-1'))
47
- .getByTestId('text-input-border')
48
- .props.style.flat()
49
- ).toEqual(
50
- expect.arrayContaining([
51
- expect.objectContaining({
52
- borderBottomLeftRadius: 0,
53
- borderBottomRightRadius: 0,
54
- }),
55
- ])
56
- );
57
-
58
- expect(
59
- within(getByTestId('text-input-2'))
60
- .getByTestId('text-input-border')
61
- .props.style.flat()
62
- ).toEqual(
63
- expect.arrayContaining([
64
- expect.objectContaining({
65
- borderRadius: 0,
66
- }),
67
- ])
68
- );
69
-
70
- expect(
71
- within(getByTestId('text-input-3'))
72
- .getByTestId('text-input-border')
73
- .props.style.flat()
74
- ).toEqual(
75
- expect.arrayContaining([
76
- expect.objectContaining({
77
- borderTopLeftRadius: 0,
78
- borderTopRightRadius: 0,
79
- }),
80
- ])
81
- );
82
- });
83
-
84
- it('merges with the children styles in correct order', () => {
85
- const { getByTestId } = renderWithTheme(
86
- <TextInputGroup>
87
- <TextInput
88
- value="Text Input 1"
89
- testID="text-input-1"
90
- textStyle={{
91
- borderColor: '#ffffff',
92
- borderWidth: 1,
93
- }}
94
- />
95
- <TextInput
96
- value="Text Input 2"
97
- testID="text-input-2"
98
- style={{ width: 300 }}
99
- />
100
- </TextInputGroup>
101
- );
102
-
103
- // Merging text styles
104
- expect(
105
- within(getByTestId('text-input-1'))
106
- .getByTestId('text-input-border')
107
- .props.style.flat()
108
- ).toEqual(
109
- expect.arrayContaining([
110
- expect.objectContaining({
111
- // Passed style
112
- borderWidth: 1,
113
- borderColor: '#ffffff',
114
- // Injected style
115
- borderBottomLeftRadius: 0,
116
- borderBottomRightRadius: 0,
117
- }),
118
- ])
119
- );
120
-
121
- // Merging container styles
122
- expect(getByTestId('text-input-2').props.style.flat()).toEqual(
123
- expect.arrayContaining([
124
- expect.objectContaining({
125
- // Passed style
126
- width: 300,
127
- // Injected style
128
- marginTop: -theme.__hd__.textInput.borderWidths.container.normal,
129
- }),
130
- ])
131
- );
132
-
133
- expect(
134
- within(getByTestId('text-input-2'))
135
- .getByTestId('text-input-border')
136
- .props.style.flat()
137
- ).toEqual(
138
- expect.arrayContaining([
139
- expect.objectContaining({
140
- // Injected style
141
- borderTopLeftRadius: 0,
142
- borderTopRightRadius: 0,
143
- }),
144
- ])
145
- );
146
- });
147
-
148
- it('does not alter the children styles if there is only one child', () => {
149
- const { getByTestId } = renderWithTheme(
150
- <TextInputGroup>
151
- <TextInput
152
- value="Text Input 1"
153
- testID="text-input-1"
154
- style={{
155
- marginTop: theme.space.medium,
156
- }}
157
- />
158
- </TextInputGroup>
159
- );
160
-
161
- // Container style is not injected
162
- expect(getByTestId('text-input-1').props.style.flat()).toEqual(
163
- expect.arrayContaining([
164
- expect.objectContaining({
165
- // Passed style instead of injected style
166
- marginTop: theme.space.medium,
167
- }),
168
- ])
169
- );
170
-
171
- // Border style is not injected
172
- const internalBorderStyle =
173
- getByTestId('text-input-border').props.style.flat();
174
- const borderKeys = Object.keys(internalBorderStyle).filter((key) =>
175
- key.startsWith('border')
176
- );
177
- expect(borderKeys).toHaveLength(0);
178
- });
179
- });
@@ -1,73 +0,0 @@
1
- import { theme } from '../../../..';
2
- import { generateBorderStyle, generateMarginStyle } from '../utils';
3
-
4
- describe('utils', () => {
5
- describe('generateBorderStyle', () => {
6
- it('should generate the correct border style for the first child', () => {
7
- const borderStyle = generateBorderStyle({ index: 0, length: 3 });
8
- expect(borderStyle).toEqual({
9
- borderBottomLeftRadius: 0,
10
- borderBottomRightRadius: 0,
11
- });
12
- });
13
-
14
- it('should generate the correct border style for the last child', () => {
15
- const borderStyle = generateBorderStyle({ index: 2, length: 3 });
16
- expect(borderStyle).toEqual({
17
- borderTopLeftRadius: 0,
18
- borderTopRightRadius: 0,
19
- });
20
- });
21
-
22
- it('should generate the correct border style for the middle child', () => {
23
- const borderStyle = generateBorderStyle({ index: 1, length: 3 });
24
- expect(borderStyle).toEqual({
25
- borderRadius: 0,
26
- });
27
- });
28
- });
29
-
30
- describe('generateMarginStyle', () => {
31
- it('should generate the correct margin style for the first child', () => {
32
- const marginStyle = generateMarginStyle({
33
- index: 0,
34
- length: 3,
35
- theme,
36
- });
37
- expect(marginStyle).toEqual({
38
- marginTop: 0,
39
- });
40
- });
41
-
42
- it('should generate the correct margin style for the last child', () => {
43
- const marginStyle = generateMarginStyle({
44
- index: 2,
45
- length: 3,
46
- theme,
47
- });
48
- expect(marginStyle).toEqual({
49
- marginTop: -theme.__hd__.textInput.borderWidths.container.normal,
50
- });
51
- });
52
-
53
- it('should generate the correct margin style for the middle child', () => {
54
- const marginStyle = generateMarginStyle({
55
- index: 1,
56
- length: 3,
57
- theme,
58
- });
59
- expect(marginStyle).toEqual({
60
- marginTop: -theme.__hd__.textInput.borderWidths.container.normal,
61
- });
62
- });
63
-
64
- it('should generate the correct margin style for a single child', () => {
65
- const marginStyle = generateMarginStyle({
66
- index: 0,
67
- length: 1,
68
- theme,
69
- });
70
- expect(marginStyle).toEqual({});
71
- });
72
- });
73
- });
@@ -1,107 +0,0 @@
1
- import React, { ReactNode, useMemo } from 'react';
2
- import { StyleProp, StyleSheet, ViewProps, ViewStyle } from 'react-native';
3
- import { Box, useTheme } from '@hero-design/rn';
4
- import { generateBorderStyle, generateMarginStyle } from './utils';
5
- import { useDeprecation } from '../../../utils/hooks';
6
-
7
- export interface TextInputGroupProps extends ViewProps {
8
- /**
9
- * The children of the TextInputGroup. In order for the group styling to work,
10
- * they must be either HD TextInput components or enhanced TextInput components
11
- * that supports TextInput interface.
12
- *
13
- * Example:
14
- * const EnhancedTextInput = (props: TextInputProps) => {
15
- * return <TextInput {...props} />;
16
- * };
17
- *
18
- * <TextInput.Group>
19
- * <TextInput ... />
20
- * <EnhancedTextInput ... />
21
- * </TextInput.Group>
22
- */
23
- children: ReactNode;
24
- /**
25
- * The style of the TextInputGroup.
26
- */
27
- style?: StyleProp<ViewStyle>;
28
- /**
29
- * The testID of the TextInputGroup.
30
- */
31
- testID?: string;
32
- }
33
-
34
- const TextInputGroup = ({
35
- children,
36
- style,
37
- testID,
38
- ...props
39
- }: TextInputGroupProps) => {
40
- useDeprecation(
41
- 'TextInput.Group is deprecated. Please use FormGroup instead.',
42
- true
43
- );
44
- const theme = useTheme();
45
- const childrenArray = React.Children.toArray(children).filter(
46
- React.isValidElement
47
- );
48
-
49
- // If there are multiple children, inject styles to group them together.
50
- const groupedChildren = useMemo(
51
- () =>
52
- childrenArray.map((child, index) => {
53
- const rawChildStyle = (child as React.ReactElement).props.style;
54
- const rawChildTextStyle = (child as React.ReactElement).props.textStyle;
55
-
56
- // Handle array styles by flattening them first
57
- const childStyle = StyleSheet.flatten(rawChildStyle);
58
- const childTextStyle = StyleSheet.flatten(rawChildTextStyle);
59
-
60
- /**
61
- * Merge the child style with the group injected style.
62
- * Order of precedence:
63
- * 1. Child style.
64
- * 2. Group injected style.
65
- */
66
- const mergedStyle = {
67
- ...childStyle,
68
- ...generateMarginStyle({
69
- index,
70
- length: childrenArray.length,
71
- theme,
72
- }),
73
- };
74
-
75
- /**
76
- * Merge the child text style with the group text style.
77
- * Order of precedence:
78
- * 1. Group text style through textStyle prop.
79
- * 2. Child text style.
80
- * 3. Group injected border style.
81
- */
82
- const mergedTextStyle = {
83
- ...childTextStyle,
84
- ...generateBorderStyle({
85
- index,
86
- length: childrenArray.length,
87
- }),
88
- };
89
-
90
- return React.cloneElement(child as React.ReactElement, {
91
- style: mergedStyle,
92
- textStyle: mergedTextStyle,
93
- // Internal text input prop to allow for different styling
94
- groupStyleEnabled: true,
95
- });
96
- }),
97
- [childrenArray, theme]
98
- );
99
-
100
- return (
101
- <Box style={style} testID={testID} {...props}>
102
- {groupedChildren}
103
- </Box>
104
- );
105
- };
106
-
107
- export default TextInputGroup;
@@ -1,67 +0,0 @@
1
- import { CSSProperties } from 'react';
2
- import { Theme } from '@hero-design/rn';
3
-
4
- /**
5
- * Generates the border style for the TextInputGroup.
6
- * @param index - The index of the TextInput.
7
- * @param length - The length of the TextInputGroup.
8
- * @returns The border style for the TextInputGroup.
9
- */
10
- const generateBorderStyle = ({
11
- index,
12
- length,
13
- }: {
14
- index: number;
15
- length: number;
16
- }): CSSProperties => {
17
- const isFirst = index === 0;
18
- const isLast = index === length - 1;
19
-
20
- if (length === 1) {
21
- return {};
22
- }
23
-
24
- if (isFirst) {
25
- return {
26
- borderBottomLeftRadius: 0,
27
- borderBottomRightRadius: 0,
28
- };
29
- }
30
-
31
- if (isLast) {
32
- return {
33
- borderTopLeftRadius: 0,
34
- borderTopRightRadius: 0,
35
- };
36
- }
37
-
38
- return {
39
- borderRadius: 0,
40
- };
41
- };
42
-
43
- const generateMarginStyle = ({
44
- index,
45
- length,
46
- theme,
47
- }: {
48
- index: number;
49
- length: number;
50
- theme: Theme;
51
- }) => {
52
- if (length === 1) {
53
- return {};
54
- }
55
-
56
- if (index === 0) {
57
- return {
58
- marginTop: 0,
59
- };
60
- }
61
-
62
- return {
63
- marginTop: -theme.__hd__.textInput.borderWidths.container.normal,
64
- };
65
- };
66
-
67
- export { generateBorderStyle, generateMarginStyle };