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: create FieldWarnings component and update FieldErrors #2686

Merged
merged 9 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions .changeset/blue-news-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-uikit/messages': minor
---

"Forward Id prop to WarningMessage Text Detail"
5 changes: 5 additions & 0 deletions .changeset/slow-carrots-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-uikit/field-warnings': major
---

Create field warnings component
4 changes: 2 additions & 2 deletions packages/components/field-errors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const Example = () => (
renderError={(key) => {
switch (key) {
case 'duplicate':
return 'This thing is already in use. It must be unique.';
return 'This is already in use. It must be unique.';
default:
// When null is returned then the default error handling from
// renderDefaultError will kick in for that error.
Expand All @@ -56,7 +56,7 @@ const Example = () => (
renderDefaultError={(key) => {
switch (key) {
case 'minLength':
return 'This thing is too short.';
return 'This is too short.';
default:
// When null is returned then the error handling defined in
// FieldError itself will kick in
Expand Down
4 changes: 2 additions & 2 deletions packages/components/field-errors/docs/usage-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const Example = () => (
renderError={(key) => {
switch (key) {
case 'duplicate':
return 'This thing is already in use. It must be unique.';
return 'This is already in use. It must be unique.';
default:
// When null is returned then the default error handling from
// renderDefaultError will kick in for that error.
Expand All @@ -24,7 +24,7 @@ const Example = () => (
renderDefaultError={(key) => {
switch (key) {
case 'minLength':
return 'This thing is too short.';
return 'This is too short.';
default:
// When null is returned then the error handling defined in
// FieldError itself will kick in
Expand Down
49 changes: 49 additions & 0 deletions packages/components/field-errors/src/field-errors.story.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { storiesOf } from '@storybook/react';
import { withKnobs, boolean, text, object } from '@storybook/addon-knobs/react';
import Section from '../../../../docs/.storybook/decorators/section';
import Readme from '../README.md';
import FieldErrors from './field-errors';

storiesOf('Components|FieldError', module)
.addDecorator(withKnobs)
.addParameters({
readme: {
// Show readme at the addons panel
sidebar: Readme,
},
})
.add('FieldError', () => {
return (
<Section>
<FieldErrors
id={text('id', 'error-id')}
errors={object('errors', {
missing: true,
duplicate: true,
minLength: true,
})}
renderError={(key) => {
switch (key) {
case 'duplicate':
return 'This thing is already in use. It must be unique.';
default:
// When null is returned then the default error handling from
// renderDefaultError will kick in for that error.
return null;
}
}}
renderDefaultError={(key) => {
switch (key) {
case 'minLength':
return 'This thing is too short.';
default:
// When null is returned then the error handling defined in
// FieldError itself will kick in
return null;
}
}}
isVisible={boolean('isVisible', true)}
/>
</Section>
);
});
Empty file.
21 changes: 21 additions & 0 deletions packages/components/field-warnings/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 commercetools GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
93 changes: 93 additions & 0 deletions packages/components/field-warnings/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<!-- THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. -->
<!-- This file is created by the `yarn generate-readme` script. -->

# FieldWarnings

## Description

The `FieldWarnings` component allows the user to display field warnings.

## Installation

```
yarn add @commercetools-uikit/field-warnings
```

```
npm --save install @commercetools-uikit/field-warnings
```

Additionally install the peer dependencies (if not present)

```
yarn add react react-intl
```

```
npm --save install react react-intl
```

## Usage

```jsx
import FieldWarnings from '../src/field-warnings';

const Example = () => (
// This example shows how to handle custom warnings on top of the
// predefined warnings of FieldWarnings which this component and other
// Field components use under the hood.
<FieldWarnings
warnings={{
customWarning: true,
defaultWarning: true,
}}
isVisible={true}
renderWarning={(key) => {
switch (key) {
case 'customWarning':
return 'The current password is weak, You may want to use a stronger password';
default:
// When null is returned then the default warning handling from
// renderDefaultWarning will kick in for that warning.
return null;
}
}}
renderDefaultWarning={(key) => {
switch (key) {
case 'defaultWarning':
return 'Always use a strong password';
default:
// When null is returned then the warning handling defined in
// FieldWarning itself will kick in
return null;
}
}}
/>
);

export default Example;
```

## Properties

| Props | Type | Required | Default | Description |
| ---------------------- | ---------------------------------------------------------------- | :------: | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id` | `string` | | | ID of the warning field. |
| `warnings` | `Record` | | | List of warnings. Only entries with truthy values will count as active warnings. |
| `isVisible` | `boolean` | | | `true` when the warning messages should be rendered. Usually you'd pass in a `touched` state of fields. |
| `renderWarning` | `Function`<br/>[See signature.](#signature-renderWarning) | | | Function which gets called with each warning key (from the `warnings` prop) and may render a warning message or return `null` to hand the warning handling off to `renderDefaultWarning`. |
| `renderDefaultWarning` | `Function`<br/>[See signature.](#signature-renderDefaultWarning) | | | Function which gets called with each warning key (from the `warnings` prop) for which `renderWarning` returned `null`.&#xA;It may render a warning message or return `null` to hand the warning handling off to `FieldWarning`s built-in warning handling. |

## Signatures

### Signature `renderWarning`

```ts
(key: string, warning?: boolean) => ReactNode;
```

### Signature `renderDefaultWarning`

```ts
(key: string, warning?: boolean) => ReactNode;
```
1 change: 1 addition & 0 deletions packages/components/field-warnings/docs/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The `FieldWarnings` component allows the user to display field warnings.
36 changes: 36 additions & 0 deletions packages/components/field-warnings/docs/usage-example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import FieldWarnings from '../src/field-warnings';

const Example = () => (
// This example shows how to handle custom warnings on top of the
// predefined warnings of FieldWarnings which this component and other
// Field components use under the hood.
<FieldWarnings
warnings={{
customWarning: true,
defaultWarning: true,
}}
isVisible={true}
renderWarning={(key) => {
switch (key) {
case 'customWarning':
return 'The current password is weak, You may want to use a stronger password';
default:
// When null is returned then the default warning handling from
// renderDefaultWarning will kick in for that warning.
return null;
}
}}
renderDefaultWarning={(key) => {
switch (key) {
case 'defaultWarning':
return 'Always use a strong password';
default:
// When null is returned then the warning handling defined in
// FieldWarning itself will kick in
return null;
}
}}
/>
);

export default Example;
6 changes: 6 additions & 0 deletions packages/components/field-warnings/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This file exists because we want jest to use our non-compiled code to run tests
// if this file is missing, and you have a `module` or `main` that points to a non-existing file
// (ie, a bundle that hasn't been built yet) then jest will fail if the bundle is not yet built.
// all apps should export all their named exports from their root index.js
export { default } from './src';
export * from './src';
37 changes: 37 additions & 0 deletions packages/components/field-warnings/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@commercetools-uikit/field-warnings",
"description": "Renders warnings based on configuration.",
"version": "1.0.0",
"bugs": "https://github.com/commercetools/ui-kit/issues",
"repository": {
"type": "git",
"url": "https://github.com/commercetools/ui-kit.git",
"directory": "packages/components/field-warnings"
},
"homepage": "https://uikit.commercetools.com",
"keywords": ["javascript", "typescript", "design-system", "react", "uikit"],
"license": "MIT",
"publishConfig": {
"access": "public"
},
"sideEffects": false,
"main": "dist/commercetools-uikit-field-warnings.cjs.js",
"module": "dist/commercetools-uikit-field-warnings.esm.js",
"files": ["dist"],
"dependencies": {
"@babel/runtime": "^7.20.13",
"@babel/runtime-corejs3": "^7.20.13",
"@commercetools-uikit/messages": "16.12.1",
"@emotion/react": "^11.10.5",
"@emotion/styled": "^11.10.5",
"prop-types": "15.8.1"
},
"devDependencies": {
"react": "17.0.2",
"react-intl": "^6.3.2"
},
"peerDependencies": {
"react": "17.x",
"react-intl": "6.x"
}
}
1 change: 1 addition & 0 deletions packages/components/field-warnings/src/export-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { TFieldWarningsProps, TFieldWarnings } from './field-warnings';
43 changes: 43 additions & 0 deletions packages/components/field-warnings/src/field-warnings.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import FieldWarnings from './field-warnings';
import { screen, render } from '../../../../test/test-utils';

describe('when not visible', () => {
it('should render no warnings', () => {
const { container } = render(
<FieldWarnings warninings={{}} isVisible={false} />
);
expect(container).toBeEmptyDOMElement();
});
});

describe('warning renderers', () => {
it('should give highest importance to renderWarning', () => {
render(
<FieldWarnings
warnings={{ customWarning: true }}
isVisible={true}
renderWarning={(key) =>
key === 'customWarning' ? 'RENDER_WARNING' : null
}
renderDefaultWarning={(key) =>
key === 'defaultWarning' ? 'RENDER_DEFAULT_WARNING' : null
}
/>
);
expect(screen.getByText('RENDER_WARNING')).toBeInTheDocument();
});

it('should give second highest importance to renderDefaultWarning', () => {
render(
<FieldWarnings
warnings={{ defaultWarning: true }}
isVisible={true}
renderWarning={() => null}
renderDefaultWarning={(key) =>
key === 'defaultWarning' ? 'RENDER_DEFAULT_WARNING' : null
}
/>
);
expect(screen.getByText('RENDER_DEFAULT_WARNING')).toBeInTheDocument();
});
});
36 changes: 36 additions & 0 deletions packages/components/field-warnings/src/field-warnings.story.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { storiesOf } from '@storybook/react';
import { withKnobs, boolean, text, object } from '@storybook/addon-knobs/react';
import Section from '../../../../docs/.storybook/decorators/section';
import Readme from '../README.md';
import FieldWarning from './field-warnings';

storiesOf('Components|FieldWarning', module)
.addDecorator(withKnobs)
.addParameters({
readme: {
// Show readme at the addons panel
sidebar: Readme,
},
})
.add('FieldWarning', () => {
return (
<Section>
<FieldWarning
id={text('id', 'warning-id')}
warnings={object('warnings', {
customWarning: true,
defaultWarning: true,
})}
renderWarning={(key) =>
key === 'customWarning'
? 'The current password is weak, You may want to use a stronger password'
: null
}
renderDefaultWarning={(key, warning) =>
key === 'defaultWarning' ? 'Always use a strong password' : null
}
isVisible={boolean('isVisible', true)}
/>
</Section>
);
});
Loading
Loading