Checkbox control

default discord avatar
agolovan
7 months ago
17

We just have that control in our code: import { Text, Checkbox, Select } from 'payload/components/forms';


<Checkbox name={controlName} label={label} required={isRequired} /> Could we use some additional attribute to set that checked initially?



@jacobsfletch

  • discord user avatar
    jacobsfletch
    Payload Team
    7 months ago

    Is this a custom field component? In that case you could use

    defaultValue: true

    in your config as described here

    https://payloadcms.com/docs/fields/checkbox#config


    Otherwise if you're importing Payload's checkbox for another use, it's not quite setup for that yet. This is a roadmap item described here

    https://github.com/payloadcms/payload/discussions/293

    which we plan on tackling with moderate priority.



    Basically we need to allow the checkbox to be externally controlled. Right now it ties into form state but we need to detach it. We've already done this for the

    Text

    ,

    Textarea

    , and

    Upload

    fields, just need to get to the rest.

  • default discord avatar
    agolovan
    7 months ago

    This checkbox is in AdminView that is invoked with some url that is that in config file. I could use defaultValue, but to what value to set that?



    Yes we are setting using initial values for form when state is saved into database. But when started to create, it is set to false.



    Not a huge deal, we could just allow the user to click and check. When submitted, state is saved into database.

  • discord user avatar
    jacobsfletch
    Payload Team
    7 months ago

    Ok then you should be able to use

    defaultValue: true

    in your checkbox config

  • default discord avatar
    agolovan
    7 months ago

    Not really - I don’t know name of this field when config is created. When we are selecting property required, for example, check box is created but the user need to click that checkbox, not a huge deal.

  • discord user avatar
    jacobsfletch
    Payload Team
    6 months ago

    Ok so it does sound like this falls under the latter, which means we just need to allow support for external control of the checkbox field

  • default discord avatar
    Jarrod
    6 months ago

    I think we just need to export the checkbox input, like we do for the text field

  • discord user avatar
    jacobsfletch
    Payload Team
    6 months ago

    We have to detach it from form context to allow to be controlled via props.

  • default discord avatar
    agolovan
    6 months ago

    This is not a huge deal at all - it is ok for user to select checkbox to be added under the form and click to be checked.



    Yes, checkbox input has checked property, but it is inside checkbox and does not work under the form. I mean when we press submit, fields and data have the state of all controls, so we are using checkbox for that to work properly.



    What do you mean related to text field? Could text field to be controlled via properties?

  • default discord avatar
    Jarrod
    6 months ago

    I was thinking you were trying to render a custom field component, but I’m not sure if you are

  • default discord avatar
    agolovan
    6 months ago

    We could use initialData, for form, but may be there is a different way, like to use property by text field.

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.