diff --git a/src/App.tsx b/src/App.tsx index b861764..061dfa3 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -72,7 +72,18 @@ function App() { const [time, setTime] = useState(new Date()) const [clockStyle, setClockStyle] = useState('digital') const [clockSize, setClockSize] = useState(120) - const [is24Hour, setIs24Hour] = useState(true) + const [is24Hour, setIs24Hour] = useState(() => { + // 从 localStorage 读取,默认 24 小时制 (true) + const saved = localStorage.getItem('clock-is24Hour') + return saved === null ? true : saved === 'true' + }) + + // 切换格式时保存到 localStorage + const toggleFormat = () => { + const newValue = !is24Hour + setIs24Hour(newValue) + localStorage.setItem('clock-is24Hour', String(newValue)) + } useEffect(() => { const timer = setInterval(() => { @@ -122,9 +133,9 @@ function App() { const renderClock = () => { switch (clockStyle) { case 'flip': - return + return case 'roll': - return + return case 'digital': default: return ( @@ -133,7 +144,7 @@ function App() { size={clockSize} period={period} is24Hour={is24Hour} - onToggleFormat={() => setIs24Hour(!is24Hour)} + onToggleFormat={toggleFormat} dateInfo={dateInfo} /> ) diff --git a/src/components/FlipClock.tsx b/src/components/FlipClock.tsx index c1c1d9a..24a90b5 100644 --- a/src/components/FlipClock.tsx +++ b/src/components/FlipClock.tsx @@ -1,4 +1,3 @@ -import { useState } from 'react'; import { FlipDigit } from './FlipDigit'; import './FlipClock.css'; @@ -12,11 +11,11 @@ interface FlipClockProps { size: number; dateInfo: DateInfo; period?: string; + is24Hour: boolean; + onToggleFormat: () => void; } -export function FlipClock({ time, size, dateInfo, period }: FlipClockProps) { - const [is24Hour, setIs24Hour] = useState(true); - +export function FlipClock({ time, size, dateInfo, period, is24Hour, onToggleFormat }: FlipClockProps) { // Parse time - it's already in 12h format when period is provided const [hoursStr, minutes, seconds] = time.split(':'); const displayHours = hoursStr; @@ -29,7 +28,7 @@ export function FlipClock({ time, size, dateInfo, period }: FlipClockProps) {
diff --git a/src/components/RollClock.tsx b/src/components/RollClock.tsx index e7a4005..825503c 100644 --- a/src/components/RollClock.tsx +++ b/src/components/RollClock.tsx @@ -12,12 +12,13 @@ interface RollClockProps { size: number; dateInfo: DateInfo; period?: string; + is24Hour: boolean; + onToggleFormat: () => void; } -export function RollClock({ time, size, dateInfo, period }: RollClockProps) { +export function RollClock({ time, size, dateInfo, period, is24Hour, onToggleFormat }: RollClockProps) { // Roll direction: 'down' = roll down, 'up' = roll up const [direction, setDirection] = useState<'down' | 'up'>('down'); - const [is24Hour, setIs24Hour] = useState(true); // Parse time - it's already in 12h format when period is provided const [hoursStr, minutes, seconds] = time.split(':'); @@ -37,7 +38,7 @@ export function RollClock({ time, size, dateInfo, period }: RollClockProps) {
@@ -46,7 +47,7 @@ export function RollClock({ time, size, dateInfo, period }: RollClockProps) {
- {!is24Hour && period && ( + {period && (