base-ui icon indicating copy to clipboard operation
base-ui copied to clipboard

[FormControl] Integrate FormControl with input components

Open mj12albert opened this issue 1 year ago • 1 comments

Motivation

To be able to easily compose different kinds of (higher level) form fields with consistent interaction states using Base components/hooks,

Currently, only the Input component is supported (doc).

<FormControlUnstyled defaultValue="" required>
  <Label>Name</Label>
  <Input />
  <HelperText />
</FormControlUnstyled>

But that <Input /> could just as well be an Autocomplete or a Switch.

Requirements

1. Consistent interface for getting a value from any form component

  • Currently FormControl accepts an onChange prop that gets an event as the sole argument
  • This works for getting the value from a basic Input using event.target.value, but doesn't work for cases like multi-select or Autocomplete with multiple options
  • It could updated to provide the latest value as a second argument like: onChange: (event: React.SyntheticEvent, newValue: any) => void

Some components already have this second argument:

  • useAutocomplete: onChange?: (event: React.SyntheticEvent, value: AutocompleteValue<T, Multiple, DisableClearable, FreeSolo>, reason: AutocompleteChangeReason, details?: AutocompleteChangeDetails<T>) => void
  • useSlider: onChange?: (event: Event, value: number | number[], activeThumb: number) => void
  • useSelect: onChange?: ( event: React.MouseEvent | React.KeyboardEvent | React.FocusEvent | null, value: SelectValue<OptionValue, Multiple>, ) => void
  • useNumberInput

2. Additional metadata

Some components call onChange with "metadata" arguments:

  • Autocomplete: reason, details
  • Slider: activeThumb

Perhaps a general metadata object can be used as the third argument, e.g. onChange: (event, newValue, metadata).

Components

  • [ ] Input
  • [ ] Select https://github.com/mui/base-ui/issues/12
  • [ ] Slider
  • [ ] Switch https://github.com/mui/base-ui/issues/13
  • [ ] Autocomplete
  • [ ] TextareaAutosize
  • [ ] NumberInput

mj12albert avatar Mar 23 '23 11:03 mj12albert

I made a sandbox that integrates a react-hook-form based form with all of Base's form components as a reference: https://codesandbox.io/s/baseui-react-hook-form-8s6ptc?file=/src/App.tsx

mj12albert avatar May 24 '23 08:05 mj12albert

The FormControl has been superseded by Form and Field components.

michaldudak avatar Sep 10 '24 09:09 michaldudak