11import React , { useEffect } from "react" ;
22import { FormItemProps , FormOption } from "@/components/form/types" ;
3- import { Button , CheckList , Form , Popup , SearchBar } from "antd-mobile" ;
3+ import { Button , CheckList , Form , InfiniteScroll , Popup , PullToRefresh , SearchBar } from "antd-mobile" ;
44import { RightOutline , SetOutline } from "antd-mobile-icons" ;
55import formFieldInit from "@/components/form/common" ;
66import { FormAction } from "@/components/form/index" ;
@@ -21,6 +21,122 @@ const formToValue = (value: string[]) => {
2121 return value ;
2222}
2323
24+ interface CheckboxItemProps {
25+ item : FormOption ;
26+ paths : FormOption [ ] ;
27+ setPaths : ( paths : FormOption [ ] ) => void ;
28+ setOptions : ( options : FormOption [ ] ) => void ;
29+
30+ }
31+
32+ const CheckboxItem : React . FC < CheckboxItemProps > = ( props ) => {
33+ const { item, paths, setPaths, setOptions} = props ;
34+ if ( item . children && item . children . length > 0 ) {
35+ return (
36+ < CheckList . Item
37+ value = { item . value }
38+ disabled = { item . disable }
39+ readOnly = { true }
40+ >
41+ < div
42+ className = { "checkbox-parent" }
43+ onClick = { ( ) => {
44+ setPaths ( [ ...paths , item ] ) ;
45+ setOptions ( item . children || [ ] ) ;
46+ } }
47+ >
48+ { item . label }
49+ < RightOutline />
50+ </ div >
51+ </ CheckList . Item >
52+ )
53+ }
54+ return (
55+ < CheckList . Item
56+ value = { item . value }
57+ disabled = { item . disable }
58+ >
59+ { item . label }
60+ </ CheckList . Item >
61+ )
62+ }
63+
64+ interface CheckboxListViewProps {
65+ data : FormOption [ ] ;
66+ paths : FormOption [ ] ;
67+ setPaths : ( paths : FormOption [ ] ) => void ;
68+ setOptions : ( options : FormOption [ ] ) => void ;
69+ }
70+
71+ const CheckboxListView : React . FC < CheckboxListViewProps > = ( props ) => {
72+ const { data} = props ;
73+ const pageSize = 20 ;
74+
75+ const [ currentPage , setCurrentPage ] = React . useState ( 1 ) ;
76+
77+ const [ list , setList ] = React . useState < FormOption [ ] > ( [ ] ) ;
78+
79+ const [ hasMore , setHasMore ] = React . useState ( true ) ;
80+
81+ const reload = ( ) => {
82+ const currentPage = 1 ;
83+ if ( data . length > 0 ) {
84+ const list = data . slice ( ( currentPage - 1 ) * pageSize , currentPage * pageSize ) ;
85+ setList ( list ) ;
86+ setHasMore ( true ) ;
87+ } else {
88+ setList ( [ ] ) ;
89+ setHasMore ( false ) ;
90+ }
91+ setCurrentPage ( currentPage ) ;
92+ }
93+
94+ const loadMore = ( ) => {
95+ setCurrentPage ( prevState => {
96+ const newPage = prevState + 1 ;
97+ if ( newPage * pageSize >= data . length ) {
98+ setHasMore ( false ) ;
99+ } else {
100+ setHasMore ( true ) ;
101+ }
102+ setList ( prevState => {
103+ const list = data . slice ( ( newPage - 1 ) * pageSize , newPage * pageSize ) ;
104+ return [ ...prevState , ...list ]
105+ } ) ;
106+ return newPage ;
107+ } ) ;
108+ }
109+
110+
111+ useEffect ( ( ) => {
112+ reload ( ) ;
113+ } , [ props . data ] ) ;
114+
115+ return (
116+ < >
117+ < PullToRefresh
118+ onRefresh = { async ( ) => {
119+ reload ( ) ;
120+ } }
121+ >
122+ { list && list . map ( ( item : FormOption , index : number ) => {
123+ return < CheckboxItem key = { index } item = { item } { ...props } />
124+ } ) }
125+
126+ { hasMore && (
127+ < InfiniteScroll
128+ loadMore = { async ( ) => {
129+ loadMore ( ) ;
130+ } }
131+ hasMore = { hasMore }
132+ />
133+ ) }
134+
135+ </ PullToRefresh >
136+ </ >
137+ )
138+ }
139+
24140const FormSelect : React . FC < FormItemProps > = ( props ) => {
25141
26142 const [ visible , setVisible ] = React . useState ( false ) ;
@@ -97,37 +213,6 @@ const FormSelect: React.FC<FormItemProps> = (props) => {
97213 return displaySpan ( currentValue ) ;
98214 }
99215
100- const CheckboxItem : React . FC < FormOption > = ( item ) => {
101- if ( item . children && item . children . length > 0 ) {
102- return (
103- < CheckList . Item
104- value = { item . value }
105- disabled = { item . disable }
106- readOnly = { true }
107- >
108- < div
109- className = { "checkbox-parent" }
110- onClick = { ( ) => {
111- setPaths ( [ ...paths , item ] ) ;
112- setOptions ( item . children ) ;
113- } }
114- >
115- { item . label }
116- < RightOutline />
117- </ div >
118- </ CheckList . Item >
119- )
120- }
121- return (
122- < CheckList . Item
123- value = { item . value }
124- disabled = { item . disable }
125- >
126- { item . label }
127- </ CheckList . Item >
128- )
129- }
130-
131216 const reloadOptions = ( ) => {
132217 if ( props . loadOptions ) {
133218 props . loadOptions ( formAction ) . then ( list => {
@@ -155,7 +240,7 @@ const FormSelect: React.FC<FormItemProps> = (props) => {
155240 const selectOptionFormEditAction = React . useRef < FormAction > ( null ) ;
156241
157242
158- const handlerOptionFormFinish = ( ) => {
243+ const handlerOptionFormFinish = ( ) => {
159244 if ( props . onSelectOptionFormFinish && selectOptionFormEditAction . current && formAction ) {
160245 props . onSelectOptionFormFinish (
161246 formAction ,
@@ -209,11 +294,16 @@ const FormSelect: React.FC<FormItemProps> = (props) => {
209294 setVisible ( false )
210295 } }
211296 > 取消</ a >
297+ { settingOptionVisible && (
298+ < >
299+ { props . selectOptionFormEditFooterOkText || "添加选项" }
300+ </ >
301+ ) }
212302 < a
213303 onClick = { ( ) => {
214- if ( props . selectOptionFormEditable ) {
304+ if ( props . selectOptionFormEditable ) {
215305 handlerOptionFormFinish ( ) ;
216- } else {
306+ } else {
217307 formAction ?. setFieldValue ( props . name , formToValue ( selected ) ) ;
218308 props . onChange && props . onChange ( selected , formAction ) ;
219309 setVisible ( false ) ;
@@ -265,8 +355,10 @@ const FormSelect: React.FC<FormItemProps> = (props) => {
265355
266356 { settingOptionVisible && (
267357 < div className = { "select-popup-content-custom-form" } >
268- { props . selectOptionFormEditView && (
269- < props . selectOptionFormEditView formAction = { selectOptionFormEditAction } />
358+ { formAction && props . selectOptionFormEditView && (
359+ < props . selectOptionFormEditView
360+ currentAction = { selectOptionFormEditAction }
361+ formAction = { formAction } />
270362 ) }
271363 < div className = { "select-popup-content-custom-footer" } >
272364 < Button
@@ -275,12 +367,13 @@ const FormSelect: React.FC<FormItemProps> = (props) => {
275367 onClick = { ( ) => {
276368 handlerOptionFormFinish ( ) ;
277369 } }
278- > 添加选项 </ Button >
370+ > { props . selectOptionFormEditFooterOkText || "添加" } </ Button >
279371 < Button
280372 size = { 'middle' }
281373 onClick = { ( ) => {
282374 setSettingOptionVisible ( false ) ;
283- } } > 取消添加</ Button >
375+ } }
376+ > { props . selectOptionFormEditFooterOkText || "取消" } </ Button >
284377 </ div >
285378
286379 </ div >
@@ -303,24 +396,25 @@ const FormSelect: React.FC<FormItemProps> = (props) => {
303396 } }
304397 multiple = { props . selectMultiple }
305398 >
306- { options
307- && options
308- . filter ( item => {
309- if ( searchText ) {
310- if ( item . value . toUpperCase ( ) . includes ( searchText . toUpperCase ( ) ) ) {
311- return true ;
399+ < CheckboxListView
400+ data = { options && options
401+ . filter ( ( item => {
402+ if ( searchText ) {
403+ if ( item . value . toUpperCase ( ) . includes ( searchText . toUpperCase ( ) ) ) {
404+ return true ;
405+ }
406+ if ( item . label . toUpperCase ( ) . includes ( searchText . toUpperCase ( ) ) ) {
407+ return true ;
408+ }
409+ return false ;
312410 }
313- if ( item . label . toUpperCase ( ) . includes ( searchText . toUpperCase ( ) ) ) {
314- return true ;
315- }
316- return false ;
317- }
318- return true ;
319- } ) . map ( ( item , index ) => {
320- return (
321- < CheckboxItem key = { index } { ...item } />
322- )
323- } ) }
411+ return true ;
412+ } ) )
413+ || [ ] }
414+ setOptions = { setOptions }
415+ paths = { paths }
416+ setPaths = { setPaths }
417+ />
324418 </ CheckList >
325419 ) }
326420 </ div >
0 commit comments