lundi 11 mars 2019

ReactJS checkbox default value

I'm having a problem setting a checkbox to checked right on load to always be selected by the user using ReactJS. I'm fairly new to react and i'm struggling a lot with this kind of things for now.

My code is like this

import DbCheckbox2 from 'modules/Checkbox-Checked'

const GenerateTemplateComponent = ({
  handleSubmit,
  handleCancel,
  classes
}) => {
  return (
    <Form
      onSubmit={handleSubmit}
      validate={validate}
      render={({ handleSubmit, invalid, pristine, form: { change } }) => (
        <form className={classes.form} onSubmit={handleSubmit}>
          <PanelBody>
            <Grid container>
              <Grid item xs={12} md={12}>
                <Field
                  name='name'
                  component={DbInputField}
                  required
                  label={<FormattedMessage id='offre-name' />}
                />
              </Grid>
              <Grid container>
                <Grid item xs={6} md={6}>
                  <Field
                    //className={classes.inputCheckbox}
                    name='section'
                    component={DbCheckbox2}
                    type='checkbox'
                    color='primary'
                    label={<FormattedMessage id='offre-section' />}  
                  />
                </Grid>

And on the Checkbox-Checked file is like this

import React from 'react'

import FormControl from '@material-ui/core/FormControl'
import FormControlLabel from '@material-ui/core/FormControlLabel'
import Checkbox from '@material-ui/core/Checkbox'

const DbCheckbox2 = ({
  label,
  className,
  checked=true,
  value=true,
}) => (
  <FormControl className={className} component="fieldset">
    <FormControlLabel
      control={
        <Checkbox
          checked={checked}
          value={value}
        />
      }
      label={label}
    />
  </FormControl>
)

export default DbCheckbox2

I tried to add value="true" on the field properties, and i get a value="true" on the html, but its really get me the results i desire when i click again on the checkbox, like the first time its doesn't recognize. Maybe he awaiting a onclick event or something, but i dont get it at all. Any help?




Aucun commentaire:

Enregistrer un commentaire