Skip to content

Commit 108d8e2

Browse files
author
Enrique Moreno
committed
Merge branch 'master' of github.com:dxc-technology/halstack-react into Mil4n0r/code_quality_monorepo
2 parents 278941c + d004266 commit 108d8e2

File tree

12 files changed

+103
-147
lines changed

12 files changed

+103
-147
lines changed

apps/website/.eslintrc.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"argsIgnorePattern": "^_"
1212
}
1313
],
14-
"curly": ["warn", "all"],
1514
// TODO: Remove rules
1615
"react/no-unescaped-entities": "off",
1716
"import/no-anonymous-default-export": "off",

packages/eslint-config/library.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ module.exports = {
6565
},
6666
],
6767
rules: {
68-
curly: ["warn", "all"],
6968
"no-use-before-define": [
7069
"error",
7170
{

packages/lib/src/BackgroundColorContext.tsx

Lines changed: 0 additions & 28 deletions
This file was deleted.

packages/lib/src/select/Listbox.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import styled from "styled-components";
33
import DxcIcon from "../icon/Icon";
44
import { HalstackLanguageContext } from "../HalstackContext";
55
import ListOption from "./ListOption";
6-
import { groupsHaveOptions } from "./selectUtils";
6+
import { groupsHaveOptions } from "./utils";
77
import { ListboxProps, ListOptionGroupType, ListOptionType } from "./types";
88

99
const Listbox = ({
@@ -24,12 +24,9 @@ const Listbox = ({
2424

2525
let globalIndex = optional && !multiple ? 0 : -1;
2626

27-
const isListOptionGroupType = (option: ListOptionType | ListOptionGroupType): option is ListOptionGroupType =>
28-
"options" in option && Array.isArray(option.options);
29-
3027
const mapOptionFunc = (option: ListOptionType | ListOptionGroupType, mapIndex: number) => {
3128
const groupId = `${id}-group-${mapIndex}`;
32-
if (isListOptionGroupType(option)) {
29+
if ("options" in option) {
3330
return (
3431
option.options.length > 0 && (
3532
<li key={groupId}>

packages/lib/src/select/Select.tsx

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
groupsHaveOptions,
3030
isArrayOfOptionGroups,
3131
notOptionalCheck,
32-
} from "./selectUtils";
32+
} from "./utils";
3333
import SelectPropsType, { ListOptionType, RefType } from "./types";
3434

3535
const DxcSelect = forwardRef<RefType, SelectPropsType>(
@@ -94,28 +94,34 @@ const DxcSelect = forwardRef<RefType, SelectPropsType>(
9494
}
9595
};
9696

97-
const handleSelectChangeValue = (newOption: ListOptionType | undefined) => {
98-
if (newOption) {
99-
const currentValue = value ?? innerValue;
100-
// TODO: Fix types
101-
const newValue = multiple
102-
? (currentValue as string[]).includes(newOption.value)
103-
? (currentValue as string[]).filter((optionVal: string) => optionVal !== newOption.value)
104-
: [...(currentValue as string[]), newOption.value]
105-
: newOption.value;
106-
107-
if (value == null) {
108-
setInnerValue(newValue);
97+
const handleSelectChangeValue = useCallback(
98+
(newOption: ListOptionType | undefined) => {
99+
if (newOption) {
100+
let newValue: string | string[];
101+
102+
if (multiple) {
103+
const currentValue = (value ?? innerValue) as string[];
104+
newValue = currentValue.includes(newOption.value)
105+
? currentValue.filter((optionVal) => optionVal !== newOption.value)
106+
: [...currentValue, newOption.value];
107+
} else {
108+
newValue = newOption.value;
109+
}
110+
111+
if (value == null) {
112+
setInnerValue(newValue);
113+
}
114+
onChange?.({
115+
value: newValue as string & string[],
116+
error: notOptionalCheck(newValue, multiple, optional)
117+
? translatedLabels.formFields.requiredValueErrorMessage
118+
: undefined,
119+
});
109120
}
110-
// TODO: Fix types
111-
onChange?.({
112-
value: newValue as string & string[],
113-
...(notOptionalCheck(newValue, multiple, optional) && {
114-
error: translatedLabels.formFields.requiredValueErrorMessage,
115-
}),
116-
});
117-
}
118-
};
121+
},
122+
[multiple, value, innerValue, onChange, optional, translatedLabels]
123+
);
124+
119125
const handleSelectOnClick = () => {
120126
if (searchable) {
121127
selectSearchInputRef?.current?.focus();

packages/lib/src/switch/Switch.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const DxcSwitch = forwardRef<RefType, SwitchPropsType>(
3636
case "Enter":
3737
case " ":
3838
event.preventDefault();
39-
refTrack?.current?.focus();
39+
refTrack.current?.focus();
4040
setInnerChecked(!(checked ?? innerChecked));
4141
onChange?.(!(checked ?? innerChecked));
4242
break;

packages/lib/src/tabs/Tab.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ const DxcTab = forwardRef(
4242

4343
useEffect(() => {
4444
if (activeLabel === label) {
45-
setActiveIndicatorWidth?.(tabRef?.current?.offsetWidth ?? 0);
46-
setActiveIndicatorLeft?.(tabRef?.current?.offsetLeft ?? 0);
45+
setActiveIndicatorWidth?.(tabRef.current?.offsetWidth ?? 0);
46+
setActiveIndicatorLeft?.(tabRef.current?.offsetLeft ?? 0);
4747
}
48-
}, [activeLabel, label]);
48+
}, [activeLabel, label, setActiveIndicatorWidth, setActiveIndicatorLeft]);
4949

5050
useEffect(() => {
5151
if (active) {
5252
setActiveLabel?.(label);
5353
}
54-
}, [active, label]);
54+
}, [active, label, setActiveLabel]);
5555

5656
const handleOnKeyDown = (event: KeyboardEvent<HTMLButtonElement>) => {
5757
switch (event.key) {

packages/lib/src/tabs/Tabs.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ const DxcTabs = ({
134134
}, [iconPosition, tabIndex, innerFocusIndex, activeTabLabel, childrenArray, hasLabelAndIcon]);
135135

136136
const scrollLeft = () => {
137-
const scrollWidth = (refTabList?.current?.offsetHeight || 0) * 0.75;
137+
const scrollWidth = (refTabList?.current?.offsetHeight ?? 0) * 0.75;
138138
let moveX = 0;
139139
if (countClick <= scrollWidth) {
140140
moveX = 0;
@@ -150,10 +150,11 @@ const DxcTabs = ({
150150
};
151151

152152
const scrollRight = () => {
153-
const scrollWidth = (refTabList?.current?.offsetHeight || 0) * 0.75;
153+
const offsetHeight = refTabList?.current?.offsetHeight ?? 0;
154+
const scrollWidth = offsetHeight * 0.75;
154155
let moveX = 0;
155-
if (countClick + scrollWidth + (refTabList?.current?.offsetHeight || 0) >= totalTabsWidth) {
156-
moveX = totalTabsWidth - (refTabList?.current?.offsetHeight || 0);
156+
if (countClick + scrollWidth + offsetHeight >= totalTabsWidth) {
157+
moveX = totalTabsWidth - offsetHeight;
157158
setScrollRightEnabled(false);
158159
setScrollLeftEnabled(true);
159160
} else {
@@ -257,8 +258,6 @@ const DxcTabs = ({
257258
);
258259
};
259260

260-
DxcTabs.Tab = DxcTab;
261-
262261
const Underline = styled.div`
263262
position: absolute;
264263
left: 0;
@@ -383,4 +382,5 @@ const TabsContentScroll = styled.div<{
383382
transition: all 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
384383
`;
385384

385+
DxcTabs.Tab = DxcTab;
386386
export default DxcTabs;

packages/lib/src/tabs/TabsLegacy.tsx

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const useResize = (refTabList: MutableRefObject<HTMLDivElement | null>) => {
1010
const [viewWidth, setViewWidth] = useState(0);
1111

1212
const handleWindowSizeChange = useCallback(() => {
13-
setViewWidth(refTabList?.current?.offsetWidth ?? 0);
13+
setViewWidth(refTabList.current?.offsetWidth ?? 0);
1414
}, [refTabList]);
1515

1616
useEffect(() => {
@@ -60,21 +60,21 @@ const DxcTabs = ({
6060

6161
useEffect(() => {
6262
if (activeTabIndex != null || innerActiveTabIndex != null) {
63-
const sumWidth = refTabs?.current?.reduce((count, obj) => count + obj.offsetWidth, 0);
63+
const sumWidth = refTabs.current?.reduce((count, obj) => count + obj.offsetWidth, 0);
6464
setTotalTabsWidth(sumWidth);
65-
setActiveIndicatorWidth(refTabs?.current[activeTabIndex ?? innerActiveTabIndex!]?.offsetWidth ?? 0);
66-
setActiveIndicatorLeft(refTabs?.current[activeTabIndex ?? innerActiveTabIndex!]?.offsetLeft ?? 0);
65+
setActiveIndicatorWidth(refTabs.current[activeTabIndex ?? innerActiveTabIndex!]?.offsetWidth ?? 0);
66+
setActiveIndicatorLeft(refTabs.current[activeTabIndex ?? innerActiveTabIndex!]?.offsetLeft ?? 0);
6767
}
68-
}, [refTabs]);
68+
}, [activeTabIndex, innerActiveTabIndex]);
6969

7070
useEffect(() => {
71-
setMinHeightTabs((refTabList?.current?.offsetHeight || 0) + 1);
72-
}, [refTabList]);
71+
setMinHeightTabs((refTabList.current?.offsetHeight ?? 0) + 1);
72+
}, []);
7373

7474
useEffect(() => {
7575
if (activeTabIndex && activeTabIndex >= 0) {
76-
setActiveIndicatorWidth(refTabs?.current[activeTabIndex]?.offsetWidth ?? 0);
77-
setActiveIndicatorLeft(refTabs?.current[activeTabIndex]?.offsetLeft ?? 0);
76+
setActiveIndicatorWidth(refTabs.current[activeTabIndex]?.offsetWidth ?? 0);
77+
setActiveIndicatorLeft(refTabs.current[activeTabIndex]?.offsetLeft ?? 0);
7878
}
7979
}, [activeTabIndex]);
8080

@@ -84,13 +84,13 @@ const DxcTabs = ({
8484
}
8585
onTabClick?.(newValue);
8686
if (activeTabIndex === undefined) {
87-
setActiveIndicatorWidth(refTabs?.current[newValue]?.offsetWidth ?? 0);
88-
setActiveIndicatorLeft(refTabs?.current[newValue]?.offsetLeft ?? 0);
87+
setActiveIndicatorWidth(refTabs.current[newValue]?.offsetWidth ?? 0);
88+
setActiveIndicatorLeft(refTabs.current[newValue]?.offsetLeft ?? 0);
8989
}
9090
};
9191

9292
const scrollLeft = () => {
93-
const scrollWidth = (refTabList?.current?.offsetHeight || 0) * 0.75;
93+
const scrollWidth = (refTabList?.current?.offsetHeight ?? 0) * 0.75;
9494
let moveX = 0;
9595
if (countClick <= scrollWidth) {
9696
moveX = 0;
@@ -106,10 +106,11 @@ const DxcTabs = ({
106106
};
107107

108108
const scrollRight = () => {
109-
const scrollWidth = (refTabList?.current?.offsetHeight || 0) * 0.75;
109+
const offsetHeight = refTabList?.current?.offsetHeight ?? 0;
110+
const scrollWidth = offsetHeight * 0.75;
110111
let moveX = 0;
111-
if (countClick + scrollWidth + (refTabList?.current?.offsetHeight || 0) >= totalTabsWidth) {
112-
moveX = totalTabsWidth - (refTabList?.current?.offsetHeight || 0);
112+
if (countClick + scrollWidth + offsetHeight >= totalTabsWidth) {
113+
moveX = totalTabsWidth - offsetHeight;
113114
setScrollRightEnabled(false);
114115
setScrollLeftEnabled(true);
115116
} else {
@@ -129,7 +130,7 @@ const DxcTabs = ({
129130
while (tabs[index]?.isDisabled) {
130131
index = index === 0 ? tabs.length - 1 : index - 1;
131132
}
132-
refTabs?.current[index]?.focus({ preventScroll: true });
133+
refTabs.current[index]?.focus({ preventScroll: true });
133134
setScrollFocus(index);
134135
return index;
135136
}
@@ -146,7 +147,7 @@ const DxcTabs = ({
146147
while (tabs[index]?.isDisabled) {
147148
index = index === tabs.length - 1 ? 0 : index + 1;
148149
}
149-
refTabs?.current[index]?.focus({ preventScroll: true });
150+
refTabs.current[index]?.focus({ preventScroll: true });
150151
setScrollFocus(index);
151152
return index;
152153
}
@@ -158,7 +159,7 @@ const DxcTabs = ({
158159
const setScrollFocus = (actualIndex: number) => {
159160
if (tabs) {
160161
let sumPrev = 0;
161-
refTabs?.current?.forEach((item, index) => {
162+
refTabs.current?.forEach((item, index) => {
162163
if (index <= actualIndex) {
163164
sumPrev += item.offsetWidth;
164165
}
@@ -207,7 +208,7 @@ const DxcTabs = ({
207208
if (temporalFocusIndex !== currentFocusIndex) {
208209
event.preventDefault();
209210
setTemporalFocusIndex(currentFocusIndex);
210-
refTabs?.current[currentFocusIndex]?.focus();
211+
refTabs.current[currentFocusIndex]?.focus();
211212
}
212213
handleSelected(currentFocusIndex);
213214
}

0 commit comments

Comments
 (0)