Skip to content

Commit

Permalink
Fix valid selector error of form, close ant-design#7693
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 committed Sep 21, 2017
1 parent 5d7ef9d commit 3d1914f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
19 changes: 12 additions & 7 deletions components/form/FormItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { findDOMNode } from 'react-dom';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Animate from 'rc-animate';
import PureRenderMixin from 'rc-util/lib/PureRenderMixin';
import Row from '../grid/row';
import Col, { ColProps } from '../grid/col';
Expand Down Expand Up @@ -125,11 +126,15 @@ export default class FormItem extends React.Component<FormItemProps, any> {
renderHelp() {
const prefixCls = this.props.prefixCls;
const help = this.getHelpMsg();
return help ? (
<div className={`${prefixCls}-explain`} key="help">
{help}
</div>
) : null;
const children = help ? (
<div className={`${prefixCls}-explain`} key="help">
{help}
</div>
) : null;
return (
<Animate transitionName="show-help" component="" transitionAppear key="help">
{children}
</Animate>);
}

renderExtra() {
Expand Down Expand Up @@ -220,10 +225,10 @@ export default class FormItem extends React.Component<FormItemProps, any> {
if (!id) {
return;
}
const controls = document.querySelectorAll(`#${id}`);
const controls = document.querySelectorAll(`[id="${id}"]`);
if (controls.length !== 1) {
e.preventDefault();
const control = findDOMNode(this).querySelector(`#${id}`) as HTMLElement;
const control = findDOMNode(this).querySelector(`[id="${id}"]`) as HTMLElement;
if (control && control.focus) {
control.focus();
}
Expand Down
16 changes: 16 additions & 0 deletions components/form/__tests__/label.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,20 @@ describe('Form', () => {
wrapper.find('Form label').at(1).simulate('click');
expect(wrapper.find('Form input').at(1).node).toBe(document.activeElement);
});

// https://github.com/ant-design/ant-design/issues/7693
it('should not throw error when is not a valid id', () => {
const Form1 = Form.create()(({ form }) => (
<Form>
<Form.Item label="label 1">
{form.getFieldDecorator('member[0].name.firstname')(<input />)}
</Form.Item>
</Form>
));
const wrapper = mount(<Form1 />);
expect(() => {
wrapper.find('Form label').at(0).simulate('click');
}).not.toThrow();
expect(wrapper.find('Form input').at(0).node).toBe(document.activeElement);
});
});

0 comments on commit 3d1914f

Please sign in to comment.