Mobilens 中文

Control · Selection

Toggle row

Every state

When
Behaviour

    Usage

    import { useState } from 'react';
    import { ToggleRow } from '@/components/mobilens/ToggleRow';
    
    export function PushSetting({ initial, save }: { initial: boolean; save: (on: boolean) => Promise<void> }) {
      const [on, setOn] = useState(initial);
      const [saving, setSaving] = useState(false);
    
      return (
        <ToggleRow
          label="Push notifications"
          description="Messages and reminders"
          value={on}
          pending={saving}
          onValueChange={async (next) => {
            setOn(next); // show the change at once
            setSaving(true);
            try {
              await save(next);
            } catch {
              setOn(!next); // put it back if the save failed
            } finally {
              setSaving(false);
            }
          }}
        />
      );
    }
    

    API

    PropTypeDefaultDescription
    label string – What the setting turns on, such as "Push notifications".
    description? string – One line under the label about what changes.
    value boolean – On or off.
    onValueChange (value: boolean) => void – Called with the new value when the row or the switch is tapped.
    disabled? boolean false Grey and cannot be changed. Say why in the description.
    pending? boolean false The change is being saved: shows a spinner and ignores taps until it is cleared.

    Used in