Mobilens English

控件 · 选择

开关行

所有状态

何时出现
行为规则

    用法

    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

    参数类型默认值说明
    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.

    用在这些流程里