Browse Source

🐛 Fix Form throw error when using BraftEditor (#21425)

component.props.onChange should not run when it is falsy

close #21415
pull/21446/head
偏右 5 years ago
committed by GitHub
parent
commit
4f2ef2a355
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      components/form/FormItem.tsx
  2. 26
      components/form/__tests__/index.test.js

10
components/form/FormItem.tsx

@ -279,12 +279,10 @@ function FormItem(props: FormItemProps): React.ReactElement {
});
triggers.forEach(eventName => {
if (eventName in mergedControl && eventName in children.props) {
childProps[eventName] = (...args: any[]) => {
mergedControl[eventName](...args);
children.props[eventName](...args);
};
}
childProps[eventName] = (...args: any[]) => {
mergedControl[eventName]?.(...args);
children.props[eventName]?.(...args);
};
});
childNode = React.cloneElement(children, childProps);

26
components/form/__tests__/index.test.js

@ -1,4 +1,4 @@
import React from 'react';
import React, { Component } from 'react';
import { mount } from 'enzyme';
import scrollIntoView from 'scroll-into-view-if-needed';
import Form from '..';
@ -522,4 +522,28 @@ describe('Form', () => {
'Warning: [antd: Form.Item] `null` is passed as `name` property',
);
});
// https://github.com/ant-design/ant-design/issues/21415
it('Component.props.onChange is null', () => {
// eslint-disable-next-line
class CustomComponent extends Component {
static defaultProps = {
onChange: null,
};
render() {
return <input {...this.props} />;
}
}
expect(() => {
const wrapper = mount(
<Form>
<Form.Item name="custom">
<CustomComponent />
</Form.Item>
</Form>,
);
wrapper.find(CustomComponent).simulate('change', { value: '123' });
}).not.toThrow();
});
});

Loading…
Cancel
Save