Skip to content

Commit

Permalink
Merge pull request #2057 from quadratichq/fix-custom-dates
Browse files Browse the repository at this point in the history
Fix bugs with custom dates
  • Loading branch information
davidkircos authored Nov 20, 2024
2 parents 33fcdc9 + eeb808a commit 51156b2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
12 changes: 10 additions & 2 deletions quadratic-client/src/app/ui/components/DateFormat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,17 @@ export const DateFormat = (props: DateFormatProps) => {
<ValidationInput
ref={ref}
placeholder="%d, %B %Y"
onChange={changeCustom}
onInput={changeCustom}
value={custom ?? ''}
onEnter={closeMenu}
onEnter={(value) => {
changeCustom(value);
apply();
closeMenu();
}}
onKeyDown={(e) => {
// ensures that the menu does not close when the user presses keys like arrow
e.stopPropagation();
}}
/>
<p className="text-xs text-muted-foreground ">
Learn custom date and time formatting{' '}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Input } from '@/shared/shadcn/ui/input';
import { cn } from '@/shared/shadcn/utils';
import { FocusEvent, forwardRef, Ref, useCallback, useEffect, useRef } from 'react';
import { FocusEvent, forwardRef, KeyboardEvent, Ref, useCallback, useEffect, useRef } from 'react';

interface ValidationInputProps {
className?: string;
Expand All @@ -15,8 +15,8 @@ interface ValidationInputProps {

// used to update whenever the input is changed (ie, a character is changes within the input box)
onInput?: (value: string) => void;

onEnter?: () => void;
onKeyDown?: (e: KeyboardEvent<HTMLInputElement>) => void;
onEnter?: (value: string) => void;

footer?: string | JSX.Element;
height?: string;
Expand Down Expand Up @@ -46,6 +46,7 @@ export const ValidationInput = forwardRef((props: ValidationInputProps, ref: Ref
readOnly,
type,
clear,
onKeyDown,
} = props;

const parentRef = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -105,10 +106,12 @@ export const ValidationInput = forwardRef((props: ValidationInputProps, ref: Ref
}

// timeout is needed to ensure the state updates before the onEnter function is called
setTimeout(onEnter, 0);
const savedValue = e.currentTarget.value;
setTimeout(() => onEnter?.(savedValue), 0);
e.preventDefault();
e.stopPropagation();
}
onKeyDown?.(e);
}}
/>
</div>
Expand Down
6 changes: 5 additions & 1 deletion quadratic-core/src/date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ fn find_items_date_start(items: &[Item<'_>]) -> Option<usize> {
/// Converts a NaiveDateTime to a date and time string using a strftime format string.
pub fn date_time_to_date_time_string(date_time: NaiveDateTime, format: Option<String>) -> String {
let format = format.map_or(DEFAULT_DATE_TIME_FORMAT.to_string(), |f| f);
date_time.format(&format).to_string()
let strftime_items = StrftimeItems::new(&format);
let Ok(items) = strftime_items.parse() else {
return date_time.format(DEFAULT_DATE_TIME_FORMAT).to_string();

Check warning on line 102 in quadratic-core/src/date_time.rs

View check run for this annotation

Codecov / codecov/patch

quadratic-core/src/date_time.rs#L102

Added line #L102 was not covered by tests
};
date_time.format_with_items(items.iter()).to_string()
}

/// Converts a NaiveDateTime to a date-only string using a strftime format string.
Expand Down
5 changes: 5 additions & 0 deletions quadratic-rust-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ impl SheetOffsetsWasm {
SheetOffsets::default()
}
}

#[wasm_bindgen(start)]
pub fn start() {
console_error_panic_hook::set_once();
}

0 comments on commit 51156b2

Please sign in to comment.