屏幕
配色方案
明暗
圆角
颜色
所有状态
- 何时出现
- 行为规则
把这 2 个文件放进 components/mobilens/。只依赖 react-native(已用 Expo SDK 58 检查)。
import { useState, type ComponentRef, type ReactNode, type Ref } from 'react';
import { Platform, StyleSheet, Text, TextInput, View, type TextStyle } from 'react-native';
import { colors, radius } from './theme';
export type TextareaProps = {
/** Visible label, also used as the accessible name of the native text area. */
label: string;
/** The current text. Line breaks are preserved. */
value: string;
/** Receives edits and pasted text, including line breaks. */
onChangeText: (text: string) => void;
/** Called once editing ends; the Return key still inserts a new line. */
onEndEditing?: () => void;
/** Example shown while the text area is empty. */
placeholder?: string;
/** Help shown under the text area. */
helper?: string;
/** Validation message, shown in place of help text. */
error?: string;
/** Makes the field read-only and dims it. */
disabled?: boolean;
/** Native character limit; shows the length and limit below the input. */
maxLength?: number;
/** Focus the text area when it mounts. */
autoFocus?: boolean;
/** Native input handle, for calling focus or blur. */
ref?: Ref<ComponentRef<typeof TextInput>>;
};
export function Textarea({
label, value, onChangeText, onEndEditing, placeholder, helper, error, disabled = false, maxLength, autoFocus = false, ref, copyTestIDs,
}: TextareaProps & Pick<FrameProps, 'copyTestIDs'>) {
const [focused, setFocused] = useState(false);
return (
<TextareaFrame label={label} copyTestIDs={copyTestIDs} value={value} placeholder={placeholder} helper={helper} error={error}
disabled={disabled} maxLength={maxLength} focused={focused && !disabled}>
<TextInput ref={ref} value={value} onChangeText={onChangeText} multiline submitBehavior="newline"
editable={!disabled} autoFocus={autoFocus} maxLength={maxLength} textAlignVertical="top"
onFocus={() => setFocused(true)} onBlur={() => { setFocused(false); onEndEditing?.(); }}
accessibilityLabel={label} accessibilityHint={error || helper} accessibilityState={{ disabled }}
aria-invalid={!!error} placeholder={placeholder} placeholderTextColor={colors.onSurfaceVariant}
underlineColorAndroid="transparent" selectionColor={colors.primary}
style={[styles.content, styles.input, noWebRing, disabled && styles.muted]} />
</TextareaFrame>
);
}
type FrameProps = Pick<TextareaProps, 'label' | 'value' | 'helper' | 'error' | 'disabled' | 'maxLength' | 'placeholder'> & {
copyTestIDs?: { label?: string; helper?: string };
focused?: boolean;
children?: ReactNode;
};
/** Shared rendering for the editable field and its read-only preview. */
export function TextareaFrame({ label, copyTestIDs, value, helper, error, placeholder, disabled, maxLength, focused, children }: FrameProps) {
return (
<View style={styles.root}>
<Text testID={copyTestIDs?.label} style={styles.label}>{label}</Text>
<View style={[styles.frame, focused && styles.focus, !!error && styles.invalid, disabled && styles.disabled]}>
{children ?? <Text style={[styles.content, (!value || disabled) && styles.muted]}>{value || placeholder}</Text>}
</View>
{(helper || error || maxLength !== undefined) && (
<View style={styles.foot}>
<Text testID={!error && helper ? copyTestIDs?.helper : undefined} accessibilityLiveRegion={error ? 'polite' : 'none'} style={[styles.note, !!error && styles.error]}>{error || helper}</Text>
{maxLength !== undefined && <Text style={[styles.counter, value.length >= maxLength && styles.limit]}>{value.length}/{maxLength}</Text>}
</View>
)}
</View>
);
}
const noWebRing = Platform.OS === 'web' ? ({ outlineStyle: 'none', resize: 'none' } as unknown as TextStyle) : undefined;
const styles = StyleSheet.create({
root: { gap: 8 },
label: { color: colors.onSurface, fontSize: 15, fontWeight: '600' },
frame: { minHeight: 142, borderWidth: 1, borderColor: colors.outline, borderRadius: radius.md, padding: 13, backgroundColor: colors.surface },
focus: { borderWidth: 2, borderColor: colors.primary, padding: 12 },
invalid: { borderWidth: 2, borderColor: colors.error, padding: 12 },
disabled: { borderColor: colors.outlineVariant, backgroundColor: colors.surfaceVariant },
content: { color: colors.onSurface, fontSize: 16, lineHeight: 23 },
input: { minHeight: 114, padding: 0 },
foot: { flexDirection: 'row', gap: 12, alignItems: 'flex-start' },
note: { flex: 1, color: colors.onSurfaceVariant, fontSize: 13, lineHeight: 18 },
muted: { color: colors.onSurfaceVariant },
error: { color: colors.error },
counter: { color: colors.onSurfaceVariant, fontSize: 13, fontVariant: ['tabular-nums'] },
limit: { color: colors.onSurface, fontWeight: '600' },
});
import { useState, type ComponentRef, type ReactNode, type Ref } from 'react';
import { Platform, StyleSheet, Text, TextInput, View, type TextStyle } from 'react-native';
import { colors, radius } from './theme';
export type TextareaProps = {
/** Visible label, also used as the accessible name of the native text area. */
label: string;
/** The current text. Line breaks are preserved. */
value: string;
/** Receives edits and pasted text, including line breaks. */
onChangeText: (text: string) => void;
/** Called once editing ends; the Return key still inserts a new line. */
onEndEditing?: () => void;
/** Example shown while the text area is empty. */
placeholder?: string;
/** Help shown under the text area. */
helper?: string;
/** Validation message, shown in place of help text. */
error?: string;
/** Makes the field read-only and dims it. */
disabled?: boolean;
/** Native character limit; shows the length and limit below the input. */
maxLength?: number;
/** Focus the text area when it mounts. */
autoFocus?: boolean;
/** Native input handle, for calling focus or blur. */
ref?: Ref<ComponentRef<typeof TextInput>>;
};
export function Textarea({
label, value, onChangeText, onEndEditing, placeholder, helper, error, disabled = false, maxLength, autoFocus = false, ref, copyTestIDs,
}: TextareaProps & Pick<FrameProps, 'copyTestIDs'>) {
const [focused, setFocused] = useState(false);
return (
<TextareaFrame label={label} copyTestIDs={copyTestIDs} value={value} placeholder={placeholder} helper={helper} error={error}
disabled={disabled} maxLength={maxLength} focused={focused && !disabled}>
<TextInput ref={ref} value={value} onChangeText={onChangeText} multiline submitBehavior="newline"
editable={!disabled} autoFocus={autoFocus} maxLength={maxLength} textAlignVertical="top"
onFocus={() => setFocused(true)} onBlur={() => { setFocused(false); onEndEditing?.(); }}
accessibilityLabel={label} accessibilityHint={error || helper} accessibilityState={{ disabled }}
aria-invalid={!!error} placeholder={placeholder} placeholderTextColor={colors.onSurfaceVariant}
underlineColorAndroid="transparent" selectionColor={colors.primary}
style={[styles.content, styles.input, noWebRing, disabled && styles.muted]} />
</TextareaFrame>
);
}
type FrameProps = Pick<TextareaProps, 'label' | 'value' | 'helper' | 'error' | 'disabled' | 'maxLength' | 'placeholder'> & {
copyTestIDs?: { label?: string; helper?: string };
focused?: boolean;
children?: ReactNode;
};
/** Shared rendering for the editable field and its read-only preview. */
export function TextareaFrame({ label, copyTestIDs, value, helper, error, placeholder, disabled, maxLength, focused, children }: FrameProps) {
return (
<View style={styles.root}>
<Text testID={copyTestIDs?.label} style={styles.label}>{label}</Text>
<View style={[styles.frame, focused && styles.focus, !!error && styles.invalid, disabled && styles.disabled]}>
{children ?? <Text style={[styles.content, (!value || disabled) && styles.muted]}>{value || placeholder}</Text>}
</View>
{(helper || error || maxLength !== undefined) && (
<View style={styles.foot}>
<Text testID={!error && helper ? copyTestIDs?.helper : undefined} accessibilityLiveRegion={error ? 'polite' : 'none'} style={[styles.note, !!error && styles.error]}>{error || helper}</Text>
{maxLength !== undefined && <Text style={[styles.counter, value.length >= maxLength && styles.limit]}>{value.length}/{maxLength}</Text>}
</View>
)}
</View>
);
}
const noWebRing = Platform.OS === 'web' ? ({ outlineStyle: 'none', resize: 'none' } as unknown as TextStyle) : undefined;
const styles = StyleSheet.create({
root: { gap: 8 },
label: { color: colors.onSurface, fontSize: 15, fontWeight: '600' },
frame: { minHeight: 142, borderWidth: 1, borderColor: colors.outline, borderRadius: radius.md, padding: 13, backgroundColor: colors.surface },
focus: { borderWidth: 2, borderColor: colors.primary, padding: 12 },
invalid: { borderWidth: 2, borderColor: colors.error, padding: 12 },
disabled: { borderColor: colors.outlineVariant, backgroundColor: colors.surfaceVariant },
content: { color: colors.onSurface, fontSize: 16, lineHeight: 23 },
input: { minHeight: 114, padding: 0 },
foot: { flexDirection: 'row', gap: 12, alignItems: 'flex-start' },
note: { flex: 1, color: colors.onSurfaceVariant, fontSize: 13, lineHeight: 18 },
muted: { color: colors.onSurfaceVariant },
error: { color: colors.error },
counter: { color: colors.onSurfaceVariant, fontSize: 13, fontVariant: ['tabular-nums'] },
limit: { color: colors.onSurface, fontWeight: '600' },
});
// Theme: Emerald, light, default corners. Shared by every Mobilens component; edit the values to match your app.
export const colors = {
background: '#fafafa',
surface: '#ffffff',
surfaceVariant: '#f4f4f5',
onBackground: '#18181b',
onSurface: '#18181b',
onSurfaceVariant: '#6b6b74',
outline: '#8a8a93',
outlineVariant: '#e4e4e7',
error: '#ba1a1a',
success: '#296b2a',
warning: '#b45309',
shadow: '#000000',
scrim: 'rgba(0,0,0,0.32)',
primary: '#006c50',
onPrimary: '#ffffff',
primaryContainer: '#82f8cb',
onPrimaryContainer: '#002116',
secondary: '#3e6374',
onSecondary: '#ffffff',
secondaryContainer: '#d8dfe2',
onSecondaryContainer: '#1c2d34',
tertiary: '#1f6862',
onTertiary: '#ffffff',
tertiaryContainer: '#d3e0df',
onTertiaryContainer: '#0e2f2c',
onError: '#ffffff',
onSuccess: '#ffffff',
onWarning: '#ffffff',
} as const;
export const radius = {
sm: 6,
md: 12,
lg: 20,
} as const;
// Theme: Emerald, light, default corners. Shared by every Mobilens component; edit the values to match your app.
export const colors = {
background: '#fafafa',
surface: '#ffffff',
surfaceVariant: '#f4f4f5',
onBackground: '#18181b',
onSurface: '#18181b',
onSurfaceVariant: '#6b6b74',
outline: '#8a8a93',
outlineVariant: '#e4e4e7',
error: '#ba1a1a',
success: '#296b2a',
warning: '#b45309',
shadow: '#000000',
scrim: 'rgba(0,0,0,0.32)',
primary: '#006c50',
onPrimary: '#ffffff',
primaryContainer: '#82f8cb',
onPrimaryContainer: '#002116',
secondary: '#3e6374',
onSecondary: '#ffffff',
secondaryContainer: '#d8dfe2',
onSecondaryContainer: '#1c2d34',
tertiary: '#1f6862',
onTertiary: '#ffffff',
tertiaryContainer: '#d3e0df',
onTertiaryContainer: '#0e2f2c',
onError: '#ffffff',
onSuccess: '#ffffff',
onWarning: '#ffffff',
} as const;
export const radius = {
sm: 6,
md: 12,
lg: 20,
} as const;
用法
import { useState } from 'react';
import { Button, View } from 'react-native';
import { Textarea } from '@/components/mobilens/Textarea';
export function DeliveryNoteForm({ onSave }: { onSave: (note: string) => void }) {
const [note, setNote] = useState('');
const [error, setError] = useState<string>();
function save() {
if (!note.trim()) {
setError('Enter a delivery note.');
return;
}
onSave(note);
}
return (
<View>
<Textarea
label="Delivery note"
value={note}
onChangeText={(next) => {
setNote(next);
setError(undefined);
}}
placeholder="Where should we leave your package?"
error={error}
maxLength={240}
/>
<Button title="Save note" onPress={save} />
</View>
);
}
import { useState } from 'react';
import { Button, View } from 'react-native';
import { Textarea } from '@/components/mobilens/Textarea';
export function DeliveryNoteForm({ onSave }: { onSave: (note: string) => void }) {
const [note, setNote] = useState('');
const [error, setError] = useState<string>();
function save() {
if (!note.trim()) {
setError('Enter a delivery note.');
return;
}
onSave(note);
}
return (
<View>
<Textarea
label="Delivery note"
value={note}
onChangeText={(next) => {
setNote(next);
setError(undefined);
}}
placeholder="Where should we leave your package?"
error={error}
maxLength={240}
/>
<Button title="Save note" onPress={save} />
</View>
);
}
API
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| label | string | – | Visible label, also used as the accessible name of the native text area. |
| value | string | – | The current text. Line breaks are preserved. |
| onChangeText | (text: string) => void | – | Receives edits and pasted text, including line breaks. |
| onEndEditing? | () => void | – | Called once editing ends; the Return key still inserts a new line. |
| placeholder? | string | – | Example shown while the text area is empty. |
| helper? | string | – | Help shown under the text area. |
| error? | string | – | Validation message, shown in place of help text. |
| disabled? | boolean | false | Makes the field read-only and dims it. |
| maxLength? | number | – | Native character limit; shows the length and limit below the input. |
| autoFocus? | boolean | false | Focus the text area when it mounts. |
| ref? | Ref<ComponentRef<typeof TextInput>> | – | Native input handle, for calling focus or blur. |
Add the "Textarea" control to this Expo or React Native project. Create the files below with exactly this content. Do not restyle or rewrite them.
FILES
Put them in components/mobilens/. theme.ts is shared by every Mobilens component: if it already exists, keep one copy, and skip any other file that is already there with the same content. If the project has no "@/..." import alias, change those imports to relative paths.
--- components/mobilens/theme.ts ---
// Theme: Emerald, light, default corners. Shared by every Mobilens component; edit the values to match your app.
export const colors = {
background: '#fafafa',
surface: '#ffffff',
surfaceVariant: '#f4f4f5',
onBackground: '#18181b',
onSurface: '#18181b',
onSurfaceVariant: '#6b6b74',
outline: '#8a8a93',
outlineVariant: '#e4e4e7',
error: '#ba1a1a',
success: '#296b2a',
warning: '#b45309',
shadow: '#000000',
scrim: 'rgba(0,0,0,0.32)',
primary: '#006c50',
onPrimary: '#ffffff',
primaryContainer: '#82f8cb',
onPrimaryContainer: '#002116',
secondary: '#3e6374',
onSecondary: '#ffffff',
secondaryContainer: '#d8dfe2',
onSecondaryContainer: '#1c2d34',
tertiary: '#1f6862',
onTertiary: '#ffffff',
tertiaryContainer: '#d3e0df',
onTertiaryContainer: '#0e2f2c',
onError: '#ffffff',
onSuccess: '#ffffff',
onWarning: '#ffffff',
} as const;
export const radius = {
sm: 6,
md: 12,
lg: 20,
} as const;
--- end of theme.ts ---
--- components/mobilens/Textarea.tsx ---
import { useState, type ComponentRef, type ReactNode, type Ref } from 'react';
import { Platform, StyleSheet, Text, TextInput, View, type TextStyle } from 'react-native';
import { colors, radius } from './theme';
export type TextareaProps = {
/** Visible label, also used as the accessible name of the native text area. */
label: string;
/** The current text. Line breaks are preserved. */
value: string;
/** Receives edits and pasted text, including line breaks. */
onChangeText: (text: string) => void;
/** Called once editing ends; the Return key still inserts a new line. */
onEndEditing?: () => void;
/** Example shown while the text area is empty. */
placeholder?: string;
/** Help shown under the text area. */
helper?: string;
/** Validation message, shown in place of help text. */
error?: string;
/** Makes the field read-only and dims it. */
disabled?: boolean;
/** Native character limit; shows the length and limit below the input. */
maxLength?: number;
/** Focus the text area when it mounts. */
autoFocus?: boolean;
/** Native input handle, for calling focus or blur. */
ref?: Ref<ComponentRef<typeof TextInput>>;
};
export function Textarea({
label, value, onChangeText, onEndEditing, placeholder, helper, error, disabled = false, maxLength, autoFocus = false, ref, copyTestIDs,
}: TextareaProps & Pick<FrameProps, 'copyTestIDs'>) {
const [focused, setFocused] = useState(false);
return (
<TextareaFrame label={label} copyTestIDs={copyTestIDs} value={value} placeholder={placeholder} helper={helper} error={error}
disabled={disabled} maxLength={maxLength} focused={focused && !disabled}>
<TextInput ref={ref} value={value} onChangeText={onChangeText} multiline submitBehavior="newline"
editable={!disabled} autoFocus={autoFocus} maxLength={maxLength} textAlignVertical="top"
onFocus={() => setFocused(true)} onBlur={() => { setFocused(false); onEndEditing?.(); }}
accessibilityLabel={label} accessibilityHint={error || helper} accessibilityState={{ disabled }}
aria-invalid={!!error} placeholder={placeholder} placeholderTextColor={colors.onSurfaceVariant}
underlineColorAndroid="transparent" selectionColor={colors.primary}
style={[styles.content, styles.input, noWebRing, disabled && styles.muted]} />
</TextareaFrame>
);
}
type FrameProps = Pick<TextareaProps, 'label' | 'value' | 'helper' | 'error' | 'disabled' | 'maxLength' | 'placeholder'> & {
copyTestIDs?: { label?: string; helper?: string };
focused?: boolean;
children?: ReactNode;
};
/** Shared rendering for the editable field and its read-only preview. */
export function TextareaFrame({ label, copyTestIDs, value, helper, error, placeholder, disabled, maxLength, focused, children }: FrameProps) {
return (
<View style={styles.root}>
<Text testID={copyTestIDs?.label} style={styles.label}>{label}</Text>
<View style={[styles.frame, focused && styles.focus, !!error && styles.invalid, disabled && styles.disabled]}>
{children ?? <Text style={[styles.content, (!value || disabled) && styles.muted]}>{value || placeholder}</Text>}
</View>
{(helper || error || maxLength !== undefined) && (
<View style={styles.foot}>
<Text testID={!error && helper ? copyTestIDs?.helper : undefined} accessibilityLiveRegion={error ? 'polite' : 'none'} style={[styles.note, !!error && styles.error]}>{error || helper}</Text>
{maxLength !== undefined && <Text style={[styles.counter, value.length >= maxLength && styles.limit]}>{value.length}/{maxLength}</Text>}
</View>
)}
</View>
);
}
const noWebRing = Platform.OS === 'web' ? ({ outlineStyle: 'none', resize: 'none' } as unknown as TextStyle) : undefined;
const styles = StyleSheet.create({
root: { gap: 8 },
label: { color: colors.onSurface, fontSize: 15, fontWeight: '600' },
frame: { minHeight: 142, borderWidth: 1, borderColor: colors.outline, borderRadius: radius.md, padding: 13, backgroundColor: colors.surface },
focus: { borderWidth: 2, borderColor: colors.primary, padding: 12 },
invalid: { borderWidth: 2, borderColor: colors.error, padding: 12 },
disabled: { borderColor: colors.outlineVariant, backgroundColor: colors.surfaceVariant },
content: { color: colors.onSurface, fontSize: 16, lineHeight: 23 },
input: { minHeight: 114, padding: 0 },
foot: { flexDirection: 'row', gap: 12, alignItems: 'flex-start' },
note: { flex: 1, color: colors.onSurfaceVariant, fontSize: 13, lineHeight: 18 },
muted: { color: colors.onSurfaceVariant },
error: { color: colors.error },
counter: { color: colors.onSurfaceVariant, fontSize: 13, fontVariant: ['tabular-nums'] },
limit: { color: colors.onSurface, fontWeight: '600' },
});
--- end of Textarea.tsx ---
HOW TO USE IT
import { useState } from 'react';
import { Button, View } from 'react-native';
import { Textarea } from '@/components/mobilens/Textarea';
export function DeliveryNoteForm({ onSave }: { onSave: (note: string) => void }) {
const [note, setNote] = useState('');
const [error, setError] = useState<string>();
function save() {
if (!note.trim()) {
setError('Enter a delivery note.');
return;
}
onSave(note);
}
return (
<View>
<Textarea
label="Delivery note"
value={note}
onChangeText={(next) => {
setNote(next);
setError(undefined);
}}
placeholder="Where should we leave your package?"
error={error}
maxLength={240}
/>
<Button title="Save note" onPress={save} />
</View>
);
}
STATES: all 6 must work in the screen where you use it
- Empty: when the note has no text.. The field label remains visible above the placeholder.
- Writing: when the user taps the note to edit it.. Return inserts a line break. Save is a separate action.
- Multiline note: when the note contains typed or pasted text.. Preserve line breaks and update the character count as the note changes.
- At the limit: when the note reaches its character limit.. Enforce maxLength for typing and paste, and allow deletions.
- Empty submission: when the user tries to save a blank note.. Display the error under the field and keep the note editable.
- Saved note: when the screen displays the saved note in read-only mode.. Keep the text and line breaks visible while editing is disabled.
ACCESSIBILITY
- Use the visible label as the accessible name of the multiline input.
- Describe validation errors in text and expose the disabled state to assistive technology.
WHEN YOU FINISH
List every state and rule above and say how you verified each one.