代码之家  ›  专栏  ›  技术社区  ›  Sia

使用React Stripe元素时,如何向我的Card元素添加自定义字体?

  •  7
  • Sia  · 技术社区  · 7 年前

    我使用的是react stripe元素,但自述文件没有解释如何添加自定义字体。

    我有一个容器:

      <StripeProvider apiKey={stripePubkey}>
        <Elements>
          <Checkout {...this.props} />
        </Elements>
      </StripeProvider>
    

    然后在我的收银台内,我有一个包装信用卡输入:

    <form id="payment-form" onSubmit={this.onSubmit}>
       <CreditCardInput />
    </form>
    

    然后我的包装器信用卡输入是:

      <label>
        Card info
        <CardElement onChange={this.onCardChange} style={style} />
        <div id="card-errors" role="alert">{this.state.error.message}</div>
      </label>
    

    传递到 <CardElement /> :

    const style = {
      base: {
        color: '#232e35',
        fontFamily: '"Podkova", "Courier New", serif',
        fontSize: '16px',
        '::placeholder': {
          color: '#9ab4b0'
        }
      },
      invalid: {
        color: '#cf3100',
        iconColor: '#cf3100'
      }
    };
    
    2 回复  |  直到 7 年前
        1
  •  8
  •   Sia    7 年前

    结果是 stripe.elements([options]) Stripe API 映射到 <Elements /> react stripe元素中的组件。因此,您可以添加 cssSrc fonts 选项/属性如下:

      render() {
        const fonts = [{ cssSrc: "https://fonts.googleapis.com/css?family=Podkova:400" }]
    
        return (
          <StripeProvider apiKey={stripePubkey}>
            <Elements fonts={fonts}>
              <Checkout {...this.props} />
            </Elements>
          </StripeProvider>
        )
      }
    
        2
  •  3
  •   Mahesh Samudra    4 年前

    我不得不对 Sja's answer 让它在我的情况下发挥作用。(顺便说一句,Im使用loadStripe而不是StripeProvider。)

    const options = {
      fonts: [
        {
          src: require("path-to-font"),
          family: "Fontname",
        },
      ],
    }
    
    <Elements stripe={loadStripe(stripe_public_key)} options={options}>
    

    这里也提到了这一点: https://github.com/stripe/react-stripe-elements/issues/285