Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I use MUI Slider? #121

Closed
k7en opened this issue Jan 10, 2020 · 7 comments
Closed

Can I use MUI Slider? #121

k7en opened this issue Jan 10, 2020 · 7 comments
Labels
question Further information is requested

Comments

@k7en
Copy link

k7en commented Jan 10, 2020

Hi,
I couldn't use MUI-Slider on Formik, so I was searching for a solution and arrived. Can I use Slider? Or do you have plans to use it?

Please refer to the link.
mui/material-ui#13485
https://material-ui.com/api/slider/

@k7en k7en changed the title MUI Slider Can I use MUI Slider? Jan 10, 2020
@cliedeman cliedeman added the question Further information is requested label Jan 21, 2020
@jan-tee
Copy link

jan-tee commented Jan 24, 2020

You could use this for the time being:

import * as React from "react";
import MuiSlider, { SliderProps as MuiSliderProps } from "@material-ui/core/Slider";
import { FieldProps } from "formik";

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

export interface SliderProps extends FieldProps, Omit<MuiSliderProps, "name" | "onChange" | "value" | "defaultValue"> {}

export const fieldToSlider = ({
  field,
  form: { isSubmitting },
  disabled = false,
  ...props
}: SliderProps): MuiSliderProps => {
  return {
    disabled: isSubmitting || disabled,
    ...props,
    ...field,
    name: field.name,
    value: field.value
  };
};

export const Slider: React.ComponentType<SliderProps> = (props: SliderProps) => (
  <MuiSlider {...fieldToSlider(props)}
    onChange={(e, value) => props.form.setFieldValue(props.field.name, value)} />
);

Slider.displayName = "FormikMaterialUISlider";

@cliedeman
Copy link
Collaborator

@jan-tee example looks good. Please re-open if you have any other questions

@k7en
Copy link
Author

k7en commented Feb 4, 2020

Sorry for the late reply. Thanks for your answer.

@helixbass
Copy link

I'd suggest that a more robust variation of @jan-tee's solution would be eg this instead:

...

onChange={(e, value) =>
  props.field.onChange({
    ...e,
    target: {
      ...e.target,
      name: props.field.name,
      value
    },
  })
}

So basically "polyfilling" name onto the event target and forwarding that on to Formik's onChange() handler (rather than hardcoding the internal behavior of that onChange() handler, ie setFieldValue())

I ran into this with <ToggleButtonGroup>. Also needed to use similar technique for onBlur:

onBlur={(e) =>
  props.field.onBlur({
    ...e,
    target: {
      ...e.target,
      name: props.field.name,
    },
  })
}

@CharlestonREM
Copy link

CharlestonREM commented Feb 10, 2021

requesting some context for implementation

how do I correctly implement @jan-tee 's answer? I am using formik-material-ui's codesandbox implementation below:

here is a sandbox of attempted implementation

https://codesandbox.io/s/nostalgic-water-xjjdt?file=/src/index.tsx

  • The onChange handler does not seem to be working...

@cliedeman
Copy link
Collaborator

@CharlestonREM here is a working example - https://codesandbox.io/s/falling-darkness-qk3h2

You need to wrap the slider in a formik field

<Field
              component={Slider}
              name="testSlider"
              valueLabelFormat={valueLabelFormat}
              getAriaValueText={valuetext}
              aria-labelledby="discrete-slider-restrict"
              step={null}
              valueLabelDisplay="on"
              marks={marks}
            />

Also I would rely on formik for the default value

@CharlestonREM
Copy link

THANK YOU!

@cliedeman , thank you so much for your quick reply.

I forked your sandbox and made these changes:
https://codesandbox.io/s/floral-voice-5rsg3?file=/src/index.tsx

  1. Also I would rely on formik for the default value

    1. I removed defaultValue={20} from the attributes on the Field component
    2. To test that I am understanding you, I then changed the initialValues.testSlider property to a different value, and noted that the intended defaultValue behavior of mui is intact

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants