@nuskin/foundation-ui-components 1.1.0 → 1.1.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.
Files changed (135) hide show
  1. package/package.json +15 -11
  2. package/src/common/accordion/NsAccordion.tsx +32 -40
  3. package/src/common/accordion/NsAccordionHeader.tsx +29 -8
  4. package/src/common/accordion/NsAccordionItem.tsx +24 -4
  5. package/src/common/accordion/NsCollapse.tsx +96 -35
  6. package/src/common/accordion/NsToggleSection.tsx +16 -3
  7. package/src/common/accordion/ToggleSection.styled.tsx +11 -7
  8. package/src/common/accordion/colorMap.ts +117 -0
  9. package/src/common/accordion/enums.ts +18 -0
  10. package/src/common/accordion/index.ts +1 -0
  11. package/src/common/accordion/specs/NsAccordion.spec.tsx +101 -31
  12. package/src/common/accordion/specs/NsAccordionHeader.spec.tsx +77 -10
  13. package/src/common/accordion/specs/NsAccordionItem.spec.tsx +27 -10
  14. package/src/common/accordion/specs/NsCollapse.spec.tsx +109 -17
  15. package/src/common/accordion/specs/NsToggleSection.spec.tsx +50 -14
  16. package/src/common/accordion/stories/NsAccordion.stories.tsx +31 -13
  17. package/src/common/accordion/stories/NsAccordionHeader.stories.tsx +5 -1
  18. package/src/common/accordion/stories/NsAccordionItem.stories.tsx +8 -3
  19. package/src/common/accordion/stories/NsCollapse.stories.tsx +19 -4
  20. package/src/common/accordion/stories/NsToggleSection.stories.tsx +6 -3
  21. package/src/common/accordion/stories/data.tsx +140 -19
  22. package/src/common/accordion/types.d.ts +55 -47
  23. package/src/common/alert/Header.tsx +29 -0
  24. package/src/common/alert/NsAlert.spec.tsx +46 -13
  25. package/src/common/alert/NsAlert.stories.tsx +1 -1
  26. package/src/common/alert/{NsAlert.styled.ts → NsAlert.styled.tsx} +22 -22
  27. package/src/common/alert/NsAlert.tsx +22 -87
  28. package/src/common/alert/types.d.ts +7 -7
  29. package/src/common/auth-dropdown/NsAuthDropdown.spec.tsx +23 -5
  30. package/src/common/auth-dropdown/NsAuthDropdown.styled.ts +9 -2
  31. package/src/common/auth-dropdown/NsAuthDropdown.tsx +4 -4
  32. package/src/common/autocomplete/NsAutocomplete.spec.tsx +34 -11
  33. package/src/common/autocomplete/NsAutocomplete.stories.tsx +18 -2
  34. package/src/common/autocomplete/NsAutocomplete.tsx +63 -24
  35. package/src/common/autocomplete/mock/MockData.ts +50 -7
  36. package/src/common/autocomplete/mock/MockUI.tsx +2 -1
  37. package/src/common/autocomplete/types.d.ts +7 -4
  38. package/src/common/banner/NsBanner.spec.tsx +12 -3
  39. package/src/common/brand-components/NsBrandComponent.tsx +2 -2
  40. package/src/common/breadcrumb/NsBreadcrumb.spec.tsx +8 -3
  41. package/src/common/breadcrumb/data.ts +8 -5
  42. package/src/common/button/NsButton.spec.tsx +65 -49
  43. package/src/common/button/NsButton.stories.tsx +40 -4
  44. package/src/common/button/NsButton.styled.tsx +49 -66
  45. package/src/common/button/NsButton.tsx +1 -2
  46. package/src/common/button/helper.ts +82 -0
  47. package/src/common/card/NsFfgCard.tsx +13 -7
  48. package/src/common/card/specs/NsFfgCard.spec.tsx +18 -8
  49. package/src/common/card/stories/NsFfgCard.stories.tsx +47 -14
  50. package/src/common/card/styled/NsFfgStyled.tsx +10 -7
  51. package/src/common/card/types.d.ts +7 -4
  52. package/src/common/carousal/NsCarousal.tsx +3 -9
  53. package/src/common/cart-icon/NsCartIcon.spec.tsx +5 -16
  54. package/src/common/cart-icon/NsCartIcon.stories.tsx +1 -1
  55. package/src/common/cart-icon/NsCartIcon.styled.ts +7 -33
  56. package/src/common/cart-icon/NsCartIcon.tsx +5 -14
  57. package/src/common/cart-icon/types.d.ts +1 -0
  58. package/src/common/checkbox/NsCheckbox.styled.tsx +15 -16
  59. package/src/common/checkbox/NsCheckbox.tsx +1 -1
  60. package/src/common/column-control/styled/common.ts +1 -1
  61. package/src/common/column-control/utils.ts +4 -4
  62. package/src/common/common.types.d.ts +14 -5
  63. package/src/common/dropdown/NsDropdown.styled.tsx +23 -18
  64. package/src/common/hooks/index.ts +2 -0
  65. package/src/common/hooks/specs/useWindowSize.spec.tsx +19 -0
  66. package/src/common/hooks/useAccordionMap.tsx +39 -0
  67. package/src/common/hooks/useWindowSize.tsx +30 -0
  68. package/src/common/icon/NsIcon.spec.tsx +1 -1
  69. package/src/common/icon/NsIcon.stories.tsx +3 -0
  70. package/src/common/icon/NsIcon.tsx +6 -2
  71. package/src/common/icon/NsIconVariants.ts +4 -1
  72. package/src/common/icon/icons/AlertErrorIcon.tsx +2 -2
  73. package/src/common/icon/icons/ArrowLeft.tsx +3 -6
  74. package/src/common/icon/icons/ArrowRight.tsx +6 -8
  75. package/src/common/icon/icons/Cart.tsx +7 -32
  76. package/src/common/icon/icons/CartBadge.tsx +2 -2
  77. package/src/common/icon/icons/CloseIcon.tsx +1 -1
  78. package/src/common/icon/icons/ErrorIcon.tsx +2 -2
  79. package/src/common/icon/icons/HamburgerIcon.tsx +5 -2
  80. package/src/common/icon/icons/SearchIcon.tsx +1 -1
  81. package/src/common/icon/icons/SuccessIcon.tsx +2 -7
  82. package/src/common/icon/icons/TrashIcon.tsx +1 -1
  83. package/src/common/icon/icons/UserIcon.tsx +3 -6
  84. package/src/common/icon/icons/UserOutline.tsx +24 -0
  85. package/src/common/icon/icons/WarningIcon.tsx +1 -1
  86. package/src/common/icon/icons/home.tsx +1 -1
  87. package/src/common/image/NsImage.spec.tsx +56 -6
  88. package/src/common/image/NsImage.tsx +23 -11
  89. package/src/common/index.ts +6 -2
  90. package/src/common/input/InputWrapper.tsx +12 -7
  91. package/src/common/input/NsInputField.tsx +1 -1
  92. package/src/common/modal/NsModal.spec.tsx +46 -0
  93. package/src/common/modal/NsModal.styled.tsx +28 -0
  94. package/src/common/modal/NsModal.tsx +37 -0
  95. package/src/common/modal/helper-component/NsModalContentBox.tsx +32 -0
  96. package/src/common/modal/helper-component/NsModalDefault.tsx +25 -0
  97. package/src/common/modal/helper-component/NsModalStatus.tsx +15 -0
  98. package/src/common/modal/helper-component/NsModalStatusIcon.tsx +14 -0
  99. package/src/common/modal/helper-component/NsModalTitle.tsx +26 -0
  100. package/src/common/modal/index.ts +2 -0
  101. package/src/common/modal/model/NsModalVariantMap.tsx +24 -0
  102. package/src/common/modal/stories/NsModal.stories.tsx +85 -0
  103. package/src/common/modal/stories/mock.tsx +46 -0
  104. package/src/common/modal/types.ts +56 -0
  105. package/src/common/nav-tab/NavDrawer.tsx +34 -0
  106. package/src/common/nav-tab/NavTitles.tsx +52 -0
  107. package/src/common/nav-tab/NsNavTab.spec.tsx +4 -5
  108. package/src/common/nav-tab/NsNavTab.stories.tsx +3 -3
  109. package/src/common/nav-tab/NsNavTab.styled.tsx +13 -7
  110. package/src/common/nav-tab/NsNavTab.tsx +21 -75
  111. package/src/common/nav-tab/index.ts +1 -1
  112. package/src/common/nav-tab/types.d.ts +3 -3
  113. package/src/common/radiogroup/NsRadioGroup.spec.tsx +24 -25
  114. package/src/common/radiogroup/NsRadioGroup.stories.tsx +13 -31
  115. package/src/common/radiogroup/NsRadioGroup.styled.tsx +20 -19
  116. package/src/common/radiogroup/NsRadioGroup.tsx +6 -5
  117. package/src/common/search-bar/NsSearchBar.tsx +1 -3
  118. package/src/common/select/NsSelect.spec.tsx +10 -17
  119. package/src/common/select/NsSelect.styled.tsx +53 -34
  120. package/src/common/select/stories/NsSelect.stories.tsx +10 -10
  121. package/src/common/select/types.d.ts +3 -3
  122. package/src/common/spinner/NsSpinner.spec.tsx +21 -0
  123. package/src/common/spinner/NsSpinner.stories.tsx +26 -0
  124. package/src/common/spinner/NsSpinner.styled.tsx +344 -0
  125. package/src/common/spinner/NsSpinner.tsx +14 -0
  126. package/src/common/spinner/icons/Spinner.tsx +161 -0
  127. package/src/common/spinner/index.ts +2 -0
  128. package/src/common/spinner/types.ts +5 -0
  129. package/src/common/table/NsTable.tsx +32 -49
  130. package/src/common/table/NsTableHeader.tsx +30 -51
  131. package/src/common/util.ts +1 -0
  132. package/src/common/utility/Show.tsx +13 -0
  133. package/src/common/utility/index.ts +1 -0
  134. package/src/common/videos/types.d.ts +1 -1
  135. /package/src/common/brand-components/{NsBrandContanier.styled.tsx → NsBrandContainer.styled.tsx} +0 -0
@@ -3,27 +3,27 @@ import { styled, css, ColorUtils, FoundationTheme } from '@nuskin/foundation-the
3
3
 
4
4
  const { color } = ColorUtils;
5
5
 
6
- import type { AlertContainerParams, AlertType, AlertParams, TypeMapCSS } from './types.d';
7
- const gutter_width = '10px';
8
- const close_button_width = '12px';
9
- const icon_width = '24px';
6
+ import type { AlertContainerParams, AlertType, AlertParams, TypeMapCSS } from './types';
7
+ const gutterWidth = '10px';
8
+ const closeButtonWidth = '12px';
9
+ const iconWidth = '24px';
10
10
 
11
11
  const TypeMap: TypeMapCSS = {
12
12
  banner: {
13
- 'min-height': '114px',
13
+ minHeight: '114px',
14
14
  padding: '15px 45px',
15
- 'content-column-start': 'row-center',
16
- 'content-column-end': 'row-center',
17
- 'bar-position': 'top',
18
- 'foot-visibility': 'block',
15
+ contentColumnStart: 'row-center',
16
+ contentColumnEnd: 'row-center',
17
+ barPosition: 'top',
18
+ footVisibility: 'block',
19
19
  },
20
20
  card: {
21
- 'min-height': '72px',
21
+ minHeight: '72px',
22
22
  padding: '24px 24px',
23
- 'content-column-start': 'row-left',
24
- 'content-column-end': 'row-right',
25
- 'bar-position': 'left',
26
- 'foot-display': 'none',
23
+ contentColumnStart: 'row-left',
24
+ contentColumnEnd: 'row-right',
25
+ barPosition: 'left',
26
+ footDisplay: 'none',
27
27
  },
28
28
  };
29
29
 
@@ -53,14 +53,14 @@ const fontFamilyStyles = css`
53
53
  export const Alert = styled.div<AlertParams>`
54
54
  position: relative;
55
55
  display: grid;
56
- grid-template-columns: [row-left] ${icon_width} [space] ${gutter_width} [row-center] auto [space] ${gutter_width} [row-right] ${close_button_width};
56
+ grid-template-columns: [row-left] ${iconWidth} [space] ${gutterWidth} [row-center] auto [space] ${gutterWidth} [row-right] ${closeButtonWidth};
57
57
  grid-template-rows: [row1] auto;
58
58
  grid-auto-row: auto;
59
59
  grid-auto-flow: row;
60
60
  background-color: ${color('primary', 'white')};
61
61
  border: 1px solid ${color('primary', 'black')};
62
- min-height: ${({ type }) => TypeMap?.[type]?.['min-height']};
63
- padding: ${({ type }) => TypeMap?.[type]?.['padding']};
62
+ min-height: ${({ type }) => TypeMap?.[type]?.minHeight};
63
+ padding: ${({ type }) => TypeMap?.[type]?.padding};
64
64
  color: ${(props) => color(contextType?.[props?.context] as FoundationTheme.AppColors, '40')(props)} };
65
65
  `;
66
66
 
@@ -78,8 +78,8 @@ export const AlertIcon = styled.div`
78
78
  grid-column-end: row-left;
79
79
  grid-row-start: row1;
80
80
  grid-row-end: row1;
81
- width: ${icon_width};
82
- height: ${icon_width};
81
+ width: ${iconWidth};
82
+ height: ${iconWidth};
83
83
  `;
84
84
 
85
85
  export const AlertHead = styled.div`
@@ -102,14 +102,14 @@ export const AlertHead = styled.div`
102
102
  export const AlertContent = styled.div<{ type: AlertType }>`
103
103
  margin-top: 8px;
104
104
  color: ${color('primary', 'black')};
105
- grid-column-start: ${({ type }) => TypeMap?.[type]?.['content-column-start']};
106
- grid-column-end: ${({ type }) => TypeMap?.[type]?.['content-column-end']};
105
+ grid-column-start: ${({ type }) => TypeMap?.[type]?.contentColumnStart};
106
+ grid-column-end: ${({ type }) => TypeMap?.[type]?.contentColumnEnd};
107
107
  `;
108
108
 
109
109
  export const AlertFoot = styled.div<{ type: AlertType }>`
110
110
  margin-top: 8px;
111
111
  color: ${color('primary', 'black')};
112
- display: ${({ type }) => TypeMap?.[type]?.['foot-display']};
112
+ display: ${({ type }) => TypeMap?.[type]?.['footDisplay']};
113
113
  ${fontFamilyStyles}
114
114
  font-weight: 300;
115
115
  font-size: 14px;
@@ -1,6 +1,4 @@
1
1
  import React, { useEffect, useState } from 'react';
2
- import { FoundationTheme } from '@nuskin/foundation-theme';
3
-
4
2
  import {
5
3
  AlertContainer,
6
4
  Alert,
@@ -11,44 +9,15 @@ import {
11
9
  AlertFoot,
12
10
  AlertBody,
13
11
  } from './NsAlert.styled';
14
- import type { NsAlertProps, HeaderProps, ContextType, AlertType } from './types.d';
12
+ import type { NsAlertProps, ContextType, AlertType } from './types.d';
15
13
  import { NsIcon } from '../icon';
16
- import { NsIconNameType } from '../icon/types';
17
-
18
- const Header: React.FC<HeaderProps> = ({ children, head }) => (
19
- <>
20
- {children}
21
- {head}
22
- </>
23
- );
24
-
25
- function isValidContextValue(value: string): boolean {
26
- return (value as ContextType) !== undefined;
27
- }
28
-
29
- function isValidTypeValue(value: string): boolean {
30
- return (value as AlertType) !== undefined;
31
- }
32
-
33
- const colorMap: { [key: string]: FoundationTheme.PaletteColors } = {
34
- error: 'error',
35
- notice: 'info',
36
- success: 'secondary',
37
- warning: 'warning',
38
- default: 'info',
39
- };
40
-
41
- const iconMap: { [key: string]: NsIconNameType } = {
42
- error: 'alertError',
43
- success: 'success',
44
- warning: 'warning',
45
- default: 'infoFilled',
46
- };
14
+ import Header, { colorMap, iconMap } from './Header';
15
+ import useWindowSize from '../hooks/useWindowSize';
47
16
 
48
17
  export default function NsAlert({
49
18
  context = 'info',
50
19
  title = 'Information',
51
- threshhold = 600,
20
+ threshold = 600,
52
21
  type = 'banner',
53
22
  visible = false,
54
23
  head,
@@ -57,76 +26,42 @@ export default function NsAlert({
57
26
  onDismiss = undefined,
58
27
  }: NsAlertProps) {
59
28
  const [alertType, setAlertType] = useState('banner');
60
- const [windowWidth, setWindowWidth] = useState(0);
29
+ const windowSize = useWindowSize();
61
30
 
62
31
  const hasDismissListener = typeof onDismiss === 'function';
63
32
 
64
- const getColor = () => {
65
- return colorMap[context] || colorMap.default;
66
- };
67
-
68
- const getIcon = () => {
69
- return iconMap[context] || iconMap.default;
70
- };
71
-
72
- const getTitleText = () => {
73
- if (title) {
74
- return title;
75
- }
76
- };
77
-
78
- const handleDismiss = () => {
79
- if (hasDismissListener) {
80
- onDismiss();
81
- }
82
- };
83
-
84
- const handleResize = () => {
85
- setWindowWidth(window.innerWidth);
86
- };
87
-
88
- useEffect(() => {
89
- let aType = '';
90
- if (type === 'auto') {
91
- aType = windowWidth < threshhold ? 'card' : 'banner';
92
- } else {
93
- aType = type;
94
- }
95
- setAlertType(aType);
96
- }, [threshhold, type, windowWidth]);
97
-
98
33
  useEffect(() => {
99
- if (!isValidContextValue(context)) {
34
+ if (!context) {
100
35
  console.error(`Invalid prop value for 'context': ${context}`);
101
36
  }
102
-
103
- if (!isValidTypeValue(type)) {
37
+ if (!type) {
104
38
  console.error(`Invalid prop value for 'type': ${type}`);
105
39
  }
106
- }, [context, type]);
107
-
108
- useEffect(() => {
109
- setWindowWidth(window?.innerWidth);
110
- window.addEventListener('resize', handleResize);
111
- return () => {
112
- window.removeEventListener('resize', handleResize);
113
- };
114
- }, []);
40
+ if (type === 'auto') {
41
+ setAlertType(windowSize.width < threshold ? 'card' : 'banner');
42
+ } else {
43
+ setAlertType(type);
44
+ }
45
+ }, [threshold, type, windowSize.width, context]);
115
46
 
116
47
  return (
117
48
  <AlertContainer visible={visible} data-testid="alert-container">
118
49
  <Alert type={alertType as AlertType} context={context as ContextType}>
119
50
  <AlertIcon>
120
- <NsIcon name={getIcon()} color={getColor()} size="large" />
51
+ <NsIcon
52
+ name={iconMap[context] || iconMap.default}
53
+ color={colorMap[context] || colorMap.default}
54
+ size="large"
55
+ />
121
56
  </AlertIcon>
122
57
  <AlertHead>
123
- <Header head={<span>{getTitleText()}</span>}>{head}</Header>
58
+ <Header head={<span>{title}</span>}>{head}</Header>
124
59
  </AlertHead>
125
- {hasDismissListener ? (
126
- <AlertCloseButton data-testid="alert-close-button" onClick={handleDismiss}>
60
+ {hasDismissListener && (
61
+ <AlertCloseButton data-testid="alert-close-button" onClick={onDismiss}>
127
62
  <NsIcon name="close" size="small" />
128
63
  </AlertCloseButton>
129
- ) : null}
64
+ )}
130
65
  <AlertContent type={alertType as AlertType}>
131
66
  <AlertBody>{body}</AlertBody>
132
67
  {foot ? <AlertFoot type={alertType as AlertType}>{foot}</AlertFoot> : null}
@@ -3,7 +3,7 @@ import { ReactNode, CSSProperties } from 'react';
3
3
  export interface NsAlertProps {
4
4
  context?: string;
5
5
  title?: string;
6
- threshhold?: number; // TODO: update to threshold, i.e. remove extra h
6
+ threshold?: number;
7
7
  type?: string;
8
8
  visible?: boolean;
9
9
  head?: ReactNode;
@@ -30,12 +30,12 @@ export type AlertParams = {
30
30
  };
31
31
 
32
32
  export type CustomCSSProperties = CSSProperties & {
33
- 'min-height'?: string;
34
- 'content-column-start'?: string;
35
- 'content-column-end'?: string;
36
- 'bar-position'?: string;
37
- 'foot-visibility'?: string;
38
- 'foot-display'?: string;
33
+ minHeight?: string;
34
+ contentColumnStart?: string;
35
+ contentColumnEnd?: string;
36
+ barPosition?: string;
37
+ footVisibility?: string;
38
+ footDisplay?: string;
39
39
  };
40
40
 
41
41
  export interface TypeMapCSS {
@@ -10,27 +10,45 @@ describe('NsAuthDropdown', () => {
10
10
  signUpLinkText: 'Sign Up',
11
11
  signUpLinkPrefixText: 'Do not have an account?',
12
12
  titleText: 'Welcome',
13
- closeOnMouseLeave: false,
14
13
  };
15
14
 
16
- it('should render component with title text', () => {
15
+ it('1. should render component with title text', () => {
17
16
  render(<NsAuthDropdown {...props} />);
18
17
  expect(screen.getByText('Welcome')).toBeInTheDocument();
19
18
  });
20
19
 
21
- it('should render the sign in button with text', () => {
20
+ it('2. should render the sign in button with text', () => {
22
21
  render(<NsAuthDropdown {...props} />);
23
22
  const authHead = screen.getByTestId('auth-head');
24
23
  fireEvent.click(authHead);
25
24
  expect(screen.getByTestId('sign-in-btn')).toBeInTheDocument();
26
- expect(screen.getByTestId('sign-in-btn')).toHaveTextContent('Sign In');
25
+ expect(screen.getByTestId('sign-in-btn')).toHaveTextContent(props.signInBtnText);
26
+ expect(screen.getByTestId('signup-link')).toHaveTextContent(props.signUpLinkText);
27
27
  });
28
28
 
29
- it('should have the correct href attribute in SignUpLink', () => {
29
+ it('3. should have the correct href attribute in SignUpLink', () => {
30
30
  render(<NsAuthDropdown {...props} />);
31
31
  const authHead = screen.getByTestId('auth-head');
32
32
  fireEvent.click(authHead);
33
33
  const signUpLink = screen.getByTestId('signup-link');
34
34
  expect(signUpLink).toHaveAttribute('href', '/signUp');
35
35
  });
36
+ it('4. should close dropdown on mouse leave', () => {
37
+ const { container } = render(<NsAuthDropdown {...props} closeOnMouseLeave={true} />);
38
+ const authHead = screen.getByTestId('auth-head');
39
+ fireEvent.click(authHead);
40
+ expect(screen.getByTestId('sign-in-btn')).toHaveTextContent('Sign In');
41
+ const wrapper = container.querySelector('.auth-wrapper');
42
+ if (wrapper) fireEvent.mouseLeave(wrapper);
43
+ expect(container.querySelector('.auth-wrapper')).toBeNull();
44
+ });
45
+ it('5. should not close dropdown on mouse leave', () => {
46
+ const { container } = render(<NsAuthDropdown {...props} closeOnMouseLeave={false} />);
47
+ const authHead = screen.getByTestId('auth-head');
48
+ fireEvent.click(authHead);
49
+ expect(screen.getByTestId('sign-in-btn')).toHaveTextContent('Sign In');
50
+ const wrapper = container.querySelector('.auth-wrapper');
51
+ if (wrapper) fireEvent.mouseLeave(wrapper);
52
+ expect(container.querySelector('.auth-wrapper')).not.toBeNull();
53
+ });
36
54
  });
@@ -1,4 +1,9 @@
1
1
  import { styled, ColorUtils } from '@nuskin/foundation-theme';
2
+ import { DarkModeEnabledOption } from '../common.types';
3
+ import { skipDarkModeEnabled } from '../util';
4
+
5
+ //[TODO-Arun] - Use from custom color variables
6
+ // const MAGENTA_COLOR = '#bb29bb';
2
7
 
3
8
  const { color, paletteColor } = ColorUtils;
4
9
 
@@ -44,8 +49,10 @@ export const Triangle = styled.div`
44
49
  box-shadow: 0 0 5px 0 ${color('neutral', '20')};
45
50
  }
46
51
  `;
47
-
48
- export const AuthHead = styled.div<{ darkModeEnabled: boolean }>`
52
+ const options = {
53
+ shouldForwardProp: skipDarkModeEnabled,
54
+ };
55
+ export const AuthHead = styled('div', options)<DarkModeEnabledOption>`
49
56
  cursor: pointer;
50
57
  padding: 5px 10px;
51
58
  display: flex;
@@ -1,4 +1,4 @@
1
- import { useState } from 'react';
1
+ import { useCallback, useState } from 'react';
2
2
  import { NsIcon } from '../icon';
3
3
  import {
4
4
  AuthDropdownContainer,
@@ -28,11 +28,11 @@ const NsAuthDropdown = ({
28
28
  setIsOpen((prevIsOpen) => !prevIsOpen);
29
29
  };
30
30
 
31
- const closeDropdown = () => {
31
+ const closeDropdown = useCallback(() => {
32
32
  if (closeOnMouseLeave) {
33
33
  setIsOpen(false);
34
34
  }
35
- };
35
+ }, [closeOnMouseLeave]);
36
36
 
37
37
  return (
38
38
  <AuthDropdownContainer>
@@ -40,7 +40,7 @@ const NsAuthDropdown = ({
40
40
  <Button>{titleText}</Button>
41
41
  <NsIcon testId="chevron-icon" name={isOpen ? 'arrowUp' : 'arrowDown'} color="primary" />
42
42
  {isOpen ? (
43
- <div onMouseLeave={closeDropdown}>
43
+ <div onMouseLeave={closeDropdown} className="auth-wrapper">
44
44
  <Triangle />
45
45
  <AuthDropdown>
46
46
  <SignInButton href={signInRedirectLink} data-testid="sign-in-btn">
@@ -7,13 +7,12 @@ import userEvent from '@testing-library/user-event';
7
7
  import { act } from 'react-dom/test-utils';
8
8
 
9
9
  describe('CustomAutocomplete', () => {
10
- test('should render autocomplete component', () => {
10
+ it('1. should render autocomplete component', () => {
11
11
  render(
12
12
  <NsAutocomplete
13
13
  options={AutocompleteMockList}
14
- onSelect={() => {}}
15
14
  placeholder="autocomplete-input"
16
- renderOptions={OptionUiComponent}
15
+ OptionsUI={OptionUiComponent}
17
16
  inputValue=""
18
17
  value=""
19
18
  />
@@ -21,13 +20,34 @@ describe('CustomAutocomplete', () => {
21
20
  const autocompleteInput = screen.getByPlaceholderText('autocomplete-input');
22
21
  expect(autocompleteInput).toBeInTheDocument();
23
22
  });
23
+ it('2. check that onChange gets called', () => {
24
+ const onSelectMock = jest.fn();
25
+ const onChangeMock = jest.fn();
26
+ render(
27
+ <NsAutocomplete
28
+ options={AutocompleteMockList}
29
+ onSelect={onSelectMock}
30
+ onChange={onChangeMock}
31
+ placeholder="autocomplete-input"
32
+ OptionsUI={OptionUiComponent}
33
+ inputValue=""
34
+ value=""
35
+ />
36
+ );
37
+ const autocompleteInput = screen.getByPlaceholderText('autocomplete-input');
38
+ expect(autocompleteInput).toBeInTheDocument();
39
+ act(() => {
40
+ userEvent.type(autocompleteInput, 'GS Facial');
41
+ });
42
+ expect(onChangeMock).toHaveBeenCalled();
43
+ });
24
44
 
25
- test('displays options when typing in the input', async () => {
45
+ it('3. displays options when typing in the input', async () => {
26
46
  render(
27
47
  <NsAutocomplete
28
48
  options={AutocompleteMockList}
29
49
  onSelect={() => {}}
30
- renderOptions={OptionUiComponent}
50
+ OptionsUI={OptionUiComponent}
31
51
  placeholder="autocomplete-input"
32
52
  inputValue=""
33
53
  value=""
@@ -46,11 +66,13 @@ describe('CustomAutocomplete', () => {
46
66
  );
47
67
  }, 40000);
48
68
 
49
- test('selects an option when clicking on it', async () => {
69
+ it('4. selects an option when clicking on it', async () => {
70
+ const onSelectMock = jest.fn();
50
71
  render(
51
72
  <NsAutocomplete
73
+ onSelect={onSelectMock}
52
74
  options={AutocompleteMockList}
53
- renderOptions={OptionUiComponent}
75
+ OptionsUI={OptionUiComponent}
54
76
  placeholder="autocomplete-input"
55
77
  inputValue=""
56
78
  value=""
@@ -64,13 +86,14 @@ describe('CustomAutocomplete', () => {
64
86
  const optionLi = screen.getByText('GS Facial Spa');
65
87
  userEvent.click(optionLi);
66
88
  });
89
+ expect(onSelectMock).toHaveBeenCalled();
67
90
  }, 40000);
68
91
 
69
- test('should load more options when clicking load more button', async () => {
92
+ it('5. should load more options when clicking load more button', async () => {
70
93
  render(
71
94
  <NsAutocomplete
72
95
  options={AutocompleteMockList}
73
- renderOptions={OptionUiComponent}
96
+ OptionsUI={OptionUiComponent}
74
97
  placeholder="autocomplete-input"
75
98
  seeMore={{
76
99
  text: '',
@@ -91,11 +114,11 @@ describe('CustomAutocomplete', () => {
91
114
  });
92
115
  }, 40000);
93
116
 
94
- test('should clear options when clicking clear button', async () => {
117
+ it('6. should clear options when clicking clear button', async () => {
95
118
  render(
96
119
  <NsAutocomplete
97
120
  options={AutocompleteMockList}
98
- renderOptions={OptionUiComponent}
121
+ OptionsUI={OptionUiComponent}
99
122
  placeholder="autocomplete-input"
100
123
  clear={{
101
124
  text: '',
@@ -19,6 +19,8 @@ type ComponentType = typeof NsAutocomplete;
19
19
  type Story = StoryObj<ComponentType>;
20
20
  type MetaStory = Meta<ComponentType>;
21
21
 
22
+ const PAGE_SIZE = 6;
23
+
22
24
  export default {
23
25
  title: 'Components/Inputs/NsAutocomplete',
24
26
  component: NsAutocomplete,
@@ -53,6 +55,19 @@ export default {
53
55
  const RenderAutocomplete = (args: NsAutocompleteProps) => {
54
56
  const [selectedOption, setSelectedOption] = useState<string | OptionsProps | null>(null);
55
57
  const [searchTerms, setSearchTerms] = useState<string>('');
58
+ const [pageNo, setPageNo] = useState(PAGE_SIZE);
59
+
60
+ const onSeeMoreHandler = () => {
61
+ setPageNo(PAGE_SIZE + PAGE_SIZE);
62
+ };
63
+
64
+ const AutoCompleteProps = {
65
+ ...args,
66
+ seeMore: {
67
+ ...args.seeMore,
68
+ onSeeMore: onSeeMoreHandler,
69
+ },
70
+ } as NsAutocompleteProps;
56
71
 
57
72
  const onSelect = (value: string | OptionsProps | null) => {
58
73
  setSelectedOption(value);
@@ -71,10 +86,11 @@ const RenderAutocomplete = (args: NsAutocompleteProps) => {
71
86
  return (
72
87
  <div>
73
88
  <NsAutocomplete
74
- {...args}
89
+ {...AutoCompleteProps}
90
+ options={args.options.slice(0, pageNo)}
75
91
  onChange={handleInputChange}
76
92
  onSelect={onSelect}
77
- renderOptions={OptionUiComponent}
93
+ OptionsUI={OptionUiComponent}
78
94
  iconEnd={
79
95
  <IconButton onClick={onInputClear}>
80
96
  <NsIcon name="close" size="small" />
@@ -1,13 +1,17 @@
1
- import React, { PropsWithChildren } from 'react';
1
+ import React, { HTMLAttributes, PropsWithChildren, useCallback, useEffect, useRef, useState } from 'react';
2
2
  import { Autocomplete, AutocompleteRenderGroupParams, List, AutocompleteRenderInputParams } from '@mui/material';
3
3
  import { NsTypography, NsButton } from '../';
4
4
  import { NsAutocompleteProps, OptionsProps, ComponentBoxProps } from './types.d';
5
5
  import { StyledTextField } from './NsAutocomplete.styled';
6
6
 
7
+ const renderOptionLabel = (option: OptionsProps) => option.value;
8
+
9
+ const renderGroupBy = (option: OptionsProps) => option.group as string;
10
+
7
11
  const NsAutocomplete = ({
8
12
  id = 'custom_autocomplete_dropdown',
9
- options = [],
10
- renderOptions,
13
+ options,
14
+ OptionsUI,
11
15
  loading,
12
16
  noOptionsText,
13
17
  onSelect,
@@ -20,16 +24,34 @@ const NsAutocomplete = ({
20
24
  iconEnd,
21
25
  inputValue,
22
26
  value,
27
+ isFilterEnabled = true,
23
28
  }: PropsWithChildren<NsAutocompleteProps>) => {
24
- const OptionUI = renderOptions;
29
+ const [scrollTopPosition, setScrollTopPosition] = useState(0);
30
+ const listboxRef = useRef<HTMLUListElement | null>(null);
25
31
 
26
- const handleSelect = (event: React.SyntheticEvent<Element, Event>, value: string | OptionsProps | null) => {
27
- onSelect ? onSelect(value) : null;
28
- };
32
+ useEffect(() => {
33
+ if (listboxRef?.current && options.length > 0) {
34
+ listboxRef.current.scrollTop = scrollTopPosition;
35
+ }
36
+ }, [options, scrollTopPosition]);
29
37
 
30
- const handleInputChange = (event: React.SyntheticEvent<Element, Event>, newInputValue: string) => {
31
- onChange ? onChange(newInputValue) : null;
32
- };
38
+ const handleSelect = useCallback(
39
+ (event: React.SyntheticEvent, value: string | OptionsProps | null) => {
40
+ if (typeof onSelect === 'function') {
41
+ onSelect(value);
42
+ }
43
+ },
44
+ [onSelect]
45
+ );
46
+
47
+ const handleInputChange = useCallback(
48
+ (event: React.SyntheticEvent, newInputValue: string) => {
49
+ if (typeof onChange === 'function') {
50
+ onChange(newInputValue);
51
+ }
52
+ },
53
+ [onChange]
54
+ );
33
55
 
34
56
  const renderInput = (params: AutocompleteRenderInputParams) => (
35
57
  <StyledTextField
@@ -45,15 +67,11 @@ const NsAutocomplete = ({
45
67
 
46
68
  // TODO: @vijayprathap need to check if we can remove object spread of ...props and ...params in renderInput
47
69
  const renderOption = (props: ComponentBoxProps, option: OptionsProps) => (
48
- <li {...props}>
49
- <OptionUI {...props} option={option} />
70
+ <li {...props} key={props.id}>
71
+ {OptionsUI ? <OptionsUI {...props} option={option} /> : null}
50
72
  </li>
51
73
  );
52
74
 
53
- const renderOptionLabel = (option: OptionsProps) => option.value;
54
-
55
- const renderGroupBy = (option: OptionsProps) => option.group as string;
56
-
57
75
  const renderGroupName = ({ key, group, children }: AutocompleteRenderGroupParams) => (
58
76
  <li key={key}>
59
77
  {group ? (
@@ -65,7 +83,7 @@ const NsAutocomplete = ({
65
83
  </li>
66
84
  );
67
85
 
68
- const renderClearButton = () => {
86
+ const renderClearButton = useCallback(() => {
69
87
  const { show, onClear, text = 'Clear Search' } = clear || {};
70
88
  if (!show) {
71
89
  return null;
@@ -73,32 +91,47 @@ const NsAutocomplete = ({
73
91
  return (
74
92
  <li className="text-right p-3">
75
93
  <NsButton onClick={onClear} data-testid="autocomplete-clear-id">
76
- <NsTypography variant={'p-xs'} weight="bold">
94
+ <NsTypography variant="p-xs" weight="bold">
77
95
  {text}
78
96
  </NsTypography>
79
97
  </NsButton>
80
98
  </li>
81
99
  );
82
- };
100
+ }, [clear]);
101
+
102
+ const handleSeeMore = useCallback(() => {
103
+ const { onSeeMore } = seeMore || {};
104
+ // Remember the current scroll position
105
+ if (options?.length > 0 && listboxRef?.current) {
106
+ const lastScrollPosition = listboxRef.current?.scrollTop;
107
+ if (lastScrollPosition) {
108
+ setScrollTopPosition(lastScrollPosition);
109
+ }
110
+ }
111
+ if (onSeeMore && typeof onSeeMore === 'function') {
112
+ onSeeMore();
113
+ }
114
+ }, [seeMore, options]);
83
115
 
84
- const renderSeeMoreButton = () => {
85
- const { show, onSeeMore, text = 'See More Results' } = seeMore || {};
116
+ const renderSeeMoreButton = useCallback(() => {
117
+ const { show, text = 'See More Results' } = seeMore || {};
86
118
  if (!show) {
87
119
  return null;
88
120
  }
121
+
89
122
  return (
90
123
  <li className="text-center p-3">
91
- <NsButton onClick={onSeeMore} data-testid="autocomplete-loadmore-id">
124
+ <NsButton onClick={handleSeeMore} data-testid="autocomplete-loadmore-id">
92
125
  <NsTypography variant="p-xs" weight="bold">
93
126
  {text}
94
127
  </NsTypography>
95
128
  </NsButton>
96
129
  </li>
97
130
  );
98
- };
131
+ }, [seeMore, handleSeeMore]);
99
132
 
100
133
  const renderListBoxComponent = ({ children, ...otherProps }: ComponentBoxProps) => (
101
- <List {...otherProps}>
134
+ <List {...otherProps} ref={listboxRef}>
102
135
  {renderClearButton()}
103
136
  {children}
104
137
  {renderSeeMoreButton()}
@@ -118,11 +151,17 @@ const NsAutocomplete = ({
118
151
  renderOption={renderOption}
119
152
  renderInput={renderInput}
120
153
  ListboxComponent={renderListBoxComponent}
154
+ ListboxProps={{ scrollTopPosition } as HTMLAttributes<HTMLUListElement>} // Pass the current scroll position
121
155
  renderGroup={renderGroupName}
122
156
  onChange={handleSelect}
123
157
  onInputChange={handleInputChange}
124
158
  value={value}
125
159
  inputValue={inputValue}
160
+ filterOptions={
161
+ isFilterEnabled
162
+ ? undefined // Use default filtering behavior
163
+ : (options) => options
164
+ }
126
165
  freeSolo
127
166
  />
128
167
  );