新聞中心
這篇文章將為大家詳細講解有關(guān)Formik中如何使用react-select,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),安寧企業(yè)網(wǎng)站建設(shè),安寧品牌網(wǎng)站建設(shè),網(wǎng)站定制,安寧網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,安寧網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
react-select簡介
React-Select是github上一個極其火的控件庫,星數(shù)達到13004,它是React開發(fā)中幾乎是你必需打交道的一個內(nèi)容。React Select的基本功能實現(xiàn)的是一個表單中的常見的下拉列表框控件,其實它的功能擴展來看遠遠不止如此,它支持:
多選
樣式定制
多級聯(lián)動選擇
異步加載
等等。
但是,如果在你的開發(fā)中使用的是一個很基礎(chǔ)性的下拉列表框,那么你可以直接使用類似于Semantic UI或者是Material React控件庫的Select組件,甚至是最基本的HTML5組件中的那個。
值得注意的是,如如今react-select組件從1.0升級到2.0,變化了巨大變化。有關(guān)細節(jié)請參考官方網(wǎng)站,總起來看越升級越容易使用,功能也越強大。
在Formik中使用react-select組件
Formik官方也提供了一個使用react-select組件的基本例子,但是使用的是react-select組件的Ver 1.x。在1.x版本時期,組件的樣式是使用css方案定義的,但是升級到2.0后,樣式使用了更為先進且更迎合React開發(fā)思想的Emotion這個開源庫(使用JSX組件思想——CSS-in-JS——開發(fā)樣式)。由于Formik官方提供的相關(guān)實例極為簡單,所以幾需要作兩處修改即可。
工程名稱:formik-09x-react-select-example,主要文件:index.js
修改后完整代碼如下:
import './formik-demo.css';
import React from 'react';
import { render } from 'react-dom';
import { withFormik } from 'formik';
//ERROR NOW: import Yup from 'yup';==>changed into the following
import * as Yup from 'yup';
import Select from 'react-select';
//NOT SUPPORTED IN VERSION 2.X.
// Styles are now implemented with css-in-js rather than less / scss stylesheets
//import 'react-select/dist/react-select.css';
// Helper styles for demo
import './formik-demo.css';
import {
MoreResources,
DisplayFormikState,
} from './formik-helper';
import SimpleSelect from './SimpleSelect'
import AnimatedMulti from './AnimationMultiSelect'
const formikEnhancer = withFormik({
mapPropsToValues: props => ({
email: '',
topics: []
}),
validationSchema: Yup.object().shape({
email: Yup.string()
.email('Invalid email address')
.required('Email is required!'),
topics: Yup.array()
.min(3, 'Pick at least 3 tags')
.of(
Yup.object().shape({
label: Yup.string().required(),
value: Yup.string().required(),
})
),
}),
handleSubmit: (values, { setSubmitting }) => {
const payload = {
...values,
topics: values.topics.map(t => t.value),
};
setTimeout(() => {
alert(JSON.stringify(payload, null, 2));
setSubmitting(false);
}, 1000);
},
displayName: 'MyForm'
});
const MyForm = props => {
const {
values,
touched,
dirty,
errors,
handleChange,
handleBlur,
handleSubmit,
handleReset,
setFieldValue,
setFieldTouched,
isSubmitting,
} = props;
return (
);
};
const options = [
{ value: 'Food', label: 'Food' },
{ value: 'Being Fabulous', label: 'Being Fabulous' },
{ value: 'Ken Wheeler', label: 'Ken Wheeler' },
{ value: 'ReasonML', label: 'ReasonML' },
{ value: 'Unicorns', label: 'Unicorns' },
{ value: 'Kittens', label: 'Kittens' },
];
class MySelect extends React.Component {
handleChange = value => {
// this is going to call setFieldValue and manually update values.topcis
this.props.onChange('topics', value);
};
handleBlur = () => {
// this is going to call setFieldTouched and manually update touched.topcis
this.props.onBlur('topics', true);
};
render() {
return (
Using{' '} Formik {' '} with 3rd-party input components
This example shows to use Formik with a 3rd-party
input component. The trick is to use Formik's{' '}
setFieldValue prop and a custom component
class whenever you need a custom change handler.{' '}
To show this off, below is a Formik-enhanced form. It
has a "vanilla" Formik input for email{' '}
and a custom select component for topics{' '}
that uses Jed Watson's {' '}
react-select
{' '}
library.
第一處修改是屏蔽css文件的導入引用,如下:
//import 'react-select/dist/react-select.css';
第二處更為簡單,只需要把組件中的屬性表達方式修改一下,即把multi={true}修改為isMulti就可以了,如下:
另外還添加了兩個我自己加入的react-select組件,分別是SimpleSelect和AnimationMultiSelect,它們各自的源碼如下,請參考:
//SimpleSelect.js
import React,{Component} from 'react'
import Select from 'react-select'
const options=[
{value:'liu',label:'劉備'},
{value:'guan',label:'關(guān)羽'},
{value:'zhang',label:'張飛'}
]
const SimpleSelect=()=>(
)
export default SimpleSelect//AnimationMultiSelect
import React from 'react';
import Select from 'react-select';
import makeAnimated from 'react-select/lib/animated';
// import { colourOptions } from '../data';
const colourOptions=[
{value:'c1',label:'劉備'},
{value:'c2',label:'關(guān)羽1'},
{value:'c3',label:'關(guān)羽2'},
{value:'c4',label:'關(guān)羽3'},
{value:'c5',label:'張飛'}
]
export default function AnimatedMulti() {
return (
);
}關(guān)于“Formik中如何使用react-select”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
本文標題:Formik中如何使用react-select
網(wǎng)頁網(wǎng)址:http://www.dlmjj.cn/article/pciipd.html


咨詢
建站咨詢
