How i can set default value on tag <SelectField />?

how i can set default value on tag ?

Have you tried adding a selected prop to your default option?

1 Like

It appears I’ve done this by setting defaultValue on <SelectField /> – if I remember correctly, setting selected on an option worked, but gave me an error in the developer console. It’s certainly worth double checking that, though.

1 Like

thanks! defaultValue is what i need

If someone has similar issue to me where even when using defaultValue it’s not set see if converting value to string with .toString() helps.

For context:
I created a SelectField for related data inside a model and used CUID for ID of all tables.
Options are using the ID as value from query for all fileds in RelatedTable/Fields and defaultValue is set based on current model’s relatedFieldId creted by Prisma/Redwood.

Might be related to me using CUID (as String) instead of Integers.
I am curious if anyone else had a similar issue.

<SelectField
          name="relatedFieldId"
          defaultValue={props.model.relatedFieldId.toString()} // toString() here is the key change it would not work without it!
          className="rw-input"
          errorClassName="rw-input rw-input-error"
        >
          {props.relatedFields?.map((relatedField) => {
            return (
              <option
                key={relatedField?.id}
                value={relatedField?.id}
              >
                {relatedField?.title}
              </option>
            );
          })}
        </SelectField>