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

Added onFocus and onBlur props. (#170) #174

Merged
merged 5 commits into from
Jan 7, 2019
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

## Unreleased

### Added

- Add [`onFocus`](https://www.draftail.org/docs/next/api#managing-focus) and [`onBlur`](https://www.draftail.org/docs/next/api#managing-focus) props to use callbacks on those events. This can be useful for [form validation](https://www.draftail.org/docs/next/form-validation). [#170](https://github.com/springload/draftail/issues/170), [#174](https://github.com/springload/draftail/pull/174), thanks to [@TheSpicyMeatball](https://github.com/TheSpicyMeatball).

### Fixed

- Stop unnecessarily calling `onSave` in the editor’s `onBlur` ([#173](https://github.com/springload/draftail/issues/173)).
Expand Down
47 changes: 45 additions & 2 deletions examples/docs.story.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import React from "react";
import { injectIntl } from "react-intl";
import { convertFromHTML, convertToHTML } from "draft-convert";
import { convertToRaw, convertFromRaw } from "draft-js";
import { Formik } from "formik";

import { INLINE_STYLE, ENTITY_TYPE, BLOCK_TYPE } from "../lib";
import { DraftailEditor, INLINE_STYLE, ENTITY_TYPE, BLOCK_TYPE } from "../lib";

import {
INLINE_CONTROL,
Expand Down Expand Up @@ -368,4 +369,46 @@ storiesOf("Docs", module)
entityTypes={[ENTITY_CONTROL.LINK, ENTITY_CONTROL.IMAGE]}
/>
);
});
})
.add("Form validation", () => (
<Formik
initialValues={{ content: null }}
onSubmit={window.alert.bind(null, "Success!")}
validate={(values) => {
const errors = {};

if (!values.content) {
errors.content = "Please enter at least two paragraphs";
} else {
const { blocks, entityMap } = values.content;
if (Object.keys(entityMap).length === 0) {
errors.content = "Please use at least one link";
}

// Check there are at least two blocks with non-whitespace text
if (blocks.filter((b) => b.text.trim().length > 0).length < 2) {
errors.content = "Please enter at least two paragraphs";
}
}

return errors;
}}
>
{({ errors, touched, handleSubmit, setFieldTouched, setFieldValue }) => (
<form onSubmit={handleSubmit}>
<div className="form-field">
<DraftailEditor
entityTypes={[ENTITY_CONTROL.LINK]}
onSave={setFieldValue.bind(null, "content")}
onBlur={setFieldTouched.bind(null, "content")}
stateSaveInterval={errors.content ? 50 : 250}
/>
<div role="alert">
{errors.content && touched.content && errors.content}
</div>
</div>
<button type="submit">Submit</button>
</form>
)}
</Formik>
));
7 changes: 7 additions & 0 deletions examples/utils/_forms.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
$color-error: #dc143c;

fieldset {
padding: 0;
margin: 0;
Expand Down Expand Up @@ -60,3 +62,8 @@ fieldset {
display: block;
margin-bottom: 1rem;
}

.form-field [role="alert"] {
color: $color-error;
min-height: 18px;
}
18 changes: 18 additions & 0 deletions lib/components/DraftailEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,24 @@ class DraftailEditor extends Component {
this.setState({
hasFocus: true,
});

const { onFocus } = this.props;

if (onFocus) {
onFocus();
}
}

onBlur() {
this.setState({
hasFocus: false,
});

const { onBlur } = this.props;

if (onBlur) {
onBlur();
}
}

onTab(event) {
Expand Down Expand Up @@ -647,6 +659,10 @@ DraftailEditor.defaultProps = {
rawContentState: null,
// Called when changes occured. Use this to persist editor content.
onSave: () => {},
// Called when the editor receives focus.
onFocus: null,
// Called when the editor loses focus.
onBlur: null,
// Displayed when the editor is empty. Hidden if the user changes styling.
placeholder: null,
// Enable the use of horizontal rules in the editor.
Expand Down Expand Up @@ -698,6 +714,8 @@ DraftailEditor.defaultProps = {
DraftailEditor.propTypes = {
rawContentState: PropTypes.object,
onSave: PropTypes.func,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
placeholder: PropTypes.string,
enableHorizontalRule: PropTypes.oneOfType([
PropTypes.bool,
Expand Down
18 changes: 18 additions & 0 deletions lib/components/DraftailEditor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,24 @@ describe("DraftailEditor", () => {
expect(onSave).not.toHaveBeenCalled();
});

it("#onFocus", () => {
const onFocus = jest.fn();
const wrapper = shallowNoLifecycle(<DraftailEditor onFocus={onFocus} />);

wrapper.instance().onFocus();

expect(onFocus).toHaveBeenCalled();
});

it("#onBlur", () => {
const onBlur = jest.fn();
const wrapper = shallowNoLifecycle(<DraftailEditor onBlur={onBlur} />);

wrapper.instance().onBlur();

expect(onBlur).toHaveBeenCalled();
});

it("onTab", () => {
jest.spyOn(RichUtils, "onTab");

Expand Down
69 changes: 69 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"eslint": "^5.6.0",
"eslint-plugin-compat": "^2.5.1",
"express": "^4.16.3",
"formik": "^1.4.1",
"immutable": "~3.7.4",
"jest": "^23.6.0",
"jest-axe": "^3.1.0",
Expand Down
6 changes: 4 additions & 2 deletions tests/integration/performance.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const PERFORMANCE_BUFFER = 1.5;

describe("performance", () => {
let page;
beforeEach(async () => {
Expand Down Expand Up @@ -33,7 +35,7 @@ describe("performance", () => {
() => window.performance.memory.usedJSHeapSize,
);
const heapSizeMB = heapSize / 10 ** 6;
expect(heapSizeMB).toBeLessThanOrEqual(19);
expect(heapSizeMB).toBeLessThanOrEqual(19 * PERFORMANCE_BUFFER);
});

it("markov_draftjs 41 speed", async () => {
Expand All @@ -46,6 +48,6 @@ describe("performance", () => {
const mean = await page.$eval('[data-benchmark="mean"]', (elt) =>
parseFloat(elt.innerHTML),
);
expect(mean).toBeLessThanOrEqual(47 * 1.5);
expect(mean).toBeLessThanOrEqual(47 * PERFORMANCE_BUFFER);
});
});