Mobilens English

控件 · 输入

文本输入框

输入框样式

所有状态

何时出现
行为规则

    用法

    import { useState } from 'react';
    import { TextField } from '@/components/mobilens/TextField';
    
    export function EmailField({ onSubmit }: { onSubmit: (email: string) => void }) {
      const [email, setEmail] = useState('');
      const [error, setError] = useState<string>();
      const check = () => {
        const ok = /^\S+@\S+\.\S+$/.test(email);
        setError(ok ? undefined : 'Enter an email like name@example.com');
        return ok;
      };
    
      return (
        <TextField
          variant="outlined"
          label="Email"
          value={email}
          onChangeText={(next) => {
            setEmail(next);
            setError(undefined);
          }}
          error={error}
          placeholder="name@example.com"
          inputProps={{
            keyboardType: 'email-address',
            autoCapitalize: 'none',
            autoCorrect: false,
            autoComplete: 'email',
            textContentType: 'emailAddress',
            returnKeyType: 'next',
            onSubmitEditing: () => check() && onSubmit(email),
          }}
        />
      );
    }
    

    API

    参数类型默认值说明
    label string – Shown above the field and announced by screen readers.
    value string – The text. Keep it in state and pass it back.
    onChangeText (value: string) => void – Called with the new text on every edit.
    variant? TextFieldVariant 'outlined' Draw an outline, a bottom line, or a tinted box around the input.
    minHeight? number – Minimum height of the whole labelled field. Additional space is given to the input.
    placeholder? string – Grey hint inside the empty field. Not a replacement for the label.
    helper? string – One line of help under the field. Hidden while there is an error.
    error? string – The error message. The field turns red while it is set; clear it when the user edits.
    disabled? boolean false Grey and read-only.
    maxLength? number – The most characters allowed. Shows a count under the field.
    secureTextEntry? boolean false Hides the text and adds a Show / Hide button.
    autoFocus? boolean false Focus the field and bring up the keyboard when the screen opens.
    inputProps? Omit<TextInputProps, 'value' | 'onChangeText' | 'editable' | 'maxLength' | 'secureTextEntry' | 'autoFocus' | 'placeholder'> – Keyboard, autofill and return key settings, passed to the TextInput: keyboardType, autoComplete, returnKeyType…
    ref? Ref<ComponentRef<typeof TextInput>> – The TextInput, to focus() or blur() it.

    用在这些流程里