slider.tsx


Overview

The slider.tsx file defines a React functional component named SliderInputSwitchFormField that integrates a toggle switch, a slider, and a numeric input into a single form field. This component is designed to be used within a React Hook Form context, enabling seamless form state management and validation.

The main purpose of this component is to allow users to enable or disable a slider input via a switch and adjust its value either by sliding or typing a number. The component ensures synchronization between the slider and number input values and supports optional configurations like minimum, maximum, and step values. It also supports localization for labels and tooltips.


Component: SliderInputSwitchFormField

Description

SliderInputSwitchFormField renders a composite form input field consisting of:

All three controls are linked to the same form state using React Hook Form's useFormContext. The toggle switch controls whether the slider and number input are enabled or disabled.


Props

Prop Name

Type

Required

Default

Description

max

number

No

The maximum value allowed for the slider and number input.

min

number

No

The minimum value allowed for the slider and number input.

step

number

No

The increment/decrement step for the slider and number input.

name

string

Yes

The form field name for the slider and number input values.

label

string

Yes

The label text key used for translation and display.

defaultValue

number

No

The default value for the slider and number input.

onChange

(value: number) => void

No

Optional callback triggered when the slider or number input value changes.

className

string

No

Additional CSS classes to apply to the container of controls for custom styling.

checkName

string

Yes

The form field name for the toggle switch that enables/disables the slider and number input.


Usage Example

import { useForm, FormProvider } from 'react-hook-form';
import { SliderInputSwitchFormField } from './slider';

function ExampleForm() {
  const methods = useForm({
    defaultValues: {
      volume: 50,
      volumeEnabled: true,
    },
  });

  return (
    <FormProvider {...methods}>
      <form>
        <SliderInputSwitchFormField
          name="volume"
          checkName="volumeEnabled"
          min={0}
          max={100}
          step={1}
          label="volume"
          onChange={(val) => console.log('Volume changed to:', val)}
        />
      </form>
    </FormProvider>
  );
}

Detailed Behavior


Key Implementation Details


Interactions with Other Parts of the System


Mermaid Component Diagram of SliderInputSwitchFormField

componentDiagram
    component SliderInputSwitchFormField {
      +props: SliderInputSwitchFormFieldProps
      -form: UseFormContext
      -disabled: boolean
      -t: translate function
      +render()
    }

    SliderInputSwitchFormField --> FormField : renders
    FormField --> FormItem
    FormItem --> FormLabel
    FormItem --> FormMessage
    FormItem --> FormControl
    FormControl --> Switch : toggle enabled state
    FormControl --> SingleFormSlider : slider input
    FormControl --> NumberInput : numeric input

    SliderInputSwitchFormField ..> useTranslate : localization hook
    SliderInputSwitchFormField ..> useFormContext : form hook
    SliderInputSwitchFormField ..> cn : classnames utility

Summary

slider.tsx provides a reusable, integrated switch-slider-number input form field component suitable for forms managed by React Hook Form. Its design supports user-friendly enabling/disabling of a numeric input with synchronized slider and text input, complete with localization and accessibility considerations. The component leverages the project’s UI and utility libraries and is easily customizable via props.

This file fits into the UI/form layer of the application, facilitating complex form interactions with minimal setup by consumers.