Skip to content
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

Investigate enabling methods on ref holders #163

Open
asklar opened this issue Nov 16, 2021 · 1 comment
Open

Investigate enabling methods on ref holders #163

asklar opened this issue Nov 16, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@asklar
Copy link
Member

asklar commented Nov 16, 2021

Instead of the command being a static method of the class

Is it possible to wire this up so that passing in the ref holder directly works?

Originally posted by @NickGerleman in #128 (comment)

@ghost ghost added the Needs: Triage 🔍 label Nov 16, 2021
@asklar
Copy link
Member Author

asklar commented Nov 16, 2021

Here's what I'm thinking:

in Types.tsx:

export class DependencyObjectRef<T> {
  protected readonly _ref: React.MutableRefObject<T | null>;
  public constructor() {
    this._ref = React.useRef<T>(null);
  }

  public get current() : T | null {
    return this._ref.current;
  }
  
  public set current(v : T | null) {
    this._ref.current = v;
  }
}

export class MenuFlyoutRef extends DependencyObjectRef<MenuFlyout>{  
  public ShowAt(args: { point: Point, }) {
    if (this._ref.current !== null) {
      MenuFlyout.ShowAt(this._ref as React.MutableRefObject<MenuFlyout>, args);
    }
  }
}

export class MenuFlyout extends React.Component<MenuFlyoutProps> {
  render() {
    return <NativeXamlControl {...this.props} type='Windows.UI.Xaml.Controls.MenuFlyout' />;
  }
  static useRef() {
    return new MenuFlyoutRef();
  }
  static ShowAt(ref: React.MutableRefObject<MenuFlyout>, args: { point: Point,  }) {
    const tag = findNodeHandle(ref.current);
    UIManager.dispatchViewManagerCommand(tag, xamlCommands.ShowAt, [args]);
  }
};

then at the usage site:

const menu = MenuFlyout.useRef();
// ...
return <TextBlock
            text="Hello"
            onTapped={e => {
              menu.ShowAt({point: {x: x, y: 42}});
            }}
            ref={t => {
              _tbRef.current = t;
            }}>
            <MenuFlyout
              ref={m => {
                menu.current = m;
              }}>
              <MenuFlyoutItem text="menu option" />
            </MenuFlyout>
          </TextBlock>

@NickGerleman do you think there is a better way to approach this rather than creating named ref holders that have the extra methods for every class and making consumers use the custom useRef? does TS have a concept of "extension methods"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants