Skip to content

Commit

Permalink
feat: add belowMin and aboveMax error keys to NumberField
Browse files Browse the repository at this point in the history
  • Loading branch information
ragafus committed Jul 12, 2024
1 parent 46b3447 commit a8ec564
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/thirty-bikes-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@commercetools-uikit/number-field': minor
'@commercetools-uikit/i18n': minor
---

Add `belowMin` and `aboveMax` error keys to `NumberField`
2 changes: 2 additions & 0 deletions packages/components/fields/number-field/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ When the `key` is known, and when the value is truthy, and when `renderError` re
Known error keys are:

- `missing`: tells the user that this field is required
- `belowMin`: tells the user that the value is below the minimum
- `aboveMax`: tells the user that the value is above the maximum

## Static methods

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ When the `key` is known, and when the value is truthy, and when `renderError` re
Known error keys are:

- `missing`: tells the user that this field is required
- `belowMin`: tells the user that the value is below the minimum
- `aboveMax`: tells the user that the value is above the maximum

## Static methods

Expand Down
14 changes: 14 additions & 0 deletions packages/components/fields/number-field/src/messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineMessages } from 'react-intl';

export default defineMessages({
belowMinError: {
id: 'UIKit.NumberField.belowMinError',
description: 'An error message to show when the value is below the minimum',
defaultMessage: 'Value must be greater than or equal to {min}.',
},
aboveMaxError: {
id: 'UIKit.NumberField.aboveMaxError',
description: 'An error message to show when the value is above the maximum',
defaultMessage: 'Value must be less than or equal to {max}.',
},
});
28 changes: 28 additions & 0 deletions packages/components/fields/number-field/src/number-field.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,34 @@ describe('when field is touched and has errors', () => {
expect(getByText(/field is required/i)).toBeInTheDocument();
});
});
describe('when value is below min', () => {
it('should render a default error', () => {
const min = 2;
const { getByText } = renderNumberField({
value: 1,
min,
touched: true,
errors: { belowMin: true },
});
expect(
getByText(`Value must be greater than or equal to {min}.`)
).toBeInTheDocument();
});
});
describe('when value is above max', () => {
it('should render a default error', () => {
const max = 2;
const { getByText } = renderNumberField({
value: 3,
max,
touched: true,
errors: { aboveMax: true },
});
expect(
getByText(`Value must be less than or equal to ${max}.`)
).toBeInTheDocument();
});
});
describe('when there is a custom error', () => {
it('should render the custom error message', () => {
const { getByText } = renderNumberField({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ storiesOf('Components|Fields', module)
warnings={object('warnings', {
customWarning: true,
})}
errors={object('errors', { missing: true, customError: true })}
errors={object('errors', {
missing: true,
belowMin: true,
aboveMax: true,
customError: true,
})}
renderError={(key) => {
switch (key) {
case 'customError':
Expand Down
33 changes: 32 additions & 1 deletion packages/components/fields/number-field/src/number-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import FieldLabel from '@commercetools-uikit/field-label';
import FieldErrors from '@commercetools-uikit/field-errors';
import FieldWarnings from '@commercetools-uikit/field-warnings';
import NumberInput from '@commercetools-uikit/number-input';
import messages from './messages';
import { FormattedMessage } from 'react-intl';

const sequentialId = createSequentialId('number-field-');
const sequentialErrorsId = createSequentialId('number-field-error-')();
Expand Down Expand Up @@ -278,7 +280,36 @@ class NumberField extends Component<TNumberFieldProps, TNumberFieldState> {
id={sequentialErrorsId}
errors={this.props.errors}
isVisible={hasError}
renderError={this.props.renderError}
renderError={(key: string, error?: boolean) => {
const element = this.props.renderError?.(key, error);
if (element) {
return element;
} else {
switch (key) {
case 'belowMin':
if (typeof this.props.min === 'number') {
return (
<FormattedMessage
{...messages.belowMinError}
values={{ min: this.props.min }}
/>
);
}
break;
case 'aboveMax':
if (typeof this.props.max === 'number') {
return (
<FormattedMessage
{...messages.aboveMaxError}
values={{ max: this.props.max }}
/>
);
}
break;
}
return null;
}
}}
/>
<FieldWarnings
id={sequentialWarningsId}
Expand Down
8 changes: 8 additions & 0 deletions packages/i18n/data/core.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@
"developer_comment": "The label for \"expand\" action",
"string": "Expand"
},
"UIKit.NumberField.aboveMaxError": {
"developer_comment": "An error message to show when the value is above the maximum",
"string": "Value must be less than or equal to {max}."
},
"UIKit.NumberField.belowMinError": {
"developer_comment": "An error message to show when the value is below the minimum",
"string": "Value must be greater than or equal to {min}."
},
"UIKit.Pagination.PageNavigator.nextPageLabel": {
"developer_comment": "Label for next page button",
"string": "Next page"
Expand Down

0 comments on commit a8ec564

Please sign in to comment.