Skip to content

Commit

Permalink
Merge pull request #56 from environment-agency-austria/feature/hooks-…
Browse files Browse the repository at this point in the history
…rewrite

Feature/hooks rewrite
  • Loading branch information
maschino authored Apr 25, 2019
2 parents 068fcb6 + c0c7d95 commit 744a1c6
Show file tree
Hide file tree
Showing 122 changed files with 8,975 additions and 4,313 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
**/*.snap
**/*.md
12 changes: 12 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# Changelog
All notable changes to this project will be documented in this file.

## [Unreleased]
### Breaking changes
- `withValidation` has been removed, use the new hook `useValiation` instead
- `withField` has been deprecated, use the new hook `useField` instead
- removed some utils that were not intended for public use

### New features
- Added `useValidation` hook as replacement for `withValidation`
- Added `useField` hook as replacement for `withField`
- Added `useFormEventListener` hook for easy access to form events
- Added `reset` to `FormContext`

## [2.2.0] # 2019-02-19
### New features
- Added `useFormContext` hook for easy access to the form context
Expand Down
55 changes: 55 additions & 0 deletions docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
react-ocean-forms is a flexible and lightweight framework for rendering and validating forms with React.

[![npm](https://img.shields.io/npm/v/react-ocean-forms.svg)](https://www.npmjs.com/package/react-ocean-forms)
[![GitHub license](https://img.shields.io/github/license/environment-agency-austria/react-ocean-forms.svg)](https://github.com/environment-agency-austria/react-ocean-forms/blob/master/LICENSE)
[![travis](https://travis-ci.com/environment-agency-austria/react-ocean-forms.svg?branch=master)](https://travis-ci.com/environment-agency-austria/react-ocean-forms) [![Greenkeeper badge](https://badges.greenkeeper.io/environment-agency-austria/react-ocean-forms.svg)](https://greenkeeper.io/)
[![Coverage Status](https://coveralls.io/repos/github/environment-agency-austria/react-ocean-forms/badge.svg?branch=master)](https://coveralls.io/github/environment-agency-austria/react-ocean-forms?branch=master)

## Installation
with npm:

```bash
npm install --save-dev react-ocean-forms
```
or with yarn:

```bash
yarn add --dev react-ocean-forms
```

### Optional packages
The react-ocean-forms package is the core package. While you can use it on it's own, we
highly recommend adding the [react-ocean-forms-bootstrap](https://github.com/environment-agency-austria/react-ocean-forms-bootstrap)
package as well. It offers you easy bootstrap (reactstrap) integration.

If you're using react-intl in your project, add the
[react-ocean-forms-react-intl](https://github.com/environment-agency-austria/react-ocean-forms-react-intl)
package for react-intl support.

## Getting started
Assuming that you already have an up and running React app, otherwise please follow the
[create-react-app](https://github.com/facebook/create-react-app#creating-an-app) guideline.

After adding the react-ocean-forms package(s) to your project, you can simply import the components you need.

```jsx static
import React from 'react';
import { Form, Input } from 'react-ocean-forms';

function handleSubmit(values) {
console.log('form submitted with', values);
}

function GettingStarted() {
return (
<Form onSubmit={handleSubmit}>
<Input name="myInput" label="Example input" />
<button type="submit">Submit</button>
</Form>
);
}

export default GettingStarted;
```

For further documentation head to the components section.
41 changes: 24 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-ocean-forms",
"version": "2.2.0",
"version": "3.0.0-alpha.1",
"description": "Forms components for react based on the context api.",
"main": "build/index.js",
"module": "build/index.es.js",
Expand All @@ -13,7 +13,9 @@
"test:coverage": "jest --coverage",
"start": "rollup -c -w",
"build": "rollup -c",
"lint": "eslint \"src/**\""
"lint": "eslint \"src/**\"",
"start:docs": "styleguidist server",
"build:docs": "styleguidist build"
},
"peerDependencies": {
"react": "^16.8.0"
Expand All @@ -23,31 +25,36 @@
"@types/enzyme": "^3.9.1",
"@types/enzyme-adapter-react-16": "^1.0.5",
"@types/jest": "^24.0.11",
"@types/react": "^16.8.10",
"@typescript-eslint/eslint-plugin": "^1.6.0",
"@typescript-eslint/parser": "^1.6.0",
"@types/react": "^16.8.14",
"@typescript-eslint/eslint-plugin": "^1.7.0",
"@typescript-eslint/parser": "^1.7.0",
"coveralls": "^3.0.3",
"enzyme": "^3.9.0",
"enzyme-adapter-react-16": "^1.11.2",
"enzyme-adapter-react-16": "^1.12.1",
"enzyme-to-json": "^3.3.5",
"eslint": "^5.16.0",
"eslint-config-oceanjs": "^1.1.0",
"eslint-plugin-react": "^7.12.4",
"eslint-plugin-react-hooks": "^1.6.0",
"jest": "^24.5.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"rollup": "^1.7.4",
"rollup-plugin-commonjs": "^9.2.2",
"jest": "^24.7.1",
"react": "^16.9.0-alpha.0",
"react-docgen-typescript": "^1.12.4",
"react-dom": "^16.9.0-alpha.0",
"react-hooks-testing-library": "^0.5.0",
"react-styleguidist": "^9.0.8",
"react-test-renderer": "^16.9.0-alpha.0",
"rollup": "^1.10.1",
"rollup-plugin-commonjs": "^9.3.4",
"rollup-plugin-filesize": "^6.0.1",
"rollup-plugin-node-resolve": "^4.0.1",
"rollup-plugin-typescript2": "^0.20.1",
"ts-jest": "^24.0.1",
"ts-loader": "^5.3.3",
"typescript": "^3.4.1"
"rollup-plugin-node-resolve": "^4.2.3",
"rollup-plugin-typescript2": "^0.21.0",
"ts-jest": "^24.0.2",
"ts-loader": "^5.4.3",
"typescript": "^3.4.5",
"webpack": "^4.30.0"
},
"resolutions": {
"@types/react": "16.8.10",
"@types/react": "16.8.14",
"@types/enzyme": "^3.9.1"
},
"files": [
Expand Down
23 changes: 22 additions & 1 deletion src/components/FieldError/FieldError.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import React from 'react';

import { shallow, ShallowWrapper } from 'enzyme';

import { useFormContext } from '../../hooks';
import { FieldError } from './FieldError';
import { IFieldErrorProps } from './FieldError.types';

jest.mock('../../hooks');

describe('<FieldError />', () => {
const mockStringFormatter = jest.fn().mockReturnValue('string');
(useFormContext as jest.Mock).mockReturnValue({ stringFormatter: mockStringFormatter });
const setup = (props?: Partial<IFieldErrorProps>): ShallowWrapper => shallow((
<FieldError
id="unitError"
stringFormatter={mockStringFormatter}
invalid={false}
error={null}
{...props}
Expand Down Expand Up @@ -43,4 +46,22 @@ describe('<FieldError />', () => {
errorParams,
);
});

it('should render multiple errors', () => {
const wrapper = setup({
invalid: true,
error: [
{
message_id: errorId,
params: errorParams,
},
{
message_id: 'foo2',
params: { },
}
],
});

expect(wrapper).toMatchSnapshot();
});
});
13 changes: 7 additions & 6 deletions src/components/FieldError/FieldError.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,36 @@

import React from 'react';

import { toArray } from '../../utils';
import { useFormContext } from '../../hooks';
import { IFieldErrorProps } from './FieldError.types';

/**
* Component for displaying bootstrap
* form feedbacks if there are any errors
*/
export const FieldError: React.SFC<IFieldErrorProps> = (props: IFieldErrorProps): JSX.Element | null => {
export const FieldError: React.FC<IFieldErrorProps> = (props) => {
const {
id,
invalid,
error,
stringFormatter,
} = props;

const { stringFormatter } = useFormContext();

// If the field isn't invalid do nothing
if (invalid !== true || error === null) { return null; }

// Error could be either an string or an array of strings
const errorArray = toArray(error);
const errorArray = Array.isArray(error) ? error : [error];

return (
<React.Fragment>
<>
{errorArray.map((item) => {
const errorString = stringFormatter(item.message_id, item.params);

return <span key={`${id}_${item.message_id}`}>{errorString}</span>;
})}
</React.Fragment>
</>
);
};
FieldError.displayName = 'FieldError';
5 changes: 0 additions & 5 deletions src/components/FieldError/FieldError.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { TSTringFormatter } from '../../utils';
import { TFieldErrors } from '../../validators';

/**
Expand All @@ -23,8 +22,4 @@ export interface IFieldErrorProps {
* Field errors
*/
error: TFieldErrors;
/**
* stringFormatter method
*/
stringFormatter: TSTringFormatter;
}
15 changes: 15 additions & 0 deletions src/components/FieldError/__snapshots__/FieldError.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,18 @@ exports[`<FieldError /> should render an error 1`] = `
</span>
</Fragment>
`;

exports[`<FieldError /> should render multiple errors 1`] = `
<Fragment>
<span
key="unitError_foo"
>
string
</span>
<span
key="unitError_foo2"
>
string
</span>
</Fragment>
`;
46 changes: 46 additions & 0 deletions src/components/FieldGroup/FieldGroup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
### Examples
#### Simple field group
Showcase of a simple field group

```jsx
import { Form, FieldGroup, Input } from 'react-ocean-forms';

function Example() {
// Submit callback, here you'd make your api calls
const handleSubmit = (values) => {
console.log('onSubmit, values: ' + JSON.stringify(values));
};

// Notice how a field group creates a sub object
const defaultValues = {
myGroup: {
myInput1: 'input 1',
myInput2: 'input 2',
}
};

return (
<Form
className="demo"
onSubmit={handleSubmit}
defaultValues={defaultValues}
>
<FieldGroup
name="myGroup"
label="Example group"
render={() => {
return (
<React.Fragment>
<Input name="myInput1" label="Example input" />
<Input name="myInput2" label="Example input 2" />
</React.Fragment>
);
}}
/>
<button type="submit">Submit</button>
</Form>
);
}

<Example />
```
Loading

0 comments on commit 744a1c6

Please sign in to comment.