Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

IUIKitSurface State does not hold value if initialValue is used in a InputBlock #606

Open
haemza30 opened this issue Mar 28, 2023 · 1 comment

Comments

@haemza30
Copy link

What?

When I am trying to read a value from IUIKitViewSubmitIncomingInteraction.IUIKitSurface.state:

  1. if the value of say a input box is unchanged (it uses initialValue) then I get undefined
  2. if the value of input box is altered (changed once from initialValue) I am successfully able to retrieve the value

Steps to reproduce?

  1. Create a block
    const block: InputBlock = { type: "input", label: { type: "plain_text", text: labelText, }, element: { type: "plain_text_input", placeholder: { type: "plain_text", text: 'Enter your ID', }, appId: 'app-id, blockId: 'block-id', actionId: 'action-id', initialValue: '12345' }, };

  2. Use the above block in UIKitSurfaceType.MODAL

  3. Try to retrieve the value of input box in the submit handler:
    const id = data.view.state.'block-id'.'action-id'; // here data is IUIKitViewSubmitIncomingInteraction

  4. If you don't change the initialValue of the input box you will get id as undefined, but if you even change a character in it it will work fine

PS

Expectation:

Must get value even if the input box has unchanged initialValue

@eonae
Copy link

eonae commented Sep 27, 2024

I faced the same problem. The thing is that if value is unchanged it is contained in field name as UUID, instead of blockId.

image

Painful...

My workaround is something like this:

const getLeafValue = (obj: any, leafKey: string): any | undefined => {
  for (const [key, value] of Object.entries(obj)) {
    if (typeof value === 'object' && value !== null) {
      const inner = getLeafValue(value, leafKey);
      if (inner) {
        return inner;
      }
    }

    if (key === leafKey) {
      return value;
    }
  }

  return undefined;
};

const x = getLeafValue(
  {
    '0b4adc14-3fbb-4ae3-9a4f-9e906ce8ef11': {
      value1: '...',
    },
    'tg.username': {
      value2: '111',
    },
  },
  'value2',
);

console.log(x); // 111

Which works if using unique actionIds. But that definitely a dirty hack.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants