Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Implement truncation warning for large numeric input values #1186

Open
wants to merge 5 commits into
base: 3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"build:types": "node ./tools/schema-ts-generator.js && prettier src/type.source.ts --write",
"check": "tsc --noEmit",
"lint:check": "eslint ./src",
"_format": "prettier **/*.{ts,tsx,json}",
"_format": "prettier **/*.{ts,tsx,json,scss}",
"format": "npm run _format -- --write",
"format:check": "npm run _format -- --list-different",
"test:coverage": "vitest run --coverage",
Expand Down
4 changes: 4 additions & 0 deletions src/components/Input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,15 @@ export const CustomInput = slottableComponent<CustomProps>('Input', (props) => {
disabled={disabled}
readOnly={readOnly}
value={(value ?? '').toString()}
title={value ?? ''}
onChange={(e) => onChange(e.target.value)}
aria-required={required}
required={required}
maxLength={maxLength}
aria-invalid={!!errors}
onBlur={(e) => {
e.target.setSelectionRange(0, 0);
}}
/>
<ComponentErrors errors={errors} />
</div>
Expand Down
20 changes: 20 additions & 0 deletions src/components/InputNumber/InputNumber.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

it('renders without crashing', () => {
const { container } = render(<InputNumber {...baseProps} />);
expect(container).toMatchSnapshot();

Check failure on line 22 in src/components/InputNumber/InputNumber.spec.tsx

View workflow job for this annotation

GitHub Actions / SonarCloud

src/components/InputNumber/InputNumber.spec.tsx > InputNumber > renders without crashing

Error: Snapshot `InputNumber > renders without crashing 1` mismatched - Expected + Received @@ -7,10 +7,11 @@ aria-labelledby="label-number" class="" id="number" inputmode="numeric" lang="en" + title="10" type="text" value="10" /> </div> </div> ❯ src/components/InputNumber/InputNumber.spec.tsx:22:21
});

it('renders label and input', () => {
Expand Down Expand Up @@ -97,7 +97,7 @@
const { container } = render(
<InputNumber {...baseProps} value={123} readOnly />
);
expect(container).toMatchSnapshot();

Check failure on line 100 in src/components/InputNumber/InputNumber.spec.tsx

View workflow job for this annotation

GitHub Actions / SonarCloud

src/components/InputNumber/InputNumber.spec.tsx > InputNumber > should handle readOnly

Error: Snapshot `InputNumber > should handle readOnly 1` mismatched - Expected + Received @@ -8,10 +8,11 @@ class="" id="number" inputmode="numeric" lang="en" readonly="" + title="123" type="text" value="123" /> </div> </div> ❯ src/components/InputNumber/InputNumber.spec.tsx:100:21

const input = container.querySelector('input[type="text"]');
expect(input).toHaveAttribute('readonly');
Expand All @@ -112,4 +112,24 @@
const unit = container.querySelector('span');
expect(unit).toHaveTextContent('kg');
});

it('should display the input value from the start', () => {
const setSelectionRangeMock = vi.fn();
const { container } = render(
<InputNumber
{...baseProps}
value={100000000000000000000000000000000000}
/>
);

const input = container.querySelector('input[type="text"]');

fireEvent.blur(input!, {
target: {
...input,
setSelectionRange: setSelectionRangeMock,
},
});
expect(setSelectionRangeMock).toHaveBeenCalledWith(0, 0);
});
});
4 changes: 4 additions & 0 deletions src/components/InputNumber/InputNumberThousand.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const InputNumberThousand = ({
className={classNames({ disabled })}
onValueChange={handleChange}
value={value ?? ''}
title={value ? value.toString() : ''}
aria-labelledby={labelId}
disabled={disabled}
readOnly={readOnly}
Expand All @@ -68,6 +69,9 @@ export const InputNumberThousand = ({
thousandSeparator={inputNumberPropsI18N.thousandSeparator}
inputMode={decimals ? 'decimal' : 'numeric'}
aria-invalid={invalid}
onBlur={(e) => {
MailineN marked this conversation as resolved.
Show resolved Hide resolved
e.target.setSelectionRange(0, 0);
}}
MailineN marked this conversation as resolved.
Show resolved Hide resolved
/>
);
};
3 changes: 3 additions & 0 deletions src/css/components/Input.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
min-width: 0;
box-sizing: content-box;
background: none;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
&:focus {
outline: none;
border-bottom: 0.125rem solid var(--color-primary-main);
Expand Down
Loading