【问题标题】:StripeElements won't styleStripeElements 不会设置样式
【发布时间】:2018-09-22 21:45:46
【问题描述】:

我的 Stripe 表单如下所示,可以正常加载

  render() {
    const createOptions = (fontSize: string) => {
      return {
        style: {
          base: {
            fontSize,
            color: '#424770',
            letterSpacing: '0.025em',
            fontFamily: 'Source Code Pro, monospace',
            '::placeholder': {
              color: '#aab7c4',
            },
          },
          invalid: {
            color: '#9e2146',
          },
        },
      };
    };

    return (
      <div className={styles.cardsection}>
        <form onSubmit={this.handleSubmit}>
        <label>
        Card Details
            <CardElement {...createOptions(this.props.fontSize)} />
          </label>
          <button>
            {this.state.paid === 'waiting' ? 'Please wait...' : 'Confirm order'}
          </button>
        </form>
      </div>
    );
  }
}

export default injectStripe(CheckoutForm);

我有我的 styles.less 文件:

.cardsection {
  width: 60%;
  margin: auto;
  margin-top: 30px;
  background-color: #f6f9fc;
  padding: 20px;

  label {
    color: #6b7c93;
    font-weight: 300;
    letter-spacing: 0.025em;

    .StripeElement {
      display: block;
      margin: 10px 0 20px 0;
      max-width: 500px;
      padding: 10px 14px;
      box-shadow: rgba(50, 50, 93, 0.14902) 0px 1px 3px, rgba(0, 0, 0, 0.0196078) 0px 1px 0px;
      border-radius: 4px;
      background: white;
    }

    .StripeElement--focus {
      box-shadow: rgba(50, 50, 93, 0.109804) 0px 4px 6px, rgba(0, 0, 0, 0.0784314) 0px 1px 3px;
      -webkit-transition: all 150ms ease;
      transition: all 150ms ease;
    }
  }
}

button {
  white-space: nowrap;
  border: 0;
  outline: 0;
  display: inline-block;
  height: 40px;
  line-height: 40px;
  padding: 0 14px;
  box-shadow: 0 4px 6px rgba(50, 50, 93, .11), 0 1px 3px rgba(0, 0, 0, .08);
  color: #fff;
  border-radius: 4px;
  font-size: 15px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.025em;
  background-color: #6772e5;
  text-decoration: none;
  -webkit-transition: all 150ms ease;
  transition: all 150ms ease;
  margin-top: 10px;
}

button:hover {
  color: #fff;
  cursor: pointer;
  background-color: #7795f8;
  transform: translateY(-1px);
  box-shadow: 0 7px 14px rgba(50, 50, 93, .10), 0 3px 6px rgba(0, 0, 0, .08);
}

由于某种原因,我无法设置 StripeElements 的样式。我所有的其他造型都很好,但条纹元素却不行。我检查了加载的 HTML,标签内是我的名为 StripeElement 的 div

从下图可以看出,输入框是没有样式的。

我正在使用 React-Stripe-Element 库,它使用 Next.js 在 SSR 应用程序上实现

编辑:我发现了问题,但我不知道如何克服它。 当我的应用程序被构建时,我的类在 css 和 html 中都被分配了一个随机变量。

例如,cardsection 变为 cardsection__Xy12xg2。问题是在我的 HTML 中,StripeElement 保持为 StripeElement,但我的 CSS 变为 StripeElement__28vnshY

任何想法如何克服这个问题?

【问题讨论】:

    标签: reactjs next.js stripe-payments


    【解决方案1】:

    解决方法是将StripeElement类作为属性直接传入Stripe组件:

        <CardElement {...createOptions(this.props.fontSize)} className={styles.StripeElement} />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 2011-12-24
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      • 2014-09-24
      • 1970-01-01
      相关资源
      最近更新 更多