From fee4a336f373b177844cdfac69637bb797e7fe25 Mon Sep 17 00:00:00 2001 From: Rishi Balakrishnan Date: Mon, 9 Nov 2026 11:04:51 -0500 Subject: [PATCH] fix(composer): keep typed places without a region and support web browsers with no date picker API A typed place on a device with no region is saved with the country ZZ, since the address lexicon requires one, so it no longer disappears from the event. Where the browser cannot open its date picker programmatically, the date inputs are shown directly. --- src/features/events/__tests__/draft.test.ts | 8 +- src/features/events/draft.ts | 6 +- .../com/composer/event/EventDateRange.web.tsx | 133 +++++++++++------- .../__tests__/EventDateRange.web.test.tsx | 79 +++++++++++ 4 files changed, 173 insertions(+), 53 deletions(-) create mode 100644 src/view/com/composer/event/__tests__/EventDateRange.web.test.tsx diff --git a/src/features/events/__tests__/draft.test.ts b/src/features/events/__tests__/draft.test.ts index f0a1c2e89..f06508f6e 100644 --- a/src/features/events/__tests__/draft.test.ts +++ b/src/features/events/__tests__/draft.test.ts @@ -210,12 +210,16 @@ describe('event drafts', () => { ]) }) - it('leaves out a place when the country is unknown', () => { + it('keeps a place when the country is unknown', () => { const record = buildEventRecord(draft({location: 'Prospect Park'}), { now: NOW, }) - expect(record).not.toHaveProperty('locations') + expect(record).toMatchObject({ + mode: 'community.lexicon.calendar.event#inperson', + locations: [{name: 'Prospect Park', country: 'ZZ'}], + }) expect(record).not.toHaveProperty('timezone') + expect(parseEventRecord(record)?.location).toBe('Prospect Park') }) }) diff --git a/src/features/events/draft.ts b/src/features/events/draft.ts index 1f4efdf8d..8799acb0e 100644 --- a/src/features/events/draft.ts +++ b/src/features/events/draft.ts @@ -115,6 +115,8 @@ export function eventLocationUrl(draft: EventDraft): string | undefined { return webUrl(draft.location.trim()) } +// The address lexicon requires a country, and ZZ is CLDR's unknown region. +const UNKNOWN_COUNTRY = 'ZZ' const BARE_DOMAIN = /^[a-z0-9-]+(\.[a-z0-9-]+)*\.[a-z]{2,}(\/\S*)?$/i function webUrl(value: string): string | undefined { @@ -167,14 +169,14 @@ export function buildEventRecord( mode: `${EVENT_COLLECTION}#virtual`, locations: [{$type: `${EVENT_COLLECTION}#uri`, uri: locationUrl}], } - : location && country + : location ? { mode: `${EVENT_COLLECTION}#inperson`, locations: [ { $type: 'community.lexicon.location.address', name: location, - country, + country: country || UNKNOWN_COUNTRY, }, ], } diff --git a/src/view/com/composer/event/EventDateRange.web.tsx b/src/view/com/composer/event/EventDateRange.web.tsx index c3df2f6ca..84db3f539 100644 --- a/src/view/com/composer/event/EventDateRange.web.tsx +++ b/src/view/com/composer/event/EventDateRange.web.tsx @@ -1,9 +1,10 @@ -import {useRef} from 'react' +import {type CSSProperties, useRef, useState} from 'react' import {Pressable, View} from 'react-native' import {useLingui} from '@lingui/react/macro' -import {atoms as a} from '#/alf' +import {atoms as a, useTheme} from '#/alf' import { + type EventTimeField, fromLocalInputValue, toLocalInputValue, } from '#/features/events/draft' @@ -12,66 +13,100 @@ import {DateBox, FieldLabelText} from './EventFields' const FIELDS = ['startsAt', 'endsAt'] as const +const HIDDEN_INPUT: CSSProperties = { + position: 'absolute', + left: 0, + bottom: 0, + width: '100%', + height: 0, + opacity: 0, + border: 0, + padding: 0, + pointerEvents: 'none', +} + export function EventDateRange({ startsAt, endsAt, isEndInvalid, onChange, }: EventDateRangeProps) { + const t = useTheme() const {t: l} = useLingui() const inputs = useRef>({}) + const [isPickerUnavailable, setIsPickerUnavailable] = useState( + () => !('showPicker' in HTMLInputElement.prototype), + ) const values = {startsAt, endsAt} const labels = {startsAt: l`Start date`, endsAt: l`End date`} + const renderInput = ( + key: EventTimeField, + style: CSSProperties, + hidden: boolean, + ) => ( + { + inputs.current[key] = el + }} + type="datetime-local" + data-testid={`${key}Input`} + aria-label={labels[key]} + tabIndex={hidden ? -1 : undefined} + value={toLocalInputValue(values[key])} + onChange={e => { + const iso = fromLocalInputValue(e.target.value) + if (iso) onChange(key, iso) + }} + style={style} + /> + ) + return ( - {FIELDS.map(key => ( - - {labels[key]} - { - const input = inputs.current[key] - try { - input?.showPicker() - } catch { - input?.focus() - } - }}> - - { - inputs.current[key] = el - }} - type="datetime-local" - data-testid={`${key}Input`} - aria-label={labels[key]} - tabIndex={-1} - value={toLocalInputValue(values[key])} - onChange={e => { - const iso = fromLocalInputValue(e.target.value) - if (iso) onChange(key, iso) - }} - style={{ - position: 'absolute', - left: 0, - bottom: 0, - width: '100%', - height: 0, - opacity: 0, - border: 0, - padding: 0, - pointerEvents: 'none', - }} - /> - - - ))} + {FIELDS.map(key => { + const isInvalid = key === 'endsAt' && isEndInvalid + return ( + + {labels[key]} + {isPickerUnavailable ? ( + renderInput( + key, + { + boxSizing: 'border-box', + width: '100%', + padding: '6px 16px', + borderRadius: 10, + border: `1px solid ${ + isInvalid ? t.palette.negative_500 : 'transparent' + }`, + background: t.atoms.bg_contrast_50.backgroundColor, + color: t.atoms.text.color, + colorScheme: t.scheme, + font: 'inherit', + fontSize: a.text_lg.fontSize, + }, + false, + ) + ) : ( + { + try { + inputs.current[key]?.showPicker() + } catch { + setIsPickerUnavailable(true) + } + }}> + + {renderInput(key, HIDDEN_INPUT, true)} + + )} + + ) + })} ) } diff --git a/src/view/com/composer/event/__tests__/EventDateRange.web.test.tsx b/src/view/com/composer/event/__tests__/EventDateRange.web.test.tsx new file mode 100644 index 000000000..365bb3480 --- /dev/null +++ b/src/view/com/composer/event/__tests__/EventDateRange.web.test.tsx @@ -0,0 +1,79 @@ +import {fireEvent, render} from '@testing-library/react-native' + +jest.mock('@lingui/react', () => ({ + useLingui: () => ({ + _: (message: {message?: string} | string) => + typeof message === 'string' ? message : (message.message ?? ''), + }), +})) +jest.mock('#/alf', () => { + const blank = new Proxy({}, {get: () => ({})}) + return { + atoms: blank, + useTheme: () => ({ + atoms: blank, + palette: new Proxy({}, {get: (target, key) => String(key)}), + scheme: 'dark', + }), + } +}) +jest.mock('../EventFields', () => ({ + DateBox: () => null, + FieldLabelText: () => null, +})) + +import {EventDateRange} from '../EventDateRange.web' + +const props = { + startsAt: '2027-01-15T11:05:00.000Z', + endsAt: '2027-01-15T12:05:00.000Z', + isEndInvalid: false, + onChange: jest.fn(), +} + +function withInputPrototype(prototype: object) { + ;(globalThis as unknown as {HTMLInputElement: unknown}).HTMLInputElement = { + prototype, + } +} + +describe('EventDateRange on web', () => { + afterEach(() => { + delete (globalThis as unknown as {HTMLInputElement?: unknown}) + .HTMLInputElement + }) + + it('opens the browser picker from the styled boxes when it can', () => { + withInputPrototype({showPicker() {}}) + const {getAllByRole} = render() + expect(getAllByRole('button')).toHaveLength(2) + }) + + it('shows the date inputs directly when the browser has no picker API', () => { + withInputPrototype({}) + const {queryAllByRole, UNSAFE_getAllByType} = render( + , + ) + expect(queryAllByRole('button')).toHaveLength(0) + const [start] = UNSAFE_getAllByType('input' as never) as unknown as { + props: {tabIndex?: number} + }[] + expect(start.props.tabIndex).toBeUndefined() + }) + + it('falls back to the date inputs when opening the picker fails', () => { + withInputPrototype({showPicker() {}}) + const {getAllByRole, queryAllByRole} = render( + , + { + createNodeMock: () => ({ + showPicker() { + throw new Error('not supported') + }, + }), + }, + ) + fireEvent.press(getAllByRole('button')[0]) + expect(queryAllByRole('button')).toHaveLength(0) + }) +}) -- knotmirror 0.0.1