react-stripe-elements icon indicating copy to clipboard operation
react-stripe-elements copied to clipboard

Made all functional props nullable

Open n-sviridenko opened this issue 7 years ago • 5 comments

I have a case when I need to clone an <*Element /> component with overriding of its properties with less priority. For example:

import React from 'react';

function SmartElement({ children }) {
  return React.cloneElement(children, { onChange: () => console.log('change'), ...children.props });
}

In this case, my onChange will not be called because of defaultProps.onChange of the *Element. So { ...{ onChange: myFunc }, ...{ onChange: noop } } will return { onChange: noop }.

A real use case:

https://github.com/callemall/material-ui/blob/v0.19.2/src/TextField/TextField.js#L462-L467

const inputProps = {
  id: inputId,
  ref: (elem) => this.input = elem,
  disabled: this.props.disabled,
  onBlur: this.handleInputBlur,
  onChange: this.handleInputChange,
  onFocus: this.handleInputFocus,
};

// ...

inputElement = React.cloneElement(children, {
  ...inputProps,
  ...children.props, // it overrides `inputProps`
  style: Object.assign(childStyleMerged, children.props.style),
});

Usage:

<TextField>
  <CardNumberElement />
</TextField>

n-sviridenko avatar Sep 23 '17 08:09 n-sviridenko

Hi!

Is there any reason you can't flip the two deconstructions? Is it because you still want children.props to override inputProps in some cases?

inputElement = React.cloneElement(children, {
  ...children.props,
  ...inputProps,
  style: Object.assign(childStyleMerged, children.props.style),
});

michelle-stripe avatar Oct 06 '17 16:10 michelle-stripe

Hi @michelle-stripe, it is an open source ui library and not my business logic part, unfortunately.

n-sviridenko avatar Oct 06 '17 16:10 n-sviridenko

OH, I see :(.

michelle-stripe avatar Oct 06 '17 17:10 michelle-stripe

Thanks for the PR! I just have one nit. We recently added the PaymentRequestButtonElement -- do you mind making these changes there as well?

michelle-stripe avatar Oct 06 '17 17:10 michelle-stripe

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

CLAassistant avatar Aug 07 '20 18:08 CLAassistant