11import React , { useEffect } from "react" ;
22import { FormItemProps , FormOption } from "@/components/form/types" ;
3- import { CheckList , Form , Popup , SearchBar } from "antd-mobile" ;
4- import { RightOutline } from "antd-mobile-icons" ;
3+ import { Button , CheckList , Form , Popup , SearchBar } from "antd-mobile" ;
4+ import { RightOutline , SetOutline } from "antd-mobile-icons" ;
55import formFieldInit from "@/components/form/common" ;
6+ import { FormAction } from "@/components/form/index" ;
67import "./form.scss" ;
78
89
@@ -14,7 +15,6 @@ const valueToForm = (value: string) => {
1415}
1516
1617const formToValue = ( value : string [ ] ) => {
17- console . log ( 'select' , value ) ;
1818 if ( value && value . length > 0 ) {
1919 return value . join ( "," )
2020 }
@@ -34,6 +34,8 @@ const FormSelect: React.FC<FormItemProps> = (props) => {
3434
3535 const [ selected , setSelected ] = React . useState < string [ ] > ( currentValue ) ;
3636
37+ const [ settingOptionVisible , setSettingOptionVisible ] = React . useState ( false ) ;
38+
3739 // 当前页面展示的选项的数据,会随着树级目录进入子数据,从而更新数据
3840 const [ options , setOptions ] = React . useState ( props . options ) ;
3941
@@ -150,6 +152,23 @@ const FormSelect: React.FC<FormItemProps> = (props) => {
150152 }
151153 } , [ visible ] ) ;
152154
155+ const selectOptionFormEditAction = React . useRef < FormAction > ( null ) ;
156+
157+
158+ const handlerOptionFormFinish = ( ) => {
159+ if ( props . onSelectOptionFormFinish && selectOptionFormEditAction . current && formAction ) {
160+ props . onSelectOptionFormFinish (
161+ formAction ,
162+ selectOptionFormEditAction . current ,
163+ reloadOptions ,
164+ ( ) => {
165+ setSettingOptionVisible ( false ) ;
166+ setVisible ( false ) ;
167+ }
168+ ) ;
169+ }
170+ }
171+
153172 return (
154173 < Form . Item
155174 name = { props . name }
@@ -192,21 +211,37 @@ const FormSelect: React.FC<FormItemProps> = (props) => {
192211 > 取消</ a >
193212 < a
194213 onClick = { ( ) => {
195- formAction ?. setFieldValue ( props . name , formToValue ( selected ) ) ;
196- props . onChange && props . onChange ( selected , formAction ) ;
197- setVisible ( false ) ;
214+ if ( props . selectOptionFormEditable ) {
215+ handlerOptionFormFinish ( ) ;
216+ } else {
217+ formAction ?. setFieldValue ( props . name , formToValue ( selected ) ) ;
218+ props . onChange && props . onChange ( selected , formAction ) ;
219+ setVisible ( false ) ;
220+ }
198221 } }
199222 > 确定</ a >
200223 </ div >
201- < div className = { "select-popup-search" } >
202- < SearchBar
203- placeholder = '输入查询选项'
204- value = { searchText }
205- onChange = { v => {
206- setSearchText ( v ) ;
207- } }
208- />
209- </ div >
224+ { ! settingOptionVisible && (
225+ < div className = { "select-popup-search" } >
226+ < SearchBar
227+ className = { "select-popup-search-bar" }
228+ placeholder = '输入查询选项'
229+ value = { searchText }
230+ onChange = { v => {
231+ setSearchText ( v ) ;
232+ } }
233+ />
234+ { props . selectOptionFormEditable && (
235+ < SetOutline
236+ className = { "select-popup-search-button" }
237+ onClick = { ( ) => {
238+ setSettingOptionVisible ( ! settingOptionVisible ) ;
239+ } }
240+ />
241+ ) }
242+ </ div >
243+ ) }
244+
210245 { paths . length > 0 && (
211246 < div className = { "select-popup-navbar" } >
212247 { paths . map ( ( item , index ) => {
@@ -227,41 +262,67 @@ const FormSelect: React.FC<FormItemProps> = (props) => {
227262 ) }
228263
229264 < div className = { "select-popup-content" } >
230- < CheckList
231- className = { "select-popup-content-list" }
232- value = { selected }
233- onChange = { ( value ) => {
234- const currentValue = value as string [ ] ;
235- setSelected ( currentValue ) ;
236- // 单选时,选中即关闭弹框
237- if ( ! props . selectMultiple ) {
238- formAction ?. setFieldValue ( props . name , formToValue ( currentValue ) ) ;
239- props . onChange && props . onChange ( formToValue ( currentValue ) , formAction ) ;
240265
241- setVisible ( false ) ;
242- }
243- } }
244- multiple = { props . selectMultiple }
245- >
246- { options
247- && options
248- . filter ( item => {
249- if ( searchText ) {
250- if ( item . value . toUpperCase ( ) . includes ( searchText . toUpperCase ( ) ) ) {
251- return true ;
252- }
253- if ( item . label . toUpperCase ( ) . includes ( searchText . toUpperCase ( ) ) ) {
254- return true ;
266+ { settingOptionVisible && (
267+ < div className = { "select-popup-content-custom-form" } >
268+ { props . selectOptionFormEditView && (
269+ < props . selectOptionFormEditView formAction = { selectOptionFormEditAction } />
270+ ) }
271+ < div className = { "select-popup-content-custom-footer" } >
272+ < Button
273+ size = { 'middle' }
274+ color = { 'primary' }
275+ onClick = { ( ) => {
276+ handlerOptionFormFinish ( ) ;
277+ } }
278+ > 添加选项</ Button >
279+ < Button
280+ size = { 'middle' }
281+ onClick = { ( ) => {
282+ setSettingOptionVisible ( false ) ;
283+ } } > 取消添加</ Button >
284+ </ div >
285+
286+ </ div >
287+ ) }
288+
289+ { ! settingOptionVisible && (
290+ < CheckList
291+ className = { "select-popup-content-list" }
292+ value = { selected }
293+ onChange = { ( value ) => {
294+ const currentValue = value as string [ ] ;
295+ setSelected ( currentValue ) ;
296+ // 单选时,选中即关闭弹框
297+ if ( ! props . selectMultiple ) {
298+ formAction ?. setFieldValue ( props . name , formToValue ( currentValue ) ) ;
299+ props . onChange && props . onChange ( formToValue ( currentValue ) , formAction ) ;
300+
301+ setVisible ( false ) ;
302+ }
303+ } }
304+ multiple = { props . selectMultiple }
305+ >
306+ { options
307+ && options
308+ . filter ( item => {
309+ if ( searchText ) {
310+ if ( item . value . toUpperCase ( ) . includes ( searchText . toUpperCase ( ) ) ) {
311+ return true ;
312+ }
313+ if ( item . label . toUpperCase ( ) . includes ( searchText . toUpperCase ( ) ) ) {
314+ return true ;
315+ }
316+ return false ;
255317 }
256- return false ;
257- }
258- return true ;
259- } ) . map ( ( item ) => {
260- return (
261- < CheckboxItem { ...item } />
262- )
263- } ) }
264- </ CheckList >
318+ return true ;
319+ } ) . map ( ( item ) => {
320+ return (
321+ < CheckboxItem { ...item } />
322+ )
323+ } ) }
324+ </ CheckList >
325+ ) }
265326 </ div >
266327 </ Popup >
267328 </ Form . Item >
0 commit comments