-
Notifications
You must be signed in to change notification settings - Fork 479
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
155715d
commit 86a32bb
Showing
6 changed files
with
452 additions
and
209 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
xinference/web/ui/src/scenes/register_model/components/addStop.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import AddIcon from '@mui/icons-material/Add' | ||
import DeleteIcon from '@mui/icons-material/Delete' | ||
import { Alert, Button, TextField } from '@mui/material' | ||
import React, { useEffect, useState } from 'react' | ||
|
||
const regex = /^[1-9]\d*$/ | ||
|
||
const AddStop = ({ label, onGetData, arrItemType, formData, onGetError }) => { | ||
const [dataArr, setDataArr] = useState(formData?.length ? formData : ['']) | ||
const arr = [] | ||
|
||
useEffect(() => { | ||
if (arrItemType === 'number') { | ||
const newDataArr = dataArr.map((item) => { | ||
if (item && regex.test(item)) { | ||
arr.push('true') | ||
return Number(item) | ||
} | ||
if (item && !regex.test(item)) arr.push('false') | ||
return item | ||
}) | ||
onGetError(arr) | ||
onGetData(newDataArr) | ||
} else { | ||
onGetData(dataArr) | ||
} | ||
}, [dataArr]) | ||
|
||
const handleChange = (value, index) => { | ||
const arr = [...dataArr] | ||
arr[index] = value | ||
setDataArr([...arr]) | ||
} | ||
|
||
const handleAdd = () => { | ||
if (dataArr[dataArr.length - 1]) { | ||
setDataArr([...dataArr, '']) | ||
} | ||
} | ||
|
||
const handleDelete = (index) => { | ||
setDataArr(dataArr.filter((_, subIndex) => index !== subIndex)) | ||
} | ||
|
||
const handleShowAlert = (item) => { | ||
return item !== '' && !regex.test(item) && arrItemType === 'number' | ||
} | ||
|
||
return ( | ||
<> | ||
<div> | ||
<div | ||
style={{ display: 'flex', alignItems: 'center', marginBottom: 10 }} | ||
> | ||
<label>{label}</label> | ||
<Button | ||
variant="contained" | ||
size="small" | ||
endIcon={<AddIcon />} | ||
className="addBtn" | ||
onClick={handleAdd} | ||
> | ||
more | ||
</Button> | ||
</div> | ||
<div | ||
style={{ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
gap: 10, | ||
marginInline: 50, | ||
padding: 20, | ||
backgroundColor: '#eee', | ||
borderRadius: 10, | ||
}} | ||
> | ||
{dataArr.map((item, index) => ( | ||
<div key={index}> | ||
<div style={{ display: 'flex', alignItems: 'center', gap: 5 }}> | ||
<TextField | ||
value={item} | ||
onChange={(e) => handleChange(e.target.value, index)} | ||
size="small" | ||
style={{ width: '100%' }} | ||
/> | ||
{dataArr.length > 1 && ( | ||
<DeleteIcon | ||
onClick={() => handleDelete(index)} | ||
style={{ cursor: 'pointer', color: '#1976d2' }} | ||
/> | ||
)} | ||
</div> | ||
|
||
{handleShowAlert(item) && ( | ||
<Alert severity="error"> | ||
Please enter an integer greater than 0. | ||
</Alert> | ||
)} | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
</> | ||
) | ||
} | ||
|
||
export default AddStop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.