-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add autocomplete prop to rac datefield + datepicker #7773
Open
yihuiliao
wants to merge
8
commits into
main
Choose a base branch
from
datepicker-autocomplete
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+364
−9
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
213b100
add hidden date input component
yihuiliao cdc178a
add stories
yihuiliao 946bf7b
fix lint
yihuiliao e69098e
fix story
yihuiliao 264d098
add comments, fix types
yihuiliao 1508188
add test (not finished)
yihuiliao da9fcc2
fix types
yihuiliao 18e401d
add test
yihuiliao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/* | ||
* Copyright 2025 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
|
||
import {CalendarDate, CalendarDateTime, parseDate, parseDateTime} from '@internationalized/date'; | ||
import {DateFieldState, DatePickerState, DateSegmentType} from 'react-stately'; | ||
import React, {useEffect} from 'react'; | ||
import {useVisuallyHidden} from 'react-aria'; | ||
|
||
interface AriaHiddenDateInputProps { | ||
/** | ||
* Describes the type of autocomplete functionality the input should provide if any. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefautocomplete). | ||
*/ | ||
autoComplete?: string, | ||
/** HTML form input name. */ | ||
name?: string, | ||
/** Sets the disabled state of the input. */ | ||
isDisabled?: boolean | ||
} | ||
|
||
interface HiddenDateInputProps extends AriaHiddenDateInputProps { | ||
/** | ||
* State for the input. | ||
*/ | ||
state: DateFieldState | DatePickerState | ||
} | ||
|
||
export interface HiddenDateAria { | ||
/** Props for the container element. */ | ||
containerProps: React.HTMLAttributes<HTMLDivElement>, | ||
/** Props for the hidden input element. */ | ||
inputProps: React.InputHTMLAttributes<HTMLInputElement> | ||
} | ||
|
||
export function useHiddenDateInput(props: HiddenDateInputProps, state: DateFieldState | DatePickerState) { | ||
let { | ||
autoComplete, | ||
isDisabled, | ||
name | ||
} = props; | ||
let [dateValue, setDateValue] = React.useState(''); | ||
let {visuallyHiddenProps} = useVisuallyHidden(); | ||
|
||
let inputStep = 60; | ||
if (state.granularity === 'second') { | ||
inputStep = 1; | ||
} else if (state.granularity === 'hour') { | ||
inputStep = 3600; | ||
} | ||
|
||
useEffect(() => { | ||
if (state.value == null) { | ||
setDateValue(''); | ||
} else { | ||
setDateValue(state.value.toString()); | ||
} | ||
}, [state.value]); | ||
|
||
let inputType = state.granularity === 'day' ? 'date' : 'datetime-local'; | ||
|
||
let dateSegments = ['day', 'month', 'year']; | ||
let timeSegments = ['hour', 'minute', 'second']; | ||
let granularityMap = {'hour': 1, 'minute': 2, 'second': 3}; | ||
|
||
// Depending on the granularity, we only want to validate certain time segments | ||
let end = 0; | ||
if (timeSegments.includes(state.granularity)) { | ||
end = granularityMap[state.granularity]; | ||
timeSegments = timeSegments.slice(0, end); | ||
} | ||
|
||
return { | ||
containerProps: { | ||
...visuallyHiddenProps, | ||
'aria-hidden': true, | ||
// @ts-ignore | ||
['data-react-aria-prevent-focus']: true, | ||
// @ts-ignore | ||
['data-a11y-ignore']: 'aria-hidden-focus' | ||
}, | ||
inputProps: { | ||
tabIndex: -1, | ||
autoComplete, | ||
disabled: isDisabled, | ||
type: inputType, | ||
// We set the form prop to an empty string to prevent the hidden date input's value from being submitted | ||
form: '', | ||
name, | ||
step: inputStep, | ||
value: dateValue, | ||
onChange: (e) => { | ||
let targetString = e.target.value.toString(); | ||
if (targetString) { | ||
let targetValue: CalendarDateTime | CalendarDate = parseDateTime(targetString); | ||
if (state.granularity === 'day') { | ||
targetValue = parseDate(targetString); | ||
} | ||
setDateValue(targetString); | ||
// We check to to see if setSegment exists in the state since it only exists in DateFieldState and not DatePickerState. | ||
// The setValue method has different behavior depending on if it's coming from DateFieldState or DatePickerState. | ||
// In DateFieldState, setValue firsts checks to make sure that each segment is filled before committing the newValue | ||
// which is why in the code below we first set each segment to validate it before committing the new value. | ||
// However, in DatePickerState, since we have to be able to commit values from the Calendar popover, we are also able to | ||
// set a new value when the field itself is empty. | ||
if ('setSegment' in state) { | ||
for (let type in targetValue) { | ||
if (dateSegments.includes(type)) { | ||
state.setSegment(type as DateSegmentType, targetValue[type]); | ||
} | ||
if (timeSegments.includes(type)) { | ||
state.setSegment(type as DateSegmentType, targetValue[type]); | ||
} | ||
} | ||
|
||
} | ||
state.setValue(targetValue); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
export function HiddenDateInput(props: HiddenDateInputProps) { | ||
let {state} = props; | ||
let {containerProps, inputProps} = useHiddenDateInput({...props}, state); | ||
return ( | ||
<div {...containerProps} data-testid="hidden-dateinput-container"> | ||
<input {...inputProps} /> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a little unsure if this is the direction we want to go. i left some comments in the code to explain what's going on but essentially the issue is that depending if the state is DateFieldState or DatePickerState, the setValue method behaves a bit differently.
i've tried validating each segment inside setValue in useDateFieldState but the problem is that we aren't able to differentiate between a newValue that is the placeholder date versus something external (like in this case). as a result, we end up committing the placeholder value as an actual value that we want to display. in any case, it causes some really strange behavior when trying to validate segments inside setValue so it worked better to do it here inside HiddenDateInput