Skip to content

Commit 50acff4

Browse files
authored
Merge pull request #1974 from dxc-technology/jialecl/inputs-size-fix
Fixed some weird size behaviors in some input components
2 parents 4958611 + b62092b commit 50acff4

File tree

8 files changed

+66
-26
lines changed

8 files changed

+66
-26
lines changed

lib/src/number-input/NumberInput.stories.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from "react";
22
import Title from "../../.storybook/components/Title";
33
import ExampleContainer from "../../.storybook/components/ExampleContainer";
44
import DxcNumberInput from "./NumberInput";
5+
import DxcFlex from "../flex/Flex";
56

67
export default {
78
title: "Number Input",
@@ -113,5 +114,13 @@ export const Chromatic = () => (
113114
<Title title="FillParent size" theme="light" level={4} />
114115
<DxcNumberInput label="FillParent" size="fillParent" />
115116
</ExampleContainer>
117+
<ExampleContainer>
118+
<Title title="Different sizes inside a flex" theme="light" level={4} />
119+
<DxcFlex justifyContent="space-between" gap="1rem">
120+
<DxcNumberInput label="fillParent" size="fillParent" />
121+
<DxcNumberInput label="medium" size="medium" />
122+
<DxcNumberInput label="large" size="large" />
123+
</DxcFlex>
124+
</ExampleContainer>
116125
</>
117126
);

lib/src/number-input/NumberInput.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const DxcNumberInput = React.forwardRef<RefType, NumberInputPropsType>(
2929
size,
3030
tabIndex,
3131
},
32-
ref
32+
ref,
3333
) => {
3434
const numberInputRef = React.useRef<HTMLInputElement>(null);
3535

@@ -47,7 +47,7 @@ const DxcNumberInput = React.forwardRef<RefType, NumberInputPropsType>(
4747

4848
return (
4949
<NumberInputContext.Provider value={{ typeNumber: "number", minNumber: min, maxNumber: max, stepNumber: step }}>
50-
<NumberInputContainer ref={numberInputRef}>
50+
<NumberInputContainer ref={numberInputRef} size={size}>
5151
<DxcTextInput
5252
label={label}
5353
name={name}
@@ -72,10 +72,11 @@ const DxcNumberInput = React.forwardRef<RefType, NumberInputPropsType>(
7272
</NumberInputContainer>
7373
</NumberInputContext.Provider>
7474
);
75-
}
75+
},
7676
);
7777

78-
const NumberInputContainer = styled.div`
78+
const NumberInputContainer = styled.div<{ size: NumberInputPropsType["size"] }>`
79+
${(props) => props.size == "fillParent" && "width: 100%;"}
7980
// Chrome, Safari, Edge, Opera
8081
input::-webkit-outer-spin-button,
8182
input::-webkit-inner-spin-button {

lib/src/password-input/PasswordInput.stories.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { userEvent, within } from "@storybook/test";
33
import DxcPasswordInput from "./PasswordInput";
44
import Title from "../../.storybook/components/Title";
55
import ExampleContainer from "../../.storybook/components/ExampleContainer";
6+
import DxcFlex from "../flex/Flex";
67

78
export default {
89
title: "Password Input",
@@ -81,6 +82,14 @@ export const Chromatic = () => (
8182
<Title title="FillParent size" theme="light" level={4} />
8283
<DxcPasswordInput label="FillParent" size="fillParent" />
8384
</ExampleContainer>
85+
<ExampleContainer>
86+
<Title title="Without label" theme="light" level={4} />
87+
<DxcFlex justifyContent="space-between" gap="1rem">
88+
<DxcPasswordInput label="fillParent" size="fillParent" />
89+
<DxcPasswordInput label="medium" size="medium" />
90+
<DxcPasswordInput label="large" size="large" />
91+
</DxcFlex>
92+
</ExampleContainer>
8493
</>
8594
);
8695

lib/src/password-input/PasswordInput.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const DxcPasswordInput = React.forwardRef<RefType, PasswordInputPropsType>(
3333
size = "medium",
3434
tabIndex = 0,
3535
},
36-
ref
36+
ref,
3737
) => {
3838
const [isPasswordVisible, setIsPasswordVisible] = useState(false);
3939
const inputRef = useRef<HTMLDivElement>(null);
@@ -52,7 +52,7 @@ const DxcPasswordInput = React.forwardRef<RefType, PasswordInputPropsType>(
5252
}, [isPasswordVisible, passwordInput]);
5353

5454
return (
55-
<PasswordInput ref={ref} role="group">
55+
<PasswordInput ref={ref} role="group" size={size}>
5656
<DxcTextInput
5757
label={label}
5858
name={name}
@@ -62,7 +62,7 @@ const DxcPasswordInput = React.forwardRef<RefType, PasswordInputPropsType>(
6262
onClick: () => {
6363
setIsPasswordVisible((isPasswordVisible) => !isPasswordVisible);
6464
},
65-
icon: isPasswordVisible ? 'Visibility_Off' : 'Visibility',
65+
icon: isPasswordVisible ? "Visibility_Off" : "Visibility",
6666
title: isPasswordVisible ? passwordInput.inputHidePasswordTitle : passwordInput.inputShowPasswordTitle,
6767
}}
6868
error={error}
@@ -80,10 +80,11 @@ const DxcPasswordInput = React.forwardRef<RefType, PasswordInputPropsType>(
8080
/>
8181
</PasswordInput>
8282
);
83-
}
83+
},
8484
);
8585

86-
const PasswordInput = styled.div`
86+
const PasswordInput = styled.div<{ size: PasswordInputPropsType["size"] }>`
87+
${(props) => props.size == "fillParent" && "width: 100%;"}
8788
& ::-ms-reveal {
8889
display: none;
8990
}

lib/src/select/Select.stories.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import useTheme from "../useTheme";
99
import { HalstackProvider } from "../HalstackContext";
1010
import { disabledRules } from "../../test/accessibility/rules/specific/select/disabledRules";
1111
import preview from "../../.storybook/preview";
12+
import DxcFlex from "../flex/Flex";
1213

1314
export default {
1415
title: "Select",
@@ -305,6 +306,14 @@ const Select = () => (
305306
<Title title="Fillparent size" theme="light" level={4} />
306307
<DxcSelect label="Fillparent" options={single_options} size="fillParent" />
307308
</ExampleContainer>
309+
<ExampleContainer>
310+
<Title title="Different sizes inside a flex" theme="light" level={4} />
311+
<DxcFlex justifyContent="space-between" gap="1rem">
312+
<DxcSelect label="fillParent" size="fillParent" options={single_options} />
313+
<DxcSelect label="medium" size="medium" options={single_options} />
314+
<DxcSelect label="large" size="large" options={single_options} />
315+
</DxcFlex>
316+
</ExampleContainer>
308317
<Title title="Margins" theme="light" level={2} />
309318
<ExampleContainer>
310319
<Title title="xxsmall margin" theme="light" level={4} />

lib/src/select/Select.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ const canOpenOptions = (options: Option[] | OptionGroup[], disabled: boolean) =>
2525

2626
const filterOptionsBySearchValue = (
2727
options: Option[] | OptionGroup[],
28-
searchValue: string
28+
searchValue: string,
2929
): Option[] | OptionGroup[] => {
3030
if (options?.length > 0) {
3131
if (isArrayOfOptionGroups(options))
3232
return options.map((optionGroup) => {
3333
const group = {
3434
label: optionGroup.label,
3535
options: optionGroup.options.filter((option) =>
36-
option.label.toUpperCase().includes(searchValue.toUpperCase())
36+
option.label.toUpperCase().includes(searchValue.toUpperCase()),
3737
),
3838
};
3939
return group;
@@ -47,7 +47,7 @@ const getLastOptionIndex = (
4747
filteredOptions: Option[] | OptionGroup[],
4848
searchable: boolean,
4949
optional: boolean,
50-
multiple: boolean
50+
multiple: boolean,
5151
) => {
5252
let last = 0;
5353
const reducer = (acc: number, current: OptionGroup) => acc + current.options?.length;
@@ -67,7 +67,7 @@ const getSelectedOption = (
6767
options: Option[] | OptionGroup[],
6868
multiple: boolean,
6969
optional: boolean,
70-
optionalItem: Option
70+
optionalItem: Option,
7171
) => {
7272
let selectedOption: Option | Option[] = multiple ? [] : ({} as Option);
7373
let singleSelectionIndex: number;
@@ -164,7 +164,7 @@ const DxcSelect = React.forwardRef<RefType, SelectPropsType>(
164164
size = "medium",
165165
tabIndex = 0,
166166
},
167-
ref
167+
ref,
168168
): JSX.Element => {
169169
const selectId = `select-${useId()}`;
170170
const selectLabelId = `label-${selectId}`;
@@ -188,11 +188,11 @@ const DxcSelect = React.forwardRef<RefType, SelectPropsType>(
188188
const filteredOptions = useMemo(() => filterOptionsBySearchValue(options, searchValue), [options, searchValue]);
189189
const lastOptionIndex = useMemo(
190190
() => getLastOptionIndex(options, filteredOptions, searchable, optional, multiple),
191-
[options, filteredOptions, searchable, optional, multiple]
191+
[options, filteredOptions, searchable, optional, multiple],
192192
);
193193
const { selectedOption, singleSelectionIndex } = useMemo(
194194
() => getSelectedOption(value ?? innerValue, options, multiple, optional, optionalItem),
195-
[value, innerValue, options, multiple, optional, optionalItem]
195+
[value, innerValue, options, multiple, optional, optionalItem],
196196
);
197197

198198
const openOptions = () => {
@@ -275,7 +275,7 @@ const DxcSelect = React.forwardRef<RefType, SelectPropsType>(
275275
(!isOpen || (visualFocusIndex === -1 && singleSelectionIndex > -1 && singleSelectionIndex <= lastOptionIndex))
276276
? changeVisualFocusIndex(singleSelectionIndex)
277277
: changeVisualFocusIndex((visualFocusIndex) =>
278-
visualFocusIndex === 0 || visualFocusIndex === -1 ? lastOptionIndex : visualFocusIndex - 1
278+
visualFocusIndex === 0 || visualFocusIndex === -1 ? lastOptionIndex : visualFocusIndex - 1,
279279
);
280280
openOptions();
281281
break;
@@ -350,7 +350,7 @@ const DxcSelect = React.forwardRef<RefType, SelectPropsType>(
350350
!multiple && closeOptions();
351351
setSearchValue("");
352352
},
353-
[handleSelectChangeValue, closeOptions, multiple]
353+
[handleSelectChangeValue, closeOptions, multiple],
354354
);
355355

356356
useEffect(() => {
@@ -521,7 +521,7 @@ const DxcSelect = React.forwardRef<RefType, SelectPropsType>(
521521
</SelectContainer>
522522
</ThemeProvider>
523523
);
524-
}
524+
},
525525
);
526526

527527
const sizes = {
@@ -542,6 +542,7 @@ const SelectContainer = styled.div<{ margin: SelectPropsType["margin"]; size: Se
542542
box-sizing: border-box;
543543
544544
width: ${(props) => calculateWidth(props.margin, props.size)};
545+
${(props) => props.size !== "fillParent" && "min-width:" + calculateWidth(props.margin, props.size)};
545546
margin: ${(props) => (props.margin && typeof props.margin !== "object" ? spaces[props.margin] : "0px")};
546547
margin-top: ${(props) =>
547548
props.margin && typeof props.margin === "object" && props.margin.top ? spaces[props.margin.top] : ""};

lib/src/text-input/TextInput.stories.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Suggestions from "./Suggestions";
77
import { ThemeProvider } from "styled-components";
88
import useTheme from "../useTheme";
99
import { HalstackProvider } from "../HalstackContext";
10+
import DxcFlex from "../flex/Flex";
1011

1112
export default {
1213
title: "Text Input",
@@ -268,6 +269,14 @@ export const Chromatic = () => (
268269
<Title title="FillParent size" theme="light" level={4} />
269270
<DxcTextInput label="FillParent" size="fillParent" />
270271
</ExampleContainer>
272+
<ExampleContainer>
273+
<Title title="Different sizes inside a flex" theme="light" level={4} />
274+
<DxcFlex justifyContent="space-between" gap="1.5rem">
275+
<DxcTextInput label="Text input" size="fillParent" />
276+
<DxcTextInput label="Text input" size="medium" />
277+
<DxcTextInput label="Text input" size="large" />
278+
</DxcFlex>
279+
</ExampleContainer>
271280
<Title title="Opinionated theme" theme="light" level={2} />
272281
<ExampleContainer>
273282
<HalstackProvider theme={opinionatedTheme}>

lib/src/text-input/TextInput.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const makeCancelable = (promise) => {
3333
const wrappedPromise = new Promise<string[]>((resolve, reject) => {
3434
promise.then(
3535
(val) => (hasCanceled_ ? reject({ isCanceled: true }) : resolve(val)),
36-
(promiseError) => (hasCanceled_ ? reject({ isCanceled: true }) : reject(promiseError))
36+
(promiseError) => (hasCanceled_ ? reject({ isCanceled: true }) : reject(promiseError)),
3737
);
3838
});
3939
return {
@@ -106,7 +106,7 @@ const DxcTextInput = React.forwardRef<RefType, TextInputPropsType>(
106106
size = "medium",
107107
tabIndex = 0,
108108
},
109-
ref
109+
ref,
110110
): JSX.Element => {
111111
const inputId = `input-${useId()}`;
112112
const autosuggestId = `suggestions-${inputId}`;
@@ -337,7 +337,7 @@ const DxcTextInput = React.forwardRef<RefType, TextInputPropsType>(
337337
};
338338
} else if (suggestions?.length > 0) {
339339
changeFilteredSuggestions(
340-
suggestions.filter((suggestion) => suggestion.toUpperCase().startsWith((value ?? innerValue).toUpperCase()))
340+
suggestions.filter((suggestion) => suggestion.toUpperCase().startsWith((value ?? innerValue).toUpperCase())),
341341
);
342342
changeVisualFocusIndex(-1);
343343
}
@@ -347,7 +347,7 @@ const DxcTextInput = React.forwardRef<RefType, TextInputPropsType>(
347347
numberInputContext.typeNumber,
348348
numberInputContext.minNumber,
349349
numberInputContext.maxNumber,
350-
numberInputContext.stepNumber
350+
numberInputContext.stepNumber,
351351
);
352352
}, [value, innerValue, suggestions, numberInputContext]);
353353

@@ -506,14 +506,15 @@ const DxcTextInput = React.forwardRef<RefType, TextInputPropsType>(
506506
</TextInputContainer>
507507
</ThemeProvider>
508508
);
509-
}
509+
},
510510
);
511511

512512
const TextInputContainer = styled.div<{ margin: TextInputPropsType["margin"]; size: TextInputPropsType["size"] }>`
513513
box-sizing: border-box;
514514
display: flex;
515515
flex-direction: column;
516516
width: ${(props) => calculateWidth(props.margin, props.size)};
517+
${(props) => props.size !== "fillParent" && "min-width:" + calculateWidth(props.margin, props.size)};
517518
margin: ${(props) => (props.margin && typeof props.margin !== "object" ? spaces[props.margin] : "0px")};
518519
margin-top: ${(props) =>
519520
props.margin && typeof props.margin === "object" && props.margin.top ? spaces[props.margin.top] : ""};
@@ -590,8 +591,8 @@ const InputContainer = styled.div<{
590591
props.error
591592
? "transparent"
592593
: props.readOnly
593-
? props.theme.hoverReadOnlyBorderColor
594-
: props.theme.hoverBorderColor
594+
? props.theme.hoverReadOnlyBorderColor
595+
: props.theme.hoverBorderColor
595596
};
596597
${props.error ? `box-shadow: 0 0 0 2px ${props.theme.hoverErrorBorderColor};` : ""}
597598
}

0 commit comments

Comments
 (0)