IUIKitSurface State does not hold value if initialValue is used in a InputBlock
What?
When I am trying to read a value from IUIKitViewSubmitIncomingInteraction.IUIKitSurface.state:
- if the value of say a input box is unchanged (it uses initialValue) then I get undefined
- if the value of input box is altered (changed once from initialValue) I am successfully able to retrieve the value
Steps to reproduce?
-
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' }, }; -
Use the above block in UIKitSurfaceType.MODAL
-
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 -
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
I faced the same problem. The thing is that if value is unchanged it is contained in field name as UUID, instead of blockId.
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.