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(NumberField): add belowMin and aboveMax error keys to NumberField #2853

Merged
merged 5 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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}.',
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Story extends Component {
static displayName = 'Story';
static propTypes = {
onChange: PropTypes.func.isRequired,
value: PropTypes.string,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
id: PropTypes.string,
};
static defaultProps = {
Expand Down 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ragafus Do you have a bit of time for us (remeet) to elaborate as to why this change is needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ragafus Do you have a bit of time for us (remeet) to elaborate as to why this change is needed?

Of course! I can explain my point of view as a "consumer" of NumberField component.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! I will set up a meeting for us.
Thanks again

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
Loading