Code Monkey home page Code Monkey logo

Comments (5)

snowystinger avatar snowystinger commented on September 26, 2024

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.

mehdibha avatar mehdibha commented on September 26, 2024

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.

mehdibha avatar mehdibha commented on September 26, 2024

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.

snowystinger avatar snowystinger commented on September 26, 2024

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.

let inputOrTextAreaRef = useCallback((el) => {

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.

mehdibha avatar mehdibha commented on September 26, 2024

Okey, it's more clear for me, thank you so much 🙏

from react-spectrum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.