@nuskin/foundation-ui-components 1.3.0 → 1.4.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 (38) hide show
  1. package/package.json +4 -4
  2. package/src/common/auth-dropdown/NsAuthDropdown.tsx +6 -6
  3. package/src/common/button/NsButton.stories.tsx +3 -2
  4. package/src/common/button/NsButton.tsx +27 -2
  5. package/src/common/button/types.d.ts +21 -17
  6. package/src/common/card/NsFfgCard.tsx +2 -2
  7. package/src/common/cart-icon/NsCartIcon.styled.tsx +14 -0
  8. package/src/common/dropdown/NsDropdown.tsx +6 -6
  9. package/src/common/email-simple/NsEmailSimple.tsx +1 -1
  10. package/src/common/flag/NsFlag.spec.tsx +14 -2
  11. package/src/common/flag/NsFlag.styled.tsx +2 -5
  12. package/src/common/hooks/useOnClickOutside.tsx +11 -14
  13. package/src/common/icon/NsIcon.spec.tsx +11 -2
  14. package/src/common/icon/NsIcon.stories.tsx +2 -1
  15. package/src/common/icon/NsIcon.styled.tsx +13 -10
  16. package/src/common/icon/NsIcon.tsx +9 -6
  17. package/src/common/icon/icons/UserIcon.tsx +8 -1
  18. package/src/common/icon/types.d.ts +3 -2
  19. package/src/common/progress/NsProgress.spec.tsx +2 -2
  20. package/src/common/progress/NsProgress.stories.tsx +10 -5
  21. package/src/common/progress/NsProgress.styled.tsx +30 -12
  22. package/src/common/progress/NsProgress.tsx +18 -16
  23. package/src/common/progress/types.d.ts +2 -2
  24. package/src/common/radiogroup/NsRadioGroup.spec.tsx +2 -2
  25. package/src/common/radiogroup/NsRadioGroup.stories.tsx +2 -2
  26. package/src/common/radiogroup/NsRadioGroup.tsx +5 -3
  27. package/src/common/radiogroup/types.d.ts +11 -12
  28. package/src/common/select/NsSelect.styled.tsx +37 -69
  29. package/src/common/select/NsSelect.tsx +86 -91
  30. package/src/common/select/NsSelectOption.tsx +10 -14
  31. package/src/common/select/specs/NsSelect.spec.tsx +183 -0
  32. package/src/common/select/specs/NsSelectOption.spec.tsx +32 -0
  33. package/src/common/select/stories/NsSelect.stories.tsx +25 -80
  34. package/src/common/select/stories/countries.ts +53 -0
  35. package/src/common/select/types.d.ts +21 -8
  36. package/src/common/table/NsTable.stories.tsx +1 -0
  37. package/src/common/cart-icon/NsCartIcon.styled.ts +0 -14
  38. package/src/common/select/NsSelect.spec.tsx +0 -274
@@ -0,0 +1,183 @@
1
+ import { RenderSelectOptionArgType } from '../types';
2
+ import { fireEvent, render, screen, waitFor } from '@/test-utils/index';
3
+ import React from 'react';
4
+
5
+ import { NsSelect, NsSelectOption } from '../..';
6
+
7
+ const renderSelectOption = (option: RenderSelectOptionArgType) => {
8
+ return (
9
+ <NsSelectOption
10
+ key={option.value}
11
+ value={option.value}
12
+ index={option.index ?? -1}
13
+ label={option.label}
14
+ onClick={option.onClick}
15
+ />
16
+ );
17
+ };
18
+ const createOptionTestId = (value: string) => `option-${value}`;
19
+ const selectCommonProps = {
20
+ selected: { label: 'Option 1', value: 'Option1', index: 0 },
21
+ options: [
22
+ { value: 'Option1', label: 'Option 1' },
23
+ { value: 'Option2', label: 'Option 2' },
24
+ { value: 'Option3', label: 'Option 3' },
25
+ ],
26
+ onSelect: jest.fn(),
27
+ renderOption: renderSelectOption,
28
+ };
29
+ describe('NsSelect', () => {
30
+ const onSelect = jest.fn();
31
+ const selected = { label: '', value: '' };
32
+
33
+ const options = [
34
+ { value: 'Option1', label: 'Option 1' },
35
+ { value: 'Option2', label: 'Option 2' },
36
+ { value: 'Option3', label: 'Option 3' },
37
+ ];
38
+
39
+ it('1. renders the placeholder text when no value is selected', () => {
40
+ render(
41
+ <NsSelect
42
+ placeholder="Select an option"
43
+ selected={selected}
44
+ onSelect={onSelect}
45
+ options={options}
46
+ renderOption={renderSelectOption}
47
+ />
48
+ );
49
+
50
+ expect(screen.getByText('Select an option')).toBeInTheDocument();
51
+ });
52
+
53
+ it('2. clicking outside the select container closes the dropdown', async () => {
54
+ const { getByTestId, queryByTestId, container } = render(
55
+ <div>
56
+ <button className="extra"></button>
57
+ <NsSelect {...selectCommonProps} />
58
+ </div>
59
+ );
60
+ const selectButton = getByTestId('select-head');
61
+ fireEvent.mouseDown(selectButton);
62
+ const outsideElement = container.querySelector('button.extra');
63
+ if (outsideElement) fireEvent.click(outsideElement);
64
+ // check if the dropdown is closed
65
+ expect(queryByTestId(createOptionTestId(selectCommonProps.options[1].value))).toBeNull();
66
+ });
67
+
68
+ it('3. renders the selected value label', () => {
69
+ render(<NsSelect {...selectCommonProps} />);
70
+
71
+ expect(screen.getByText(selectCommonProps.selected.label)).toBeInTheDocument();
72
+ });
73
+
74
+ it('4. clicking on select button opens the dropdown', () => {
75
+ const { getByTestId, getByText } = render(<NsSelect {...selectCommonProps} />);
76
+ const selectButton = getByTestId('select-head');
77
+ fireEvent.mouseDown(selectButton);
78
+
79
+ // Check if the dropdown is opened (isOpen state is true)
80
+ const dropdownList = getByTestId('select-menu-list');
81
+ expect(dropdownList).toBeInTheDocument();
82
+
83
+ // Check if the options are rendered in the dropdown
84
+ selectCommonProps.options.slice(1).forEach((option) => {
85
+ const optionElement = getByText(option.label);
86
+ expect(optionElement).toBeInTheDocument();
87
+ });
88
+ });
89
+
90
+ it('5. disabling the select component prevents interaction', () => {
91
+ const { getByTestId, queryByTestId } = render(<NsSelect {...selectCommonProps} disabled />);
92
+ const selectButton = getByTestId('select-head');
93
+ fireEvent.mouseDown(selectButton);
94
+
95
+ // Check if the dropdown does not open (select list is not present)
96
+ const dropdownList = queryByTestId('select-menu-list');
97
+ expect(dropdownList).toBeNull();
98
+
99
+ expect(selectCommonProps.onSelect).not.toHaveBeenCalled();
100
+ });
101
+
102
+ it('6. menu closes on mouse leave when closeOnMouseLeave is set to true', async () => {
103
+ const { getByTestId } = render(<NsSelect {...selectCommonProps} closeOnMouseLeave />);
104
+
105
+ const selectHead = getByTestId('select-head');
106
+ fireEvent.mouseDown(selectHead);
107
+ const selectList = screen.queryByTestId('select-menu-list');
108
+ const option1 = screen.queryByTestId(createOptionTestId(selectCommonProps.options[0].value));
109
+ const option2 = screen.queryByTestId(createOptionTestId(selectCommonProps.options[1].value));
110
+ const option3 = screen.queryByTestId(createOptionTestId(selectCommonProps.options[2].value));
111
+ if (selectList) fireEvent.mouseLeave(selectList);
112
+
113
+ await waitFor(() => {
114
+ expect(option1).not.toBeInTheDocument();
115
+ expect(option2).not.toBeInTheDocument();
116
+ expect(option3).not.toBeInTheDocument();
117
+ });
118
+ });
119
+
120
+ it('7. select does not open when disabled', () => {
121
+ render(<NsSelect {...selectCommonProps} disabled />);
122
+
123
+ const selectHead = screen.getByTestId('select-head');
124
+ fireEvent.mouseDown(selectHead);
125
+
126
+ expect(screen.queryByTestId('select-menu-list')).not.toBeInTheDocument();
127
+ });
128
+
129
+ it('8. opens the select on "ArrowDown" key press', () => {
130
+ render(<NsSelect {...selectCommonProps} />);
131
+
132
+ const selectHead = screen.getByTestId('select-head');
133
+ fireEvent.mouseDown(selectHead, { key: 'arrowDown' });
134
+
135
+ expect(selectHead).toBeInTheDocument();
136
+ });
137
+
138
+ it('9. selects the next option on arrow right click', () => {
139
+ const { getByTestId, container } = render(<NsSelect {...selectCommonProps} hasArrows />);
140
+
141
+ const selectHead = getByTestId('select-head');
142
+ fireEvent.mouseDown(selectHead);
143
+
144
+ const rightArrow = container.querySelector('.RightArrow');
145
+ if (rightArrow) fireEvent.click(rightArrow);
146
+
147
+ expect(selectCommonProps.onSelect).toHaveBeenCalledWith({ ...selectCommonProps.options[1], index: 1 });
148
+ });
149
+
150
+ it('10. selects the previous option on arrow left click', () => {
151
+ const { getByTestId } = render(<NsSelect {...selectCommonProps} hasArrows reverseArrowControls />);
152
+
153
+ const selectHead = getByTestId('select-head');
154
+ fireEvent.mouseDown(selectHead);
155
+ const option3 = getByTestId(createOptionTestId(selectCommonProps.options[2].value));
156
+ fireEvent.click(option3);
157
+
158
+ const leftArrow = document.getElementsByClassName('LeftArrow')[0];
159
+ fireEvent.click(leftArrow);
160
+
161
+ expect(selectCommonProps.onSelect).toHaveBeenCalledWith({ ...selectCommonProps.options[1], index: 1 });
162
+ });
163
+
164
+ it('11. renders an error message with red styles', () => {
165
+ const errorMessage = 'Invalid value';
166
+
167
+ const { getByText } = render(<NsSelect {...selectCommonProps} hasArrows errorMessage={errorMessage} />);
168
+
169
+ const errorText = getByText(errorMessage);
170
+ expect(errorText).toBeInTheDocument();
171
+ });
172
+ it('12. selecting an option calls the onSelect callback', () => {
173
+ const { getByTestId } = render(<NsSelect {...selectCommonProps} hasArrows />);
174
+
175
+ const selectHead = getByTestId('select-head');
176
+ fireEvent.mouseDown(selectHead);
177
+
178
+ const option1 = getByTestId(createOptionTestId(selectCommonProps.options[2].value));
179
+ fireEvent.click(option1);
180
+
181
+ expect(selectCommonProps.onSelect).toHaveBeenCalledWith({ ...selectCommonProps.options[2], index: 2 });
182
+ });
183
+ });
@@ -0,0 +1,32 @@
1
+ import { fireEvent, render } from '@/test-utils/index';
2
+ import React from 'react';
3
+
4
+ import { NsSelectOption } from '../';
5
+
6
+ describe('NsSelectOption', () => {
7
+ it('1. renders NsSelectOption component without errors', () => {
8
+ const value = 'option1';
9
+ const label = 'Option 1';
10
+ const onClick = jest.fn();
11
+ const { getByTestId, getByText } = render(
12
+ <NsSelectOption value={value} label={label} index={1} onClick={onClick} />
13
+ );
14
+
15
+ // Check if the option container element exists
16
+ const optionContainer = getByTestId(`option-${value}`);
17
+ expect(optionContainer).toBeInTheDocument();
18
+
19
+ // Check if the option label is rendered correctly
20
+ const optionLabel = getByText(label);
21
+ expect(optionLabel).toBeInTheDocument();
22
+ });
23
+ it('2. clicking on NsSelectOption selects the option', () => {
24
+ const value = 'option1';
25
+ const label = 'Option 1';
26
+ const onClick = jest.fn();
27
+ const { getByTestId } = render(<NsSelectOption value={value} label={label} onClick={onClick} index={1} />);
28
+ const optionElement = getByTestId(`option-${value}`);
29
+ fireEvent.click(optionElement);
30
+ expect(onClick).toHaveBeenCalled();
31
+ });
32
+ });
@@ -1,8 +1,9 @@
1
1
  import React, { useState } from 'react';
2
+
2
3
  import type { Meta, StoryObj } from '@storybook/react';
3
- import NsSelect from '../NsSelect';
4
- import NsSelectOption from '../NsSelectOption';
4
+ import { NsSelectOption, NsSelect, SelectProps } from '../';
5
5
  import { withCenterDecorator } from '@/stories/decorators/withCenterDecorator';
6
+ import { countries } from './countries';
6
7
 
7
8
  type ComponentType = typeof NsSelect;
8
9
  type Story = StoryObj<ComponentType>;
@@ -32,59 +33,7 @@ export default {
32
33
  hasArrows: { control: 'boolean' },
33
34
  },
34
35
  parameters: {
35
- options: [
36
- { label: 'Alabama', value: 'AL' },
37
- { label: 'Alaska', value: 'AK' },
38
- { label: 'Arizona', value: 'AZ' },
39
- { label: 'Arkansas', value: 'AR' },
40
- { label: 'California', value: 'CA' },
41
- { label: 'Colorado', value: 'CO' },
42
- { label: 'Connecticut', value: 'CT' },
43
- { label: 'Delaware', value: 'DE' },
44
- { label: 'District of Columbia', value: 'DC' },
45
- { label: 'Florida', value: 'FL' },
46
- { label: 'Georgia', value: 'GA' },
47
- { label: 'Hawaii', value: 'HI' },
48
- { label: 'Idaho', value: 'ID' },
49
- { label: 'Illinois', value: 'IL' },
50
- { label: 'Indiana', value: 'IN' },
51
- { label: 'Iowa', value: 'IA' },
52
- { label: 'Kansas', value: 'KA' },
53
- { label: 'Kentucky', value: 'KY' },
54
- { label: 'Louisiana', value: 'LA' },
55
- { label: 'Maine', value: 'ME' },
56
- { label: 'Maryland', value: 'MY' },
57
- { label: 'Massachusetts', value: 'MA' },
58
- { label: 'Michigan', value: 'MC' },
59
- { label: 'Minnesota', value: 'MN' },
60
- { label: 'Mississippi', value: 'MI' },
61
- { label: 'Missouri', value: 'MO' },
62
- { label: 'Montana', value: 'MT' },
63
- { label: 'Nebraska', value: 'NE' },
64
- { label: 'Nevada', value: 'NA' },
65
- { label: 'New Hampshire', value: 'NH' },
66
- { label: 'New Jersey', value: 'NJ' },
67
- { label: 'New Mexico', value: 'NM' },
68
- { label: 'New York', value: 'NY' },
69
- { label: 'North Carolina', value: 'NC' },
70
- { label: 'North Dakota', value: 'CD' },
71
- { label: 'Ohio', value: 'OH' },
72
- { label: 'Oklahoma', value: 'OK' },
73
- { label: 'Oregon', value: 'OR' },
74
- { label: 'Pennsylvania', value: 'PA' },
75
- { label: 'Rhode Island', value: 'RI' },
76
- { label: 'South Carolina', value: 'SC' },
77
- { label: 'South Dakota', value: 'SD' },
78
- { label: 'Tennessee', value: 'TN' },
79
- { label: 'Texas', value: 'TX' },
80
- { label: 'Utah', value: 'UT' },
81
- { label: 'Vermont', value: 'VT' },
82
- { label: 'Virginia', value: 'VA' },
83
- { label: 'Washington', value: 'WA' },
84
- { label: 'West Virginia', value: 'WV' },
85
- { label: 'Wisconsin', value: 'WN' },
86
- { label: 'Wyoming', value: 'WY' },
87
- ],
36
+ options: countries,
88
37
  },
89
38
  args: {
90
39
  type: 'outlined',
@@ -92,15 +41,12 @@ export default {
92
41
  selectBorderColor: 'primary',
93
42
  closeOnMouseLeave: false,
94
43
  showListDivider: true,
95
- selectedValue: '',
96
- selectedLabel: '',
44
+ selected: { label: '', value: '' },
97
45
  label: 'State',
98
46
  placeholder: '',
99
47
  name: '',
100
48
  menuProps: 'auto',
101
49
  reverseArrowControls: false,
102
- itemText: 'Text',
103
- itemValue: 'Value',
104
50
  validateOnBlur: false,
105
51
  autoComplete: '',
106
52
  rules: [],
@@ -108,36 +54,35 @@ export default {
108
54
  },
109
55
  } as MetaStory;
110
56
 
111
- const AppSelect = (props: any) => {
112
- const [selectedLabel, setSelectedLabel] = useState(props.selectedLabel);
113
- const [selectedValue, setSelectedValue] = useState(props.selectedValue);
114
- const handleSelect = (value: string) => {
115
- setSelectedValue(value);
116
- props.options.find((option: any) => {
117
- if (option.value === value) {
118
- setSelectedLabel(option.label);
119
- }
120
- });
121
- console.log('Selected:', value);
122
- };
57
+ const AppSelect = (props: SelectProps) => {
58
+ const { selected } = props;
123
59
 
60
+ const [data, setData] = useState(selected);
61
+ const onSelect = (x: any) => {
62
+ setData(x);
63
+ };
124
64
  return (
125
65
  <NsSelect
126
66
  {...props}
127
- selectedValue={selectedValue}
128
- selectedLabel={selectedLabel}
129
- onSelect={handleSelect}
67
+ selected={data}
68
+ onSelect={onSelect}
130
69
  label={props.label}
131
- >
132
- {props.options.map((option: any) => (
133
- <NsSelectOption key={option.value} value={option.value} label={option.label} />
134
- ))}
135
- </NsSelect>
70
+ options={props.options}
71
+ renderOption={(props) => {
72
+ return <NsSelectOption {...props} key={props?.value} onClick={props.onClick} />;
73
+ }}
74
+ />
136
75
  );
137
76
  };
138
77
  export const Select: Story = {
139
78
  render: (args, context) => {
140
79
  const options = context.parameters.options;
141
- return <AppSelect options={options} {...args} />;
80
+ return <AppSelect {...args} options={options} />;
81
+ },
82
+ };
83
+ export const SelectWithArrows: Story = {
84
+ render: (args, context) => {
85
+ const options = context.parameters.options;
86
+ return <AppSelect {...args} options={options} hasArrows />;
142
87
  },
143
88
  };
@@ -0,0 +1,53 @@
1
+ export const countries = [
2
+ { label: 'Alabama', value: 'AL' },
3
+ { label: 'Alaska', value: 'AK' },
4
+ { label: 'Arizona', value: 'AZ' },
5
+ { label: 'Arkansas', value: 'AR' },
6
+ { label: 'California', value: 'CA' },
7
+ { label: 'Colorado', value: 'CO' },
8
+ { label: 'Connecticut', value: 'CT' },
9
+ { label: 'Delaware', value: 'DE' },
10
+ { label: 'District of Columbia', value: 'DC' },
11
+ { label: 'Florida', value: 'FL' },
12
+ { label: 'Georgia', value: 'GA' },
13
+ { label: 'Hawaii', value: 'HI' },
14
+ { label: 'Idaho', value: 'ID' },
15
+ { label: 'Illinois', value: 'IL' },
16
+ { label: 'Indiana', value: 'IN' },
17
+ { label: 'Iowa', value: 'IA' },
18
+ { label: 'Kansas', value: 'KA' },
19
+ { label: 'Kentucky', value: 'KY' },
20
+ { label: 'Louisiana', value: 'LA' },
21
+ { label: 'Maine', value: 'ME' },
22
+ { label: 'Maryland', value: 'MY' },
23
+ { label: 'Massachusetts', value: 'MA' },
24
+ { label: 'Michigan', value: 'MC' },
25
+ { label: 'Minnesota', value: 'MN' },
26
+ { label: 'Mississippi', value: 'MI' },
27
+ { label: 'Missouri', value: 'MO' },
28
+ { label: 'Montana', value: 'MT' },
29
+ { label: 'Nebraska', value: 'NE' },
30
+ { label: 'Nevada', value: 'NA' },
31
+ { label: 'New Hampshire', value: 'NH' },
32
+ { label: 'New Jersey', value: 'NJ' },
33
+ { label: 'New Mexico', value: 'NM' },
34
+ { label: 'New York', value: 'NY' },
35
+ { label: 'North Carolina', value: 'NC' },
36
+ { label: 'North Dakota', value: 'CD' },
37
+ { label: 'Ohio', value: 'OH' },
38
+ { label: 'Oklahoma', value: 'OK' },
39
+ { label: 'Oregon', value: 'OR' },
40
+ { label: 'Pennsylvania', value: 'PA' },
41
+ { label: 'Rhode Island', value: 'RI' },
42
+ { label: 'South Carolina', value: 'SC' },
43
+ { label: 'South Dakota', value: 'SD' },
44
+ { label: 'Tennessee', value: 'TN' },
45
+ { label: 'Texas', value: 'TX' },
46
+ { label: 'Utah', value: 'UT' },
47
+ { label: 'Vermont', value: 'VT' },
48
+ { label: 'Virginia', value: 'VA' },
49
+ { label: 'Washington', value: 'WA' },
50
+ { label: 'West Virginia', value: 'WV' },
51
+ { label: 'Wisconsin', value: 'WN' },
52
+ { label: 'Wyoming', value: 'WY' },
53
+ ];
@@ -1,10 +1,9 @@
1
- import { ReactNode } from 'react';
2
1
  import { FoundationTheme } from '@nuskin/foundation-theme';
2
+ import { ReactNode } from 'react';
3
3
 
4
4
  export interface SelectProps {
5
- selectedValue: string;
6
- selectedLabel: string;
7
- onSelect: (arg: string) => void; // eslint-disable-line
5
+ selected: LabelValueType;
6
+ onSelect(arg: LabelValueType): void; // eslint-disable-line
8
7
  children?: ReactNode | null;
9
8
  disabled?: boolean;
10
9
  type?: 'outlined' | 'unfilled';
@@ -18,19 +17,33 @@ export interface SelectProps {
18
17
  errorMessage?: string;
19
18
  menuProps?: string | object;
20
19
  reverseArrowControls?: boolean;
21
- itemText?: string;
22
- itemValue?: string;
23
20
  validateOnBlur?: boolean;
24
21
  autoComplete?: string;
25
22
  // eslint-disable-next-line no-undef
26
23
  rules?: Array;
27
24
  hasArrows?: boolean;
25
+ options: LabelValueType[];
26
+ renderOption(x: any): any; // eslint-disable-line
28
27
  }
29
28
 
30
29
  export interface SelectOptionProps {
31
30
  value: string;
32
31
  label: string;
33
- selectedValue?: string;
34
- onSelect?: (arg: string) => void; // eslint-disable-line
32
+ selected?: LabelValueType;
33
+ index: number;
34
+ onClick: () => void;
35
35
  showListDivider?: boolean;
36
36
  }
37
+
38
+ export type LabelValueType = {
39
+ label: string;
40
+ value: string;
41
+ id?: string;
42
+ index?: number;
43
+ };
44
+
45
+ export type RenderSelectOptionArgType = LabelValueType & {
46
+ onClick(): void;
47
+ showListDivider: boolean;
48
+ selected: LabelValueType;
49
+ };
@@ -1,4 +1,5 @@
1
1
  import React from 'react'
2
+
2
3
  import type { Meta, StoryObj } from '@storybook/react';
3
4
  import NsTable from './NsTable';
4
5
  import { withCenterDecorator } from '../../stories/decorators/withCenterDecorator';
@@ -1,14 +0,0 @@
1
- import { Badge, Theme } from '@mui/material';
2
- import { styled, FoundationTheme } from '@nuskin/foundation-theme';
3
-
4
- type ItemCountParams = {
5
- theme?: Theme;
6
- bgcolor: FoundationTheme.PaletteColors;
7
- };
8
-
9
- export const StyledBadge = styled(Badge) <ItemCountParams>`
10
- & .MuiBadge-badge {
11
- background-color: ${({ bgcolor, theme }) => theme?.palette?.[bgcolor]?.main};
12
- color: ${({ bgcolor, theme }) => theme?.palette?.[bgcolor]?.contrastText};
13
- }
14
- `;