Files
o2o-weather/mobile/src/components/saju/pick.tsx
T

132 lines
6.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useState } from 'react';
import { Pressable, View } from 'react-native';
import { useSoft } from '@/components/sheets/parts';
import { Text } from '@/components/text';
/** 고르는 칸: 누르면 아래에 칩이 펼쳐진다 (웹 input·select 대신. 숫자 자판 없이 눌러서 고른다) */
export function Box({ label, value, placeholder, open, onPress, disabled, flex = 1 }: { label: string; value: string | null; placeholder: string; open: boolean; onPress: () => void; disabled?: boolean; flex?: number }) {
const soft = useSoft();
return (
<Pressable
onPress={onPress}
disabled={disabled}
accessibilityRole="button"
accessibilityLabel={`${label} ${value ?? placeholder}`}
accessibilityState={{ expanded: open, disabled }}
className={`min-h-[48px] min-w-0 flex-row items-center justify-center gap-1 rounded-2xl px-2 py-2.5 ${disabled ? 'opacity-45' : ''}`}
style={{ flex, backgroundColor: soft.field, borderWidth: 1.5, borderColor: open ? '#ff7a45' : soft.fieldBorder }}
>
<Text numberOfLines={1} className={`text-[17px] font-bold ${value != null ? 'text-ink' : 'text-ink3'}`}>
{value ?? placeholder}
</Text>
<Text className="text-[11px] text-ink3">{open ? '▲' : '▼'}</Text>
</Pressable>
);
}
/** 칩 여러 개 중 하나 고르기 */
export function ChipGrid<K extends string | number>({ items, value, onPick, label, wide }: { items: [K, string][]; value: K | null; onPick: (k: K) => void; label: string; wide?: boolean }) {
const soft = useSoft();
return (
<View accessibilityRole="radiogroup" accessibilityLabel={label} className="flex-row flex-wrap gap-1.5 rounded-2xl p-2" style={{ backgroundColor: soft.soft }}>
{items.map(([k, word]) => {
const on = value === k;
return (
<Pressable key={String(k)} onPress={() => onPick(k)} accessibilityRole="radio" accessibilityState={{ checked: on }} className={`min-h-[40px] items-center justify-center rounded-xl px-1 ${wide ? 'w-[64px]' : 'w-[44px]'} ${on ? 'bg-orange' : 'bg-card'}`}>
<Text className={`text-[14px] font-bold ${on ? 'text-white' : 'text-ink'}`}>{word}</Text>
</Pressable>
);
})}
</View>
);
}
/** 연도: 먼저 10년 단위를 고르고 그 안에서 연도 */
function YearGrid({ value, onPick, label }: { value: number | null; onPick: (y: number) => void; label: string }) {
const thisYear = new Date().getFullYear();
const [decade, setDecade] = useState(value != null ? Math.floor(value / 10) * 10 : 1990);
const decades: [number, string][] = [];
for (let d = 1900; d <= thisYear; d += 10) decades.push([d, `${String(d).slice(2)}년대`]);
const years: [number, string][] = [];
for (let y = decade; y < decade + 10 && y <= thisYear; y++) years.push([y, String(y)]);
return (
<View className="gap-1.5">
<ChipGrid label={`${label} 10년 단위`} items={decades} value={decade} onPick={setDecade} wide />
<ChipGrid label={label} items={years} value={value} onPick={onPick} wide />
</View>
);
}
const MONTHS: [number, string][] = Array.from({ length: 12 }, (_, i) => [i + 1, `${i + 1}월`]);
const daysIn = (y: number | null, m: number | null) => (m == null ? 31 : new Date(y ?? 2000, m, 0).getDate());
export type Ymd = { y: number | null; m: number | null; d: number | null };
/** 생년월일(양력) 고르기: 연도 → 월 → 일 순서로 이어서 펼친다 */
export function DatePick({ value, onChange, label = '' }: { value: Ymd; onChange: (v: Ymd) => void; label?: string }) {
const [open, setOpen] = useState<'y' | 'm' | 'd' | null>(null);
const toggle = (k: 'y' | 'm' | 'd') => setOpen((o) => (o === k ? null : k));
const days: [number, string][] = Array.from({ length: daysIn(value.y, value.m) }, (_, i) => [i + 1, String(i + 1)]);
return (
<View className="gap-1.5">
<View className="flex-row gap-1.5">
<Box label={`${label}년`} value={value.y != null ? `${value.y}년` : null} placeholder="연도" open={open === 'y'} onPress={() => toggle('y')} flex={1.5} />
<Box label={`${label}월`} value={value.m != null ? `${value.m}월` : null} placeholder="월" open={open === 'm'} onPress={() => toggle('m')} />
<Box label={`${label}일`} value={value.d != null ? `${value.d}일` : null} placeholder="일" open={open === 'd'} onPress={() => toggle('d')} />
</View>
{open === 'y' ? (
<YearGrid
label={`${label}년`}
value={value.y}
onPick={(y) => {
onChange({ ...value, y });
setOpen(value.m == null ? 'm' : null);
}}
/>
) : open === 'm' ? (
<ChipGrid
label={`${label}월`}
items={MONTHS}
value={value.m}
onPick={(m) => {
onChange({ ...value, m });
setOpen(value.d == null ? 'd' : null);
}}
wide
/>
) : open === 'd' ? (
<ChipGrid
label={`${label}일`}
items={days}
value={value.d}
onPick={(d) => {
onChange({ ...value, d });
setOpen(null);
}}
/>
) : null}
</View>
);
}
/** 숫자: −/+ 버튼 (분) */
export function Stepper({ value, onChange, min, max, label, unit, disabled }: { value: number; onChange: (v: number) => void; min: number; max: number; label: string; unit: string; disabled?: boolean }) {
const soft = useSoft();
const set = (v: number) => onChange(Math.min(max, Math.max(min, v)));
return (
<View className={`min-h-[48px] flex-1 flex-row items-center justify-between rounded-2xl ${disabled ? 'opacity-45' : ''}`} style={{ backgroundColor: soft.field, borderWidth: 1.5, borderColor: soft.fieldBorder }} accessibilityLabel={label}>
<Pressable onPress={() => set(value - 1)} onLongPress={() => set(value - 10)} disabled={disabled || value <= min} hitSlop={4} accessibilityLabel={`${label} 내리기`} className="px-3.5 py-2">
<Text className="text-[18px] font-extrabold text-ink2">−</Text>
</Pressable>
<Text className="text-[17px] font-bold text-ink">
{value}
{unit}
</Text>
<Pressable onPress={() => set(value + 1)} onLongPress={() => set(value + 10)} disabled={disabled || value >= max} hitSlop={4} accessibilityLabel={`${label} 올리기`} className="px-3.5 py-2">
<Text className="text-[18px] font-extrabold text-ink2">+</Text>
</Pressable>
</View>
);
}