屏幕
配色方案
明暗
圆角
颜色
所有状态
- 何时出现
- 行为规则
把这 2 个文件放进 components/mobilens/。只依赖 react-native(已用 Expo SDK 58 检查)。
import { Pressable, StyleSheet, Text, View } from 'react-native';
import { colors, radius } from './theme';
export type StepperProps = {
/** Controlled numeric amount. */
value: number;
/** Receives the bounded amount after a press. */
onValueChange: (value: number) => void;
/** Lowest allowed amount and origin for the step intervals. */
min?: number;
/** Highest allowed amount, including a partial last step. */
max?: number;
/** Positive interval between adjacent values. */
step?: number;
/** Accessible name of the minus button, including what it adjusts. */
decreaseLabel: string;
/** Accessible name of the plus button, including what it adjusts. */
increaseLabel: string;
/** Prevent both changes while preserving the current amount. */
disabled?: boolean;
};
function adjust(value: number, direction: number, min: number, max: number, step: number) {
const offset = (value - min) / step;
// Round minor floating-point noise before finding the next interval.
const index = Number(offset.toPrecision(15));
const nextIndex = direction > 0 ? Math.floor(index) + 1 : Math.ceil(index) - 1;
const result = Number((min + nextIndex * step).toPrecision(15));
return Math.max(min, Math.min(max, result));
}
export function Stepper({
value, onValueChange, min = 0, max = 99, step = 1, decreaseLabel, increaseLabel, disabled = false,
}: StepperProps) {
const current = Number.isFinite(value) ? Math.max(min, Math.min(max, value)) : min;
const locked = disabled || !Number.isFinite(min) || !Number.isFinite(max) || max < min || !Number.isFinite(step) || step <= 0;
const change = (direction: number) => onValueChange(adjust(current, direction, min, max, step));
return (
<View style={[styles.group, disabled && styles.disabled]}>
<StepButton label={decreaseLabel} sign="−" disabled={locked || current <= min} onPress={() => change(-1)} />
<Text style={styles.value} accessibilityLiveRegion="polite">{current}</Text>
<StepButton label={increaseLabel} sign="+" disabled={locked || current >= max} onPress={() => change(1)} />
</View>
);
}
type StepButtonProps = { label: string; sign: string; disabled: boolean; onPress: () => void };
function StepButton({ label, sign, disabled, onPress }: StepButtonProps) {
return (
<Pressable
accessibilityRole="button"
accessibilityLabel={label}
accessibilityState={{ disabled }}
disabled={disabled}
onPress={onPress}
style={({ pressed }) => [styles.button, pressed && styles.pressed]}
>
<Text style={[styles.sign, disabled && styles.dim]}>{sign}</Text>
</Pressable>
);
}
const styles = StyleSheet.create({
group: { alignSelf: 'flex-start', flexDirection: 'row', alignItems: 'center', borderRadius: radius.md, backgroundColor: colors.surfaceVariant },
disabled: { opacity: 0.65 },
button: { minWidth: 44, minHeight: 44, alignItems: 'center', justifyContent: 'center', borderRadius: radius.md },
pressed: { backgroundColor: colors.outlineVariant },
sign: { color: colors.primary, fontSize: 23, fontWeight: '500', lineHeight: 29 },
dim: { color: colors.outline },
value: { minWidth: 24, textAlign: 'center', color: colors.onSurface, fontSize: 17, fontWeight: '600', fontVariant: ['tabular-nums'] },
});
import { Pressable, StyleSheet, Text, View } from 'react-native';
import { colors, radius } from './theme';
export type StepperProps = {
/** Controlled numeric amount. */
value: number;
/** Receives the bounded amount after a press. */
onValueChange: (value: number) => void;
/** Lowest allowed amount and origin for the step intervals. */
min?: number;
/** Highest allowed amount, including a partial last step. */
max?: number;
/** Positive interval between adjacent values. */
step?: number;
/** Accessible name of the minus button, including what it adjusts. */
decreaseLabel: string;
/** Accessible name of the plus button, including what it adjusts. */
increaseLabel: string;
/** Prevent both changes while preserving the current amount. */
disabled?: boolean;
};
function adjust(value: number, direction: number, min: number, max: number, step: number) {
const offset = (value - min) / step;
// Round minor floating-point noise before finding the next interval.
const index = Number(offset.toPrecision(15));
const nextIndex = direction > 0 ? Math.floor(index) + 1 : Math.ceil(index) - 1;
const result = Number((min + nextIndex * step).toPrecision(15));
return Math.max(min, Math.min(max, result));
}
export function Stepper({
value, onValueChange, min = 0, max = 99, step = 1, decreaseLabel, increaseLabel, disabled = false,
}: StepperProps) {
const current = Number.isFinite(value) ? Math.max(min, Math.min(max, value)) : min;
const locked = disabled || !Number.isFinite(min) || !Number.isFinite(max) || max < min || !Number.isFinite(step) || step <= 0;
const change = (direction: number) => onValueChange(adjust(current, direction, min, max, step));
return (
<View style={[styles.group, disabled && styles.disabled]}>
<StepButton label={decreaseLabel} sign="−" disabled={locked || current <= min} onPress={() => change(-1)} />
<Text style={styles.value} accessibilityLiveRegion="polite">{current}</Text>
<StepButton label={increaseLabel} sign="+" disabled={locked || current >= max} onPress={() => change(1)} />
</View>
);
}
type StepButtonProps = { label: string; sign: string; disabled: boolean; onPress: () => void };
function StepButton({ label, sign, disabled, onPress }: StepButtonProps) {
return (
<Pressable
accessibilityRole="button"
accessibilityLabel={label}
accessibilityState={{ disabled }}
disabled={disabled}
onPress={onPress}
style={({ pressed }) => [styles.button, pressed && styles.pressed]}
>
<Text style={[styles.sign, disabled && styles.dim]}>{sign}</Text>
</Pressable>
);
}
const styles = StyleSheet.create({
group: { alignSelf: 'flex-start', flexDirection: 'row', alignItems: 'center', borderRadius: radius.md, backgroundColor: colors.surfaceVariant },
disabled: { opacity: 0.65 },
button: { minWidth: 44, minHeight: 44, alignItems: 'center', justifyContent: 'center', borderRadius: radius.md },
pressed: { backgroundColor: colors.outlineVariant },
sign: { color: colors.primary, fontSize: 23, fontWeight: '500', lineHeight: 29 },
dim: { color: colors.outline },
value: { minWidth: 24, textAlign: 'center', color: colors.onSurface, fontSize: 17, fontWeight: '600', fontVariant: ['tabular-nums'] },
});
// 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 { Stepper } from '@/components/mobilens/Stepper';
export function TicketQuantity() {
const [quantity, setQuantity] = useState(1);
return (
<Stepper value={quantity} onValueChange={setQuantity} min={0} max={6}
decreaseLabel="Remove one ticket" increaseLabel="Add one ticket" />
);
}
import { useState } from 'react';
import { Stepper } from '@/components/mobilens/Stepper';
export function TicketQuantity() {
const [quantity, setQuantity] = useState(1);
return (
<Stepper value={quantity} onValueChange={setQuantity} min={0} max={6}
decreaseLabel="Remove one ticket" increaseLabel="Add one ticket" />
);
}
API
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| value | number | – | Controlled numeric amount. |
| onValueChange | (value: number) => void | – | Receives the bounded amount after a press. |
| min? | number | 0 | Lowest allowed amount and origin for the step intervals. |
| max? | number | 99 | Highest allowed amount, including a partial last step. |
| step? | number | 1 | Positive interval between adjacent values. |
| decreaseLabel | string | – | Accessible name of the minus button, including what it adjusts. |
| increaseLabel | string | – | Accessible name of the plus button, including what it adjusts. |
| disabled? | boolean | false | Prevent both changes while preserving the current amount. |
Add the "Stepper" 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/Stepper.tsx ---
import { Pressable, StyleSheet, Text, View } from 'react-native';
import { colors, radius } from './theme';
export type StepperProps = {
/** Controlled numeric amount. */
value: number;
/** Receives the bounded amount after a press. */
onValueChange: (value: number) => void;
/** Lowest allowed amount and origin for the step intervals. */
min?: number;
/** Highest allowed amount, including a partial last step. */
max?: number;
/** Positive interval between adjacent values. */
step?: number;
/** Accessible name of the minus button, including what it adjusts. */
decreaseLabel: string;
/** Accessible name of the plus button, including what it adjusts. */
increaseLabel: string;
/** Prevent both changes while preserving the current amount. */
disabled?: boolean;
};
function adjust(value: number, direction: number, min: number, max: number, step: number) {
const offset = (value - min) / step;
// Round minor floating-point noise before finding the next interval.
const index = Number(offset.toPrecision(15));
const nextIndex = direction > 0 ? Math.floor(index) + 1 : Math.ceil(index) - 1;
const result = Number((min + nextIndex * step).toPrecision(15));
return Math.max(min, Math.min(max, result));
}
export function Stepper({
value, onValueChange, min = 0, max = 99, step = 1, decreaseLabel, increaseLabel, disabled = false,
}: StepperProps) {
const current = Number.isFinite(value) ? Math.max(min, Math.min(max, value)) : min;
const locked = disabled || !Number.isFinite(min) || !Number.isFinite(max) || max < min || !Number.isFinite(step) || step <= 0;
const change = (direction: number) => onValueChange(adjust(current, direction, min, max, step));
return (
<View style={[styles.group, disabled && styles.disabled]}>
<StepButton label={decreaseLabel} sign="−" disabled={locked || current <= min} onPress={() => change(-1)} />
<Text style={styles.value} accessibilityLiveRegion="polite">{current}</Text>
<StepButton label={increaseLabel} sign="+" disabled={locked || current >= max} onPress={() => change(1)} />
</View>
);
}
type StepButtonProps = { label: string; sign: string; disabled: boolean; onPress: () => void };
function StepButton({ label, sign, disabled, onPress }: StepButtonProps) {
return (
<Pressable
accessibilityRole="button"
accessibilityLabel={label}
accessibilityState={{ disabled }}
disabled={disabled}
onPress={onPress}
style={({ pressed }) => [styles.button, pressed && styles.pressed]}
>
<Text style={[styles.sign, disabled && styles.dim]}>{sign}</Text>
</Pressable>
);
}
const styles = StyleSheet.create({
group: { alignSelf: 'flex-start', flexDirection: 'row', alignItems: 'center', borderRadius: radius.md, backgroundColor: colors.surfaceVariant },
disabled: { opacity: 0.65 },
button: { minWidth: 44, minHeight: 44, alignItems: 'center', justifyContent: 'center', borderRadius: radius.md },
pressed: { backgroundColor: colors.outlineVariant },
sign: { color: colors.primary, fontSize: 23, fontWeight: '500', lineHeight: 29 },
dim: { color: colors.outline },
value: { minWidth: 24, textAlign: 'center', color: colors.onSurface, fontSize: 17, fontWeight: '600', fontVariant: ['tabular-nums'] },
});
--- end of Stepper.tsx ---
HOW TO USE IT
import { useState } from 'react';
import { Stepper } from '@/components/mobilens/Stepper';
export function TicketQuantity() {
const [quantity, setQuantity] = useState(1);
return (
<Stepper value={quantity} onValueChange={setQuantity} min={0} max={6}
decreaseLabel="Remove one ticket" increaseLabel="Add one ticket" />
);
}
STATES: all 4 must work in the screen where you use it
- Minimum: when the amount equals the lower bound.. Disable decrease while increase is available.
- Quantity selected: when the amount is between limits.. Each tap moves one interval and recalculates the ticket total.
- Maximum: when the amount reaches the upper bound.. Disable increase while decrease is available.
- Sales closed: when ticket sales are closed.. Keep both amounts visible and disable adjustment.
ACCESSIBILITY
- Give the decrease and increase buttons distinct names that identify the quantity.
- The changed value is available as text and is marked as a polite live region.
- Each action has a touch target of at least 44 points.
WHEN YOU FINISH
List every state and rule above and say how you verified each one.