Comments (7)
@binaryartifex I'm looking into creating an overlay that shows the formatted value on blur, but the input as numeric. It will work, but I'd obviously prefer this being something that just works, and also has a forgiving input.
To be clear though, I'm not using react-aria-components, but the hooks from react-aria, so I have a bit more code than you 😅
from react-spectrum.
From a gut feeling writing the literals seems like it could be pretty hard, just thinking about languages that use a IME like Chinese/Japanese/etc. Would need to experiment with it, but supporting the text representations on blur like you've done might be something we could consider adding ourselves.
from react-spectrum.
this. for the love of god, this.
from react-spectrum.
@yusijs what I ultimately ended up doing was thinking a little outside the box and just adding a 'hidden' class on the entire DateInput inner component and adding my own span with a formatted date value. everything works exactly the same as before, screen reader behaves the exact same as well. Minus the types and styles, this is my entire date picker component. The styles.input()
class only has a class of hidden
on it.
export const DatePicker = forwardRef<HTMLDivElement, DatePickerProps>(function DatePicker(
{ description, errorMessage, hideLabel = false, label, onChange, placeholder, ...props },
ref,
) {
const formatter = useDateFormatter({ dateStyle: "medium" });
const timezone = getLocalTimeZone();
const isInvalid = Boolean(errorMessage || props.isInvalid);
const styles = datePickerStyles({ isInvalid });
const formatDate = useCallback(
(value: DateValue | null) => (value ? formatter.format(value.toDate(timezone)) : placeholder),
[formatter, placeholder, timezone],
);
const handleChange = useCallback(
(value: DateValue) => onChange && onChange(value.toString()),
[onChange],
);
return (
<AriaDatePicker
{...props}
className={styles.base()}
isInvalid={isInvalid}
onChange={handleChange}
ref={ref}
>
{({ state }) => (
<Fragment>
<div>
<FieldLabel hideLabel={hideLabel}>{label}</FieldLabel>
{description && <FieldDescription>{description}</FieldDescription>}
</div>
<div className={styles.body()}>
<Group className={styles.field()}>
<span className={styles.value()}>{formatDate(state.value)}</span>
<DateInput className={styles.input()}>
{(segment) => <DateSegment className={styles.segment()} segment={segment} />}
</DateInput>
<Button className={styles.button()}>
<Icon.Interface.Calendar />
</Button>
</Group>
<FieldValidation>{errorMessage}</FieldValidation>
</div>
<Popover className={styles.popover()}>
<Dialog>
<Calendar />
</Dialog>
</Popover>
</Fragment>
)}
</AriaDatePicker>
);
});
from react-spectrum.
Would the formatted value month value revert to a number when the user enters one of the number segments?
from react-spectrum.
Would the formatted value month value revert to a number when the user enters one of the number segments?
Ideally it would be possible to write w/ literals as well, but not sure how feasible that is. I've actually implemented the solution you mention here on my end now, as I figured a neat way to solve it yesterday. Ideally I would like it out of the box, but this works as well tbh.. :)
When using a literal month, the format often turns a bit around:
Localized -> December 31, 2024
Numeric -> 31.12.2024
Replaced on blur -> 31. December. 2024
So it's not localized properly, but it works.
from react-spectrum.
From a gut feeling writing the literals seems like it could be pretty hard, just thinking about languages that use a IME like Chinese/Japanese/etc. Would need to experiment with it, but supporting the text representations on blur like you've done might be something we could consider adding ourselves.
Yeah, I imagine it's quite a bit harder to handle in multiple locales. I've handled it previously, but that was only for en-US locale, so it was fairly straight forward to handle.
It would be a great QOL improvement for the display-value to change natively though; it's a bit tedious to handle it now imo:
<Segment
{...segmentProps}
onFocus={(e) => {
setFocus(true)
segmentProps.onFocus(e)
}}
onBlur={(e) => {
setFocus(false)
segmentProps.onBlur(e)
}}>
{focus
? (segment.isPlaceholder || segment.type === 'literal'
? segment.text
: segment.text.padStart(segment.type === 'year' ? 4 : 2, '0') )
: value}
Being able to avoid the focus-check / custom focus-handling would be neat, and reduce the additional code I've had to add quite a bit :)
from react-spectrum.
Related Issues (20)
- Docs are not clear about where to set props when using a custom ModalOverlay with Modal HOT 5
- Setting "display:none" on ListView parent crashes the page
- useMove/ColorArea/ColorSlider: Implement autoscrolling on move
- RSP does not install on windows
- User Search ComboBox, long text causes popup to immediately close when clicked HOT 2
- `Button`s inside a `GridListItem` are not disabled when the item is disabled
- Pin dependency versions on release HOT 3
- ComboBox with name property - warnings in console.
- Allow customizing react-spectrum DropZone's padding and border HOT 2
- useOverlayPosition regression for non-viewport boundary/container HOT 1
- Different popover underlay behaviors between mobile and desktop devices HOT 2
- Enter and Space should activate long presses HOT 1
- [Slider] Custom validation for multiple thumb values
- DatePicker and DateRangePicker onChange parameter type should include null. HOT 1
- Locale should probably be set from the system's Locale, not browser langauge HOT 5
- eslint-plugin-jsdoc dependency version forces node version <=19 HOT 2
- `useHref` passed to `RouterProvider` not respected for synthetic links
- Input in HiddenSelect component is not hidden from AutoFill, so it gets the value autofilled instead of the Select field below it
- RAC ListBox preview bug on safari when using a combination of position relative and absolute HOT 1
- Source maps not included for `@react-aria` packages HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-spectrum.