# All files Switch.tsx

0% Statements 0/150
0% Branches 0/1
0% Functions 0/1
0% Lines 0/150

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151

     import { cnb } from "cnbuilder";
    import {
      forwardRef,
      type CSSProperties,
      type HTMLAttributes,
      type InputHTMLAttributes,
      type ReactNode,
    } from "react";
    import { type PropsWithRef } from "../types.js";
    import { useEnsuredId } from "../useEnsuredId.js";
    import { FormMessageContainer } from "./FormMessageContainer.js";
    import { type InputToggleLabelProps } from "./InputToggle.js";
    import { Label } from "./Label.js";
    import { SwitchTrack } from "./SwitchTrack.js";
    import { switchStyles } from "./switchStyles.js";
    import {
      type FormComponentStates,
      type FormMessageContainerExtension,
    } from "./types.js";

    /**
     * @remarks \@since 6.0.0 Added `containerProps` and support for the
     * `FormMessage` behavior.
     */
    export interface SwitchProps
      extends InputHTMLAttributes<HTMLInputElement>,
        InputToggleLabelProps,
        FormMessageContainerExtension,
        FormComponentStates {
      containerProps?: PropsWithRef<HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
      trackProps?: PropsWithRef<HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
      trackStyle?: CSSProperties;
      trackClassName?: string;
      ballAddon?: ReactNode;
      ballProps?: PropsWithRef<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>;
      ballStyle?: CSSProperties;
      ballClassName?: string;
    }

    /**
     * **Server Component**
     *
     * @example
     * Simple Example
     * ```tsx
     * import { Form, Switch } from "@react-md/core";
     * import type { ReactElement } from "react";
     * import { useState } from "react";
     *
     * function Example(): ReactElement {
     *   const [checked, setChecked] = useState(false);
     *
     *   return (
     *     <Form>
     *       <Switch
     *         label="Label"
     *         name="enabled"
     *         checked={checked}
     *         onChange={(event) => setChecked(event.currentTarget.checked)}
     *       />
     *     </Form>
     *   );
     * }
     * ```
     *
     * @remarks \@since 6.0.0 Added support for `FormMessage` behavior.
     */
    export const Switch = forwardRef<HTMLInputElement, SwitchProps>(
      function Switch(props, ref) {
        const {
          id: propId,
          label,
          labelProps,
          style,
          className,
          containerProps,
          ballProps,
          ballStyle,
          ballClassName,
          ballAddon,
          trackProps,
          trackStyle,
          trackClassName,
          messageProps,
          messageContainerProps,
          disableLabelGap = false,
          error = false,
          active = false,
          stacked = false,
          iconAfter = false,
          disabled = false,
          readOnly = false,
          ...remaining
        } = props;
        const id = useEnsuredId(propId, "switch");

        return (
          <FormMessageContainer
            {...messageContainerProps}
            messageProps={messageProps}
          >
            <Label
              style={style}
              {...labelProps}
              className={cnb(className, labelProps?.className)}
              gap={!disableLabelGap}
              stacked={stacked}
              reversed={!iconAfter}
              active={active}
              error={error}
              disabled={disabled}
            >
              {label}
              <div
                {...containerProps}
                className={switchStyles({
                  clickable: !disabled && !readOnly,
                  className: containerProps?.className,
                  currentColor: active || error,
                })}
              >
                <SwitchTrack
                  style={trackStyle}
                  {...trackProps}
                  className={cnb(trackClassName, trackProps?.className)}
                  disabled={disabled}
                  ballAddon={ballAddon}
                  ballProps={ballProps}
                  ballStyle={ballStyle}
                  ballClassName={ballClassName}
                >
                  <input
                    {...remaining}
                    id={id}
                    ref={ref}
                    role="switch"
                    type="checkbox"
                    className={cnb(
                      "rmd-switch__input rmd-hidden-input",
                      disabled && "rmd-hidden-input--disabled"
                    )}
                    disabled={disabled}
                  />
                </SwitchTrack>
              </div>
            </Label>
          </FormMessageContainer>
        );
      }
    );