Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(form): Created SwitchTrack and InputToggleIcon components
- Loading branch information
Showing
5 changed files
with
210 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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 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,75 @@ | ||
import React, { forwardRef, HTMLAttributes, ReactElement } from "react"; | ||
import cn from "classnames"; | ||
import { bem } from "@react-md/utils"; | ||
|
||
/** | ||
* @remarks \@since 2.8.0 | ||
*/ | ||
export interface InputToggleIconProps extends HTMLAttributes<HTMLSpanElement> { | ||
/** | ||
* Boolean if the icon should use circle styles. This should normally be | ||
* enabled for radio input types. | ||
*/ | ||
circle?: boolean; | ||
|
||
/** | ||
* Boolean if the disabled styles should be applied. | ||
*/ | ||
disabled?: boolean; | ||
|
||
/** | ||
* Boolean if using an overlay for the different icon states. | ||
*/ | ||
overlay?: boolean; | ||
|
||
/** | ||
* Boolean if the icon should gain the checked state. | ||
*/ | ||
checked?: boolean; | ||
|
||
/** | ||
* Boolean if using the indeterminate checkbox state. | ||
*/ | ||
indeterminate?: boolean; | ||
} | ||
|
||
const styles = bem("rmd-toggle"); | ||
|
||
/** | ||
* @remarks \@since 2.8.0 | ||
*/ | ||
export const InputToggleIcon = forwardRef< | ||
HTMLSpanElement, | ||
InputToggleIconProps | ||
>(function InputToggleIcon( | ||
{ | ||
circle = false, | ||
disabled = false, | ||
overlay = false, | ||
checked = false, | ||
indeterminate = false, | ||
className, | ||
children, | ||
...props | ||
}: InputToggleIconProps, | ||
ref | ||
): ReactElement | null { | ||
return ( | ||
<span | ||
{...props} | ||
ref={ref} | ||
className={cn( | ||
styles("icon", { | ||
circle, | ||
disabled, | ||
overlay, | ||
checked, | ||
indeterminate, | ||
}), | ||
className | ||
)} | ||
> | ||
{children} | ||
</span> | ||
); | ||
}); |
This file contains 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 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 React, { | ||
CSSProperties, | ||
forwardRef, | ||
HTMLAttributes, | ||
InputHTMLAttributes, | ||
} from "react"; | ||
import cn from "classnames"; | ||
import { bem, PropsWithRef } from "@react-md/utils"; | ||
|
||
const styles = bem("rmd-switch"); | ||
|
||
/** @remarks \@since 2.8.0 */ | ||
export interface SwitchTrackProps | ||
extends InputHTMLAttributes<HTMLInputElement> { | ||
/** | ||
* If an `id` is provided, the track will contain a checkbox input element and | ||
* render the "ball" as a `<label>`. If the `id` is omitted, no input element | ||
* will be rendered and the "ball" will be rendered as a `<span>`. | ||
* | ||
* Basically only omit the `id` if this is used in another accessible widget | ||
* like `menuitemcheckbox`. | ||
*/ | ||
id?: string; | ||
|
||
/** | ||
* An optional style object to provide to the ball. | ||
*/ | ||
ballStyle?: CSSProperties; | ||
|
||
/** | ||
* An optional class name to provide to the ball. | ||
*/ | ||
ballClassName?: string; | ||
|
||
/** | ||
* Any additional props and optional ref to provide to the track itself since | ||
* all the props are normally provided to the `<input>` element instead. | ||
*/ | ||
containerProps?: PropsWithRef< | ||
HTMLAttributes<HTMLSpanElement>, | ||
HTMLSpanElement | ||
>; | ||
} | ||
|
||
/** | ||
* This is most likely an internal only component that is used to render the | ||
* switch element either as a checkbox or in the `MenuItemSwitch` component. | ||
* | ||
* @remarks \@since 2.8.0 | ||
*/ | ||
export const SwitchTrack = forwardRef<HTMLInputElement, SwitchTrackProps>( | ||
function SwitchTrack( | ||
{ | ||
id, | ||
disabled = false, | ||
checked = false, | ||
className, | ||
ballStyle, | ||
ballClassName, | ||
containerProps, | ||
children, | ||
...props | ||
}, | ||
ref | ||
) { | ||
return ( | ||
<span | ||
{...containerProps} | ||
className={cn(styles(), containerProps?.className)} | ||
> | ||
{id && ( | ||
<> | ||
<input | ||
{...props} | ||
id={id} | ||
ref={ref} | ||
type="checkbox" | ||
className={cn(styles("input"), className)} | ||
disabled={disabled} | ||
/> | ||
<label | ||
htmlFor={id} | ||
aria-hidden | ||
style={ballStyle} | ||
className={cn(styles("ball"), ballClassName)} | ||
> | ||
{children} | ||
</label> | ||
</> | ||
)} | ||
{!id && ( | ||
<span | ||
style={ballStyle} | ||
className={cn(styles("ball", { checked }), ballClassName)} | ||
/> | ||
)} | ||
</span> | ||
); | ||
} | ||
); |
This file contains 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