-
Notifications
You must be signed in to change notification settings - Fork 350
refactor(toggle): migrate Toggle from Flow to TypeScript #4764
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
1 change: 0 additions & 1 deletion
1
src/components/toggle/Toggle.stories.js → src/components/toggle/Toggle.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| // @flow | ||
| /* eslint-disable react-hooks/rules-of-hooks */ | ||
| import * as React from 'react'; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import * as React from 'react'; | ||
| import classNames from 'classnames'; | ||
|
|
||
| import './Toggle.scss'; | ||
|
|
||
| export interface ToggleProps | ||
| extends Omit< | ||
| React.InputHTMLAttributes<HTMLInputElement>, | ||
| 'checked' | 'disabled' | 'onMouseEnter' | 'onMouseLeave' | ||
| > { | ||
| /** CSS class for the toggle container */ | ||
| className?: string; | ||
| /** Optional attribute used for targeting */ | ||
| 'data-target-id'?: string; | ||
| /** Description of the input */ | ||
| description?: React.ReactNode; | ||
| /** Whether the toggle is disabled. @TODO: eventually call this `disabled` */ | ||
| isDisabled?: boolean; | ||
| /** Toggle state. @TODO: eventually call this `checked` */ | ||
| isOn?: boolean; | ||
| /** If set to true, the toggle will be aligned to the right */ | ||
| isToggleRightAligned?: boolean; | ||
| /** Label displayed for the input */ | ||
| label: React.ReactNode; | ||
| /** Name of the input */ | ||
| name?: string; | ||
| /** blur callback function called with event as the argument */ | ||
| onBlur?: (e: React.FocusEvent<HTMLInputElement>) => void; | ||
| /** change callback function called with event as the argument */ | ||
| onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void; | ||
| /** focus callback function called with event as the argument */ | ||
| onFocus?: (e: React.FocusEvent<HTMLInputElement>) => void; | ||
| /** mouse enter callback function called with event as the argument */ | ||
| onMouseEnter?: (e: React.MouseEvent<HTMLDivElement>) => void; | ||
| /** mouse leave callback function called with event as the argument */ | ||
| onMouseLeave?: (e: React.MouseEvent<HTMLDivElement>) => void; | ||
| /** optional value for the toggles checkbox */ | ||
| value?: React.InputHTMLAttributes<HTMLInputElement>['value']; | ||
| } | ||
|
|
||
| const Toggle = React.forwardRef<HTMLInputElement, ToggleProps>( | ||
| ( | ||
| { | ||
| className = '', | ||
| 'data-target-id': dataTargetId, | ||
| description, | ||
| isDisabled, | ||
| isOn, | ||
| isToggleRightAligned = false, | ||
| label, | ||
| name, | ||
| onBlur, | ||
| onChange, | ||
| onFocus, | ||
| onMouseEnter, | ||
| onMouseLeave, | ||
| ...rest | ||
| }, | ||
| ref, | ||
| ) => { | ||
| const classes = classNames('toggle-container', className, { | ||
| 'is-toggle-right-aligned': isToggleRightAligned, | ||
| }); | ||
|
|
||
| const toggleElements = [ | ||
| <div key="toggle-simple-switch" className="toggle-simple-switch" />, | ||
| <div key="toggle-simple-label" className="toggle-simple-label"> | ||
| {label} | ||
| </div>, | ||
| ]; | ||
| if (isToggleRightAligned) { | ||
| toggleElements.reverse(); | ||
| } | ||
|
|
||
| return ( | ||
| <div className={classes} onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave}> | ||
| <label className="toggle-simple" data-target-id={dataTargetId}> | ||
| <input | ||
| checked={isOn} | ||
| className="toggle-simple-input" | ||
| disabled={isDisabled} | ||
| name={name} | ||
| onBlur={onBlur} | ||
| onChange={onChange} | ||
| onFocus={onFocus} | ||
| ref={ref} | ||
| role="switch" | ||
| type="checkbox" | ||
| {...rest} | ||
| /> | ||
| {toggleElements} | ||
| </label> | ||
| {description ? <div className="toggle-simple-description">{description}</div> : null} | ||
| </div> | ||
| ); | ||
| }, | ||
| ); | ||
| Toggle.displayName = 'Toggle'; | ||
|
|
||
| export default Toggle; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import * as React from 'react'; | ||
| import type { FieldProps } from 'formik'; | ||
|
|
||
| import TogglePrimitive from './Toggle'; | ||
| import type { ToggleProps } from './Toggle'; | ||
|
|
||
| export interface ToggleFieldProps extends Omit<ToggleProps, 'form'>, FieldProps {} | ||
|
|
||
| const ToggleField = ({ | ||
| field, | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars -- strip Formik form and meta bags from forwarded props | ||
| form, | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars -- strip Formik form and meta bags from forwarded props | ||
| meta, | ||
| ...rest | ||
| }: ToggleFieldProps) => { | ||
| const { value } = field; | ||
| return <TogglePrimitive {...field} {...rest} isOn={!!value} />; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| export default ToggleField; | ||
5 changes: 4 additions & 1 deletion
5
...omponents/toggle/__tests__/Toggle.test.js → ...mponents/toggle/__tests__/Toggle.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 4 additions & 3 deletions
7
...ents/toggle/__tests__/ToggleField.test.js → ...nts/toggle/__tests__/ToggleField.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export { default } from './Toggle'; | ||
| export { default as ToggleField } from './ToggleField'; | ||
| export type { ToggleProps } from './Toggle'; | ||
| export type { ToggleFieldProps } from './ToggleField'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.