Giter VIP home page Giter VIP logo

Comments (3)

ZeeshanTamboli avatar ZeeshanTamboli commented on September 27, 2024 1

You can leverage hidden input fields i.e <input type="hidden" />:

import { Autocomplete, Button, TextField } from '@mui/material'
import * as React from 'react'
import { Form, type ActionFunctionArgs } from 'react-router-dom'

const someOptions = [
  { label: 'Option1' },
  { label: 'Option2' },
  { label: 'Option3' },
]

export function AutoCompleteDemo() {
  const [selectedOptions, setSelectedOptions] = React.useState([])

  return (
    <Form method="POST" action="/example">
      <Autocomplete
        options={someOptions}
        getOptionLabel={(option) => option.label}
        multiple
        onChange={(event, value) => setSelectedOptions(value)}
        renderInput={(params) => (
          <TextField
            {...params}
            required
            variant="outlined"
            label="Example"
          />
        )}
      />
      {selectedOptions.map((option, index) => (
        <input
          key={index}
          type="hidden"
          name="example"
          value={option.label}
        />
      ))}
      <Button type="submit">Submit</Button>
    </Form>
  )
}

export async function AutoCompleDemoAction(args: ActionFunctionArgs) {
  const formData = await args.request.formData()
  const selectedValues = formData.getAll('example')
  console.log(selectedValues)
  return true
}

from material-ui.

TheRealCuran avatar TheRealCuran commented on September 27, 2024

Thank you for your reply. We know, that we can do it like that ourselves. But this was a feature request to make the access a native functionality – preferably semantically correct, but pure hidden input works as well. Especially since the documentation doesn't remark anywhere, that the single select and multi-select case behave different with regards to FormData.

In conclusion: we have a workaround like that operating here, but we want MUI's <Autocomplete> to handle this for us and offer us a "organic" interface to the data – no matter if we have a single or multi-value <Autocomplete>. ;-)

from material-ui.

thomasmarr avatar thomasmarr commented on September 27, 2024

@TheRealCuran I'm with you on this one. In the meantime I've shared the wrapper I'm using on #43544 in case it's of any use to you. Also interested to see what you are doing as a workaround.

from material-ui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.