-
Notifications
You must be signed in to change notification settings - Fork 843
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
Tie field to label without explicit id, and to supporting elements #130
Changes from 2 commits
4c64a20
e498857
5edc98a
459fb8d
e69149a
626f88d
795201a
ca41f4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ import { EuiFormHelpText } from '../form_help_text'; | |
import { EuiFormErrorText } from '../form_error_text'; | ||
import { EuiFormLabel } from '../form_label'; | ||
|
||
import makeId from './make_id'; | ||
|
||
export class EuiFormRow extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
@@ -40,13 +42,16 @@ export class EuiFormRow extends Component { | |
isInvalid, | ||
error, | ||
label, | ||
id, | ||
hasEmptyLabelSpace, | ||
fullWidth, | ||
className, | ||
...rest | ||
} = this.props; | ||
|
||
const id = rest.id | ||
|| (children && children.props && children.props.id) | ||
|| makeId(); | ||
|
||
const classes = classNames( | ||
'euiFormRow', | ||
{ | ||
|
@@ -60,7 +65,7 @@ export class EuiFormRow extends Component { | |
|
||
if (helpText) { | ||
optionalHelpText = ( | ||
<EuiFormHelpText className="euiFormRow__text"> | ||
<EuiFormHelpText id={makeId()} className="euiFormRow__text"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could then append the string "help" to the |
||
{helpText} | ||
</EuiFormHelpText> | ||
); | ||
|
@@ -71,7 +76,7 @@ export class EuiFormRow extends Component { | |
if (error) { | ||
const errorTexts = Array.isArray(error) ? error : [error]; | ||
optionalErrors = errorTexts.map(error => ( | ||
<EuiFormErrorText key={error} className="euiFormRow__text"> | ||
<EuiFormErrorText key={error} id={makeId()} className="euiFormRow__text"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could do something similar here by appending "error" and |
||
{error} | ||
</EuiFormErrorText> | ||
)); | ||
|
@@ -91,10 +96,24 @@ export class EuiFormRow extends Component { | |
); | ||
} | ||
|
||
const describingIds = []; | ||
if (optionalHelpText) { | ||
describingIds.push(optionalHelpText.props.id); | ||
} | ||
if (optionalErrors) { | ||
optionalErrors.forEach(error => describingIds.push(error.props.id)); | ||
} | ||
|
||
const optionalProps = {}; | ||
if (describingIds.length > 0) { | ||
optionalProps[`aria-describedby`] = describingIds.join(` `); | ||
} | ||
|
||
const field = cloneElement(children, { | ||
id, | ||
onFocus: this.onFocus, | ||
onBlur: this.onBlur, | ||
...optionalProps | ||
}); | ||
|
||
return ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function makeId() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about a comment for posterity? // Generate statistically almost-certainly-unique `id`s for associating form inputs with their labels
// and other descriptive text elements. |
||
return Math.random().toString(36).slice(-8); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we make
id
required, the consumer can use the generator to create anid
with a unique suffix.