Skip to content

Commit 1c762ba

Browse files
authored
Merge pull request #2264 from dxc-technology/Mil4n0r/implement_textinput_alignment
Added new prop `alignment` to `TextInput`
2 parents a0cade1 + efb9af2 commit 1c762ba

File tree

5 files changed

+48
-5
lines changed

5 files changed

+48
-5
lines changed

apps/website/screens/components/text-input/code/TextInputCodePage.tsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import action from "./examples/action";
99
import functionSuggestions from "./examples/functionSuggestions";
1010
import errorHandling from "./examples/errorHandling";
1111
import Code, { TableCode, ExtendedTableCode } from "@/common/Code";
12+
import StatusBadge from "@/common/StatusBadge";
1213

1314
const actionTypeString = `{
1415
icon?: string | (React.ReactNode
@@ -32,13 +33,32 @@ const sections = [
3233
</thead>
3334
<tbody>
3435
<tr>
35-
<td>action</td>
3636
<td>
37-
<ExtendedTableCode>{actionTypeString}</ExtendedTableCode>
37+
<DxcFlex direction="column" gap="var(--spacing-gap-xs)" alignItems="baseline">
38+
<StatusBadge status="new" />
39+
alignment
40+
</DxcFlex>
41+
</td>
42+
<td>
43+
<TableCode>'left' | 'right'</TableCode>
44+
</td>
45+
<td>
46+
Sets <Code>text-align</Code> CSS property inside the input. See{" "}
47+
<DxcLink newWindow href="https://developer.mozilla.org/en-US/docs/Web/CSS/text-align">
48+
MDN
49+
</DxcLink>{" "}
50+
for further information.
3851
</td>
3952
<td>
40-
Action to be displayed on the right side of the input.
53+
<TableCode>'left'</TableCode>
54+
</td>
55+
</tr>
56+
<tr>
57+
<td>action</td>
58+
<td>
59+
<ExtendedTableCode>{actionTypeString}</ExtendedTableCode>
4160
</td>
61+
<td>Action to be displayed on the right side of the input.</td>
4262
<td>-</td>
4363
</tr>
4464
<tr>

apps/website/screens/components/text-input/overview/TextInputOverviewPage.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ const sections = [
254254
<strong>Support various states consistently:</strong> reflect focus, disabled, error, and read-only states
255255
with distinct, accessible visual cues.
256256
</DxcBulletedList.Item>
257+
<DxcBulletedList.Item>
258+
<strong>Apply appropiate alignment:</strong> It is recommended to use left alignment in general, except
259+
when there is a specific need for right alignment, such as in currency inputs or numeric fields, generally
260+
using a suffix.
261+
</DxcBulletedList.Item>
257262
</DxcBulletedList>
258263
),
259264
},

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,16 @@ const TextInput = () => (
139139
action={action}
140140
/>
141141
</ExampleContainer>
142-
<Title title="Anatomy" theme="light" level={2} />{" "}
142+
<Title title="Alignment" theme="light" level={2} />
143+
<ExampleContainer>
144+
<Title title="Alignment left" theme="light" level={4} />
145+
<DxcTextInput label="Text input" defaultValue="Aligned text" alignment="left" />
146+
</ExampleContainer>
147+
<ExampleContainer>
148+
<Title title="Alignment right" theme="light" level={4} />
149+
<DxcTextInput label="Text input" defaultValue="Aligned text" alignment="right" />
150+
</ExampleContainer>
151+
<Title title="Anatomy" theme="light" level={2} />
143152
<ExampleContainer>
144153
<Title title="Complete example" theme="light" level={4} />
145154
<DxcTextInput

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ const TextInput = styled.div<{
6666
${({ disabled, error, readOnly }) => inputStylesByState(disabled, error, readOnly)}
6767
`;
6868

69-
const Input = styled.input`
69+
const Input = styled.input<{
70+
alignment: TextInputPropsType["alignment"];
71+
}>`
7072
background: none;
7173
border: none;
7274
outline: none;
@@ -79,6 +81,7 @@ const Input = styled.input`
7981
overflow: hidden;
8082
text-overflow: ellipsis;
8183
white-space: nowrap;
84+
${({ alignment }) => `text-align: ${alignment}`};
8285
8386
::placeholder {
8487
color: ${({ disabled }) => (disabled ? "var(--color-fg-neutral-medium)" : "var(--color-fg-neutral-strong)")};
@@ -105,6 +108,7 @@ const AutosuggestWrapper = ({ condition, wrapper, children }: AutosuggestWrapper
105108
const DxcTextInput = forwardRef<RefType, TextInputPropsType>(
106109
(
107110
{
111+
alignment = "left",
108112
action,
109113
ariaLabel = "Text input",
110114
autocomplete = "off",
@@ -505,6 +509,7 @@ const DxcTextInput = forwardRef<RefType, TextInputPropsType>(
505509
</Addon>
506510
)}
507511
<Input
512+
alignment={alignment}
508513
aria-activedescendant={
509514
hasSuggestions(suggestions) && isOpen && visualFocusIndex !== -1
510515
? `suggestion-${visualFocusIndex}`

packages/lib/src/text-input/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ type Action = {
1919
};
2020

2121
type Props = {
22+
/**
23+
* Sets the alignment inside the input.
24+
*/
25+
alignment?: "left" | "right";
2226
/**
2327
* Text to be placed above the input. This label will be used as the aria-label attribute of the list of suggestions.
2428
*/

0 commit comments

Comments
 (0)