getkirby / kirby

Kirby's core application folder
https://getkirby.com
Other
1.32k stars 168 forks source link

Select field has no initial value in structure/object fields #4838

Closed medienbaecker closed 1 year ago

medienbaecker commented 2 years ago

Description

When using a select field inside of a structure or object, the field doesn't initially have a value — compared to using it directly in the page blueprint.

Why is this bad?

Let's say you're using a select field and other fields react to it via field conditions:

select:
  options:
    - Option A
    - Option B
    - Option C
text:
  when:
    select: ""

The text field is not shown initially. Only when changing the select field to some option and back to nothing it's shown. Interestingly enough it also works by saving the empty value and opening the structure/object again. I tried to set default: "" but it didn't work either.

select-when

Expected behavior

I'd expect the select field to have an initial value of "" just like it does outside of structure or object fields.

To reproduce

  1. Add a structure or object field to your blueprint
  2. Add a select field to the structure/object field
  3. Add a field with a when condition for the select field to the structure/object
structure:
  fields:
    select:
      options:
        - Option A
        - Option B
        - Option C
    text:
      when:
        select: ""
# or:
object:
  fields:
    select:
      options:
        - Option A
        - Option B
        - Option C
    text:
      when:
        select: ""

Your setup

Kirby Version
3.8.1.1

afbora commented 2 years ago

Summary

Because when the page is created, the value of the empty field is empty string " ", while the value of a field is null when creating an entry in the structure or object field.

afbora commented 2 years ago

I'm not sure that this idea could be break somethings but I've a solution. We can set empty string as fallback value like that

const value = values[key.toLowerCase()] ?? "";

Related line:

https://github.com/getkirby/kirby/blob/3e5899b628a2b59c1833a8d79eda292a7fe06843/panel/src/helpers/field.js#L21

lukasbestle commented 1 year ago

@afbora Does the issue only affect the structure field or do other fields behave the same inside a structure or object field?

afbora commented 1 year ago

@lukasbestle for structure, object field and for each field containing the form (not sure).

The source of the issue: the field values are empty string when the page is created. But the field values in the form (with in structure and object field) created by the fields are assigned as null.

lukasbestle commented 1 year ago

I'd say that this is the bug then. IMO the parent form and the "field form" of the structure and object fields should behave the same. It's also weird that the default prop doesn't seem to have an effect in the "field form".

afbora commented 1 year ago

Yes, also doesn't work like you said when you define as default: "".

Related lines (for structure) that we do set default (null when no default prop) while initialization of form:

https://github.com/getkirby/kirby/blob/3e5899b628a2b59c1833a8d79eda292a7fe06843/panel/src/components/Forms/Field/StructureField.vue#L351-L353

distantnative commented 1 year ago

I would agree that the solution we should aim is making sure that creating a new page and creating a new structure form produce the same results.

bastianallgeier commented 1 year ago