How can I set default value as the last selected at my Select, in custom field configure context

Hi,

Currently, I’m developing a custom field type forge app.
I have selects at my configure custom field page.
The problem is when I select an item at Select and press save button, I can’t see what did I select at my next time visiting configure custom field page.

My code:

import ForgeUI, {useProductContext, CustomFieldContextConfig, TextField, Select, Option} from "@forge/ui";

export const ContextConfig = () => {
  const {extensionContext: {configuration = {}}} = useProductContext();
  const onSubmit = (formData) => ({ configuration: { cfDate1: formData.cfDate1 } });

  return (
    <CustomFieldContextConfig onSubmit={onSubmit}>
      <Select label="Select the first Date Custom Field" name="cfDate1" defaultValue={{label: configuration.cfDate1, value: 1}}>
        <Option label='0' value='0' />
        <Option label='1' value='1' />
      </Select>
    </CustomFieldContextConfig>
  );
};

My configure custom field page seems always same like this:
Screen Shot 2021-12-17 at 16.07.38

Waiting for a help from this experienced community.
Thanks already.
Mert

1 Like

Hey @AliMert,

You need to use the defaultSelected property of the Option component. See https://developer.atlassian.com/platform/forge/ui-kit-components/form/#option.

2 Likes

Hey @kkercz,

I saw the documentation about Option.
But in my Select, my last selected value may change.
So, I can’t assign a single option as a defaultSelected.

Here is my code to explain myself better:

//Submit data when onSubmit triggered
  const onSubmit = (formData) => (
      { configuration: { cfDate1: formData.cfDate1, cfDate2: formData.cfDate2 } });

    const selects = [];
    //Filtering Custom Field types to 'date' and 'datetime'
    for (var i=0 ; i<=ids.length ; i++){
        try{
            if (ids[i].custom==true &&  (ids[i].schema.type == "date" || ids[i].schema.type == "datetime")){
                selects.push(<Option label={ids[i].name} value={ids[i].id} />);
            };
        } catch (TypeError){}}
    return (
    <CustomFieldContextConfig onSubmit={onSubmit}>
        <Select label="Select the first Date Custom Field" name="cfDate1" >
              {selects}
        </Select>
        <Select label="Select the second Date Custom Field" name="cfDate2" >
            {selects}
        </Select>
    </CustomFieldContextConfig>
  );

This is my Custom Field Context Config page.

Hey @kkercz ,
This works great for the default value, however, through to this solution it is not possible to select None of the values. Even if the Select block does not have the parameter isRequired set to true.

How do you work around this issue?

Best,
Max

I have a similar problem related to default values in custom fields:

My custom field edit is shown on the create screen:

But the values of the field only gets saved if they are edited at least once. If I would add a custom field manually via the Jira config, I would edit the context and add a default value there - but that seems to work differently as if it is added via Forge.

Is there a way to get these values recognized as default values in the case if its added via the Forge manifest?

Hi @DanielKleissl
Thanks for bringing this issue to our attention. We’ll investigate the issue with UI Kit and see if we can change it so the default values would be submitted on create issue. Here is a public issue we created for that: [FRGE-977] - Ecosystem Jira

For now the other solution I have for you is to use custom UI and view.submit method. In custom UI you can decide on your own when you’re making a submit. That means you could write code to submit the default value on mount so it won’t require any user interaction with the field. E.g. in react it would look like:

useEffect(() => {
    view.submit(defaultValue);
}, []);

The template app for custom ui custom fields has some view.submit methods implemented on field blur actions, however you can modify it as you please :slight_smile:

2 Likes

Happy to help.

Thanks for creating the Issue. We are already discussing internally to change the Custom Field Edit to a Custom UI element, so that would be a, longterm, fix.

But if we decide to stay with UI Kit that would be a great change.

1 Like