Comments (5)
It doesn't look like we are sending a ref you'd be able to access in the way you expect anyways. Can I ask more about what you're trying to accomplish? Could you do something along these lines instead?
let CustomContext = createContext({ ref: null });
const Field = (props) => {
let inputRef = useRef(null);
return (
<CustomContext.Provider value={{ ref: inputRef }}>
<TextField defaultValue="with textfield">
<Input ref={inputRef} />
<LogButton />
</TextField>
</CustomContext.Provider>
);
};
const LogButton = (props) => {
let { ref } = useContext(CustomContext);
return (
<Button
onPress={() => {
console.log(ref?.current);
}}
>
Log ref
</Button>
);
};
from react-spectrum.
I think you're wrong
ref: https://github.com/adobe/react-spectrum/blob/main/packages/react-aria-components/src/TextField.tsx
[InputContext, {...inputProps, ref: inputOrTextAreaRef}],
[TextAreaContext, {...inputProps, ref: inputOrTextAreaRef}],
from react-spectrum.
My useCase: i have an <InputWrapper />
component that wraps any input where i need the ref to focus on input element when needed
interface InputWrapperProps
extends Omit<AriaGroupProps, "className" | "prefix">,
VariantProps<typeof inputStyles> {
prefix?: React.ReactNode;
suffix?: React.ReactNode;
loading?: boolean;
loaderPosition?: "prefix" | "suffix";
className?: string;
}
const InputWrapper = React.forwardRef<HTMLDivElement, InputWrapperProps>(
(
{
className,
size,
variant,
loading,
prefix,
suffix,
loaderPosition = "suffix",
multiline = false,
...props
},
ref
) => {
const { isInvalid } = React.useContext(AriaFieldErrorContext);
const inputProps = React.useContext(AriaInputContext);
const textAreaProps = React.useContext(AriaTextAreaContext);
const localRef = React.useRef<HTMLInputElement | HTMLTextAreaElement>(null);
const inputRef = inputProps?.ref ?? textAreaProps?.ref ?? localRef; // TODO Fix this
const { root } = inputStyles({
size,
variant: variant ?? (isInvalid ? "danger" : undefined),
multiline,
});
const showPrefixLoading = loading && loaderPosition === "prefix";
const showSuffixLoading = loading && loaderPosition === "suffix";
return (
<Provider
values={[
[AriaInputContext, { ...inputProps, ref: inputRef as React.RefObject<HTMLInputElement> }],
[
AriaTextAreaContext,
{ ...textAreaProps, ref: inputRef as React.RefObject<HTMLTextAreaElement> },
],
]}
>
<AriaGroup
ref={ref}
role="presentation"
className={root({ className })}
{...props}
onPointerDown={(event) => {
const target = event.target as HTMLElement;
if (target.closest("input, button, a")) return;
const input = inputRef.current;
if (!input) return;
requestAnimationFrame(() => {
input.focus();
});
}}
>
{composeRenderProps(props.children, (children) => (
<>
<InputInnerVisual
side="start"
loading={showPrefixLoading}
multiline={multiline}
>
{prefix}
</InputInnerVisual>
{children}
<InputInnerVisual
side="end"
loading={showSuffixLoading}
multiline={multiline}
>
{suffix}
</InputInnerVisual>
</>
))}
</AriaGroup>
</Provider>
);
}
);
InputWrapper.displayName = "InputWrapper";
from react-spectrum.
I think you're wrong
ref: https://github.com/adobe/react-spectrum/blob/main/packages/react-aria-components/src/TextField.tsx
[InputContext, {...inputProps, ref: inputOrTextAreaRef}], [TextAreaContext, {...inputProps, ref: inputOrTextAreaRef}],
Those are defined as a callback ref, it has no 'current' property, therefore, you cannot access it in the way you are expecting.
What you could do though, is merge the ref already on the context with your own ref. Then place that merged ref on the context to propagate it down. https://github.com/adobe/react-spectrum/blob/main/packages/%40react-aria/utils/src/mergeRefs.ts
from react-spectrum.
Okey, it's more clear for me, thank you so much 🙏
from react-spectrum.
Related Issues (20)
- Readonly date segment keyboard navigation does not work on iOS HOT 5
- Not able to search in Combobox in latest version of Chrome HOT 6
- Why does Table follow ARIA grid pattern instead of ARIA table pattern? HOT 2
- Button - onClick does not propagate in iframe HOT 3
- Enhance Toast Notifications: Add Update Functionality and Prevent Duplication Using Custom Keys or IDs HOT 5
- [RAC] Add `orientation` prop to `GridList` HOT 1
- Rollup warnings HOT 2
- FocusManager.focusNext() Not working properly. HOT 17
- @parcel/core: packages/@react-aria/virtualizer/src/index.ts does not export 'useVirtualizer' HOT 4
- Action Bar's dropdown menu slides off when action bar is small
- usePress wrapper on a text input swallows space key presses HOT 3
- SelectItem missing ref prop while ListboxItem implements/exposes it. HOT 6
- [RAC] Add autoscroll config to AriaLink HOT 1
- [RAC] Breaking change to `children` type in `BreadcrumbProps` in v1.3.2 HOT 1
- Date and time not updating with shouldCloseOnSelect equals false in DateRangePicker from react-aria-components HOT 1
- Type param in SelectProps<T> is not used HOT 6
- SelectValue shows placeholder when the selected value label is 0 HOT 9
- Long tag names are not truncated to fit in parent container HOT 1
- Update state when visibleRange changes in <RangeCalendar> HOT 5
- [react-aria] FocusScope with contain prevents programmatic scrolling when `prefers-reduced-motion: reduced` and `scroll-behaviour: auto` is given HOT 5
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.