Soundorv0.13.0

TextInput

A single-line text input, controlled or uncontrolled.

TextInput is a single-line input that edits itself: typing, selection with the mouse and Shift, word-wise moves and deletes, Home and End, and copy, cut and paste through the system clipboard.

import { Pressable, Text, TextInput, View } from '@soundor/react';
import { useState } from 'react';

// A controlled TextInput: its text is state, and Enter submits it.
export default function PresetName() {
  const [name, setName] = useState('');
  const [presets, setPresets] = useState(['Init']);
  const save = () => {
    if (name.trim() === '') return;
    setPresets((saved) => [...saved, name.trim()]);
    setName('');
  };

  return (
    <View style={{ width: 280, gap: 10 }}>
      <View style={{ flexDirection: 'row', gap: 8 }}>
        <TextInput
          accessibilityLabel="Preset name"
          placeholder="Preset name"
          value={name}
          onChangeText={setName}
          onSubmitEditing={save}
          style={{
            flex: 1,
            padding: 8,
            borderRadius: 6,
            backgroundColor: '#24262c',
            color: '#e6e7ea',
            fontSize: 14,
          }}
        />
        <Pressable
          onPress={save}
          disabled={name.trim() === ''}
          style={({ pressed }) => ({
            justifyContent: 'center',
            paddingHorizontal: 12,
            borderRadius: 6,
            backgroundColor: pressed ? '#2a5fd0' : '#3a7bfd',
            opacity: name.trim() === '' ? 0.5 : 1,
          })}
        >
          <Text style={{ color: 'white' }}>Save</Text>
        </Pressable>
      </View>
      {presets.map((preset, index) => (
        <Text key={index} style={{ color: '#e6e7ea' }}>
          {index + 1}. {preset}
        </Text>
      ))}
    </View>
  );
}
text-input.tsxSource
  • Controlled: value and onChangeText keep the text in your state. Uncontrolled: defaultValue gives the initial text, and the input keeps it.
  • Events: onChangeText after every edit, with the text; onSubmitEditing on Enter; onChange when the text is committed (Enter, or losing focus).
  • Focus: pressing it focuses it. ref.current.focus() does too.
  • Its own editing is the default action of its events, so a listener's preventDefault() (on onKeyDown, say) takes over that key.
  • To assistive technology it is a text field with its current text and placeholder. It offers activate (start editing), focus and setValue.

Props

Prop

Type

TextInput also takes the event props and the accessibility props.

On this page