Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
more flexible html structure for jsx components (#34)
Browse files Browse the repository at this point in the history
* set siblings after next element sibling to not include hidden prop
* fix sibling targeting for tabs and input as well as chaning forEach usage to map to get returned elements
  • Loading branch information
wwalmnes authored and eirikbacker committed May 3, 2018
1 parent 1cb3c04 commit 6d00e2e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/core-dialog/core-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const FOCUSABLE_ELEMENTS = `
export default function dialog (dialogs, open) {
const options = typeof open === 'object' ? open : {open}

return queryAll(dialogs).forEach((dialog) => {
return queryAll(dialogs).map((dialog) => {
const hasBackdrop = (dialog.nextElementSibling || {}).nodeName === 'BACKDROP'

dialog.setAttribute(UUID, '')
Expand Down
2 changes: 1 addition & 1 deletion packages/core-input/core-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function input (elements, content) {
const options = typeof content === 'object' ? content : {content}
const repaint = typeof options.content === 'string'

return queryAll(elements).forEach((input) => {
return queryAll(elements).map((input) => {
const list = input.nextElementSibling

input.setAttribute(UUID, '')
Expand Down
13 changes: 9 additions & 4 deletions packages/core-input/core-input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ export default class Input extends React.Component {
}
render () {
return React.createElement('div', exclude(this.props, DEFAULTS, {ref: el => (this.el = el)}),
React.Children.map(this.props.children, (child, adjacent) => adjacent
? React.cloneElement(child, {'hidden': !this.props.open})
: React.cloneElement(child, {'aria-expanded': String(Boolean(this.props.open))})
)
React.Children.map(this.props.children, (child, adjacent) => {
if (adjacent === 0) {
return React.cloneElement(child, {
'aria-expanded': String(Boolean(this.props.open))
})
}
if (adjacent === 1) return React.cloneElement(child, {'hidden': !this.props.open})
return child
})
)
}
}
Expand Down
17 changes: 10 additions & 7 deletions packages/core-tabs/core-tabs.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ export default class Tabs extends React.Component {
const attr = exclude(this.props, DEFAULTS, {ref: (el) => (this.el = el)})

return React.createElement('div', attr,
React.Children.map(this.props.children, (group, isPanelGroup) =>
React.cloneElement(group, null, React.Children.map(group.props.children, (child, index) =>
React.cloneElement(child, isPanelGroup
? {hidden: open !== index}
: {'aria-selected': open === index})
))
)
React.Children.map(this.props.children, (group, isPanelGroup) => {
if (isPanelGroup < 2) {
return React.cloneElement(group, null, React.Children.map(group.props.children, (child, index) =>
React.cloneElement(child, isPanelGroup
? {hidden: open !== index}
: {'aria-selected': open === index})
))
}
return group
})
)
}
}
2 changes: 1 addition & 1 deletion packages/core-toggle/core-toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const POPS = 'aria-haspopup'
export default function toggle (buttons, open) {
const options = typeof open === 'object' ? open : {open}

return queryAll(buttons).forEach((button) => {
return queryAll(buttons).map((button) => {
const open = typeof options.open === 'boolean' ? options.open : button.getAttribute(OPEN) === 'true'
const pops = typeof options.popup === 'boolean' ? options.popup : button.getAttribute(POPS) === 'true'
const next = button.nextElementSibling
Expand Down
17 changes: 10 additions & 7 deletions packages/core-toggle/core-toggle.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ export default class Toggle extends React.Component {
componentWillUnmount () { this.el.removeEventListener('toggle', this.props.onToggle) }
render () {
return React.createElement('div', exclude(this.props, DEFAULTS, {ref: (el) => (this.el = el)}),
React.Children.map(this.props.children, (child, adjacent) => adjacent
? React.cloneElement(child, {'hidden': !this.props.open})
: React.cloneElement(child, {
'aria-expanded': String(Boolean(this.props.open)),
'aria-haspopup': String(Boolean(this.props.popup))
})
)
React.Children.map(this.props.children, (child, adjacent) => {
if (adjacent === 0) {
return React.cloneElement(child, {
'aria-expanded': String(Boolean(this.props.open)),
'aria-haspopup': String(Boolean(this.props.popup))
})
}
if (adjacent === 1) return React.cloneElement(child, {'hidden': !this.props.open})
return child
})
)
}
}

0 comments on commit 6d00e2e

Please sign in to comment.