Skip to content

Commit

Permalink
chore(community-toggle-switch): remove autofocus prop
Browse files Browse the repository at this point in the history
  • Loading branch information
theetrain committed Nov 22, 2019
1 parent 273fa42 commit aede814
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 32 deletions.
26 changes: 1 addition & 25 deletions packages/ToggleSwitch/ToggleSwitch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,7 @@ import warn from '../../shared/utils/warn'

const ToggleSwitch = React.forwardRef(
(
{
id,
label,
tooltipText,
checked,
onClick,
tooltipCopy,
spinnerLabel,
autofocus,
isLoading,
...rest
},
{ id, label, tooltipText, checked, onClick, tooltipCopy, spinnerLabel, isLoading, ...rest },
ref
) => {
if (tooltipText && !tooltipCopy) {
Expand Down Expand Up @@ -63,15 +52,6 @@ const ToggleSwitch = React.forwardRef(
},
}))

useEffect(() => {
if (autofocus) {
buttonRef.current.focus()
}
/* If either checked or isLoading changes we need
to focus on buttonRef ONLY when autofocus is set
*/
}, [checked, isLoading])

const handleTooltipClick = event => {
event.preventDefault()
}
Expand Down Expand Up @@ -134,9 +114,6 @@ ToggleSwitch.propTypes = {
@param {SyntheticEvent} event The React `SyntheticEvent` */
onClick: PropTypes.func.isRequired,

/** Boolean to automatically focus on ToggleSwitch after interacting with it */
autofocus: PropTypes.bool,

/** Boolean to show or hide spinner */
isLoading: PropTypes.bool,
}
Expand All @@ -145,7 +122,6 @@ ToggleSwitch.defaultProps = {
checked: false,
tooltipText: undefined,
tooltipCopy: undefined,
autofocus: false,
isLoading: false,
}

Expand Down
14 changes: 8 additions & 6 deletions packages/ToggleSwitch/ToggleSwitch.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,22 @@ const App = () => {
;<App />
```

**Asynchoronous usage with autofocus back on toggle after checked prop transition**
**Asynchoronous usage**

```jsx
const App = () => {
const [isChecked, setIsChecked] = React.useState(false)
const [isLoading, setIsLoading] = React.useState(false)
const [isAutoFocusable, setIsAutoFocusable] = React.useState(false)
const toggleRef = React.useRef()

const handleClick = event => {
setIsLoading(true)
setIsAutoFocusable(false)

// NOTE: setTimeout does not allow proper focus management. Use promises in production
setTimeout(() => {
setIsAutoFocusable(true)
setIsChecked(!isChecked)
setIsLoading(false)
toggleRef.current.focus()
}, 2000)
}

Expand All @@ -76,6 +77,7 @@ const App = () => {
<FlexGrid.Row>
<FlexGrid.Col xs={12} md={3}>
<ToggleSwitch
ref={toggleRef}
id="toggle-autofocus"
label="Enable data"
tooltipCopy="en"
Expand All @@ -84,7 +86,6 @@ const App = () => {
checked={isChecked}
onClick={handleClick}
isLoading={isLoading}
autofocus={isAutoFocusable}
/>
</FlexGrid.Col>
</FlexGrid.Row>
Expand Down Expand Up @@ -140,8 +141,9 @@ const App = () => {
const handleToggleError = event => {
setIsLoading(true)
setShowFeedbackText(false)

// NOTE: setTimeout does not allow proper focus management. Use promises in production
setTimeout(() => {
// NOTE: setTimeout does not allow proper focus management. Use promises in production
setIsChecked(!isChecked)
setIsLoading(false)
setShowFeedbackText(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ exports[`ToggleSwitch renders 1`] = `
}
<ToggleSwitch
autofocus={false}
checked={false}
id="my-id-123"
isLoading={false}
Expand Down

0 comments on commit aede814

Please sign in to comment.