-
Notifications
You must be signed in to change notification settings - Fork 417
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1034 implement create promotion page
- Loading branch information
1 parent
aacf57a
commit 3235386
Showing
19 changed files
with
756 additions
and
44 deletions.
There are no files selected for viewing
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
110 changes: 110 additions & 0 deletions
110
backoffice/modules/promotion/components/MultipleAutoComplete.tsx
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,110 @@ | ||
import { useState } from 'react'; | ||
import { RegisterOptions, UseFormRegister } from 'react-hook-form'; | ||
|
||
type props = { | ||
labelText: string; | ||
field: string; | ||
register: UseFormRegister<any>; | ||
registerOptions?: RegisterOptions; | ||
// error: string, | ||
defaultValue?: any; | ||
options?: any[]; | ||
fetchOptions: (data: any) => any; | ||
onSelect: (value: any) => void; | ||
onRemoveElement: (value: any) => void; | ||
optionSelectedIds: number[]; | ||
isSubmitting: boolean; | ||
}; | ||
|
||
const MultipleAutoComplete = (props: props) => { | ||
const [isFocusing, setIsFocusing] = useState(false); | ||
const [optionSelecteds, setOptionSelecteds] = useState<any[]>([]); | ||
const queryData = (query: string) => { | ||
props.fetchOptions(query); | ||
}; | ||
|
||
const handleFocus = (isFocusing: boolean) => { | ||
setTimeout(() => { | ||
if (!props.isSubmitting) { | ||
setIsFocusing(isFocusing); | ||
} else { | ||
setIsFocusing(false); | ||
} | ||
}, 150); | ||
}; | ||
|
||
const selectOption = (option: any) => { | ||
setOptionSelecteds([...optionSelecteds, option]); | ||
props.onSelect(option.id); | ||
}; | ||
|
||
const removeOption = (option: any) => { | ||
setOptionSelecteds(optionSelecteds.filter((item) => item.id !== option.id)); | ||
props.onRemoveElement(option.id); | ||
}; | ||
|
||
return ( | ||
<div className="autocomplete-container"> | ||
<label className="form-label" htmlFor={props.field}> | ||
{props.labelText} | ||
</label> | ||
<div> | ||
<input | ||
type="text" | ||
id={props.field} | ||
{...props.register(props.field, props.registerOptions)} | ||
defaultValue={props.defaultValue} | ||
onChange={(e) => queryData(e.target.value)} | ||
onFocus={() => handleFocus(true)} | ||
onBlur={() => handleFocus(false)} | ||
className="form-control" | ||
/> | ||
{isFocusing && props.options!.length > 0 && ( | ||
<div className="autocomplete-list" style={{ maxHeight: '200px', overflowY: 'scroll' }}> | ||
{props.options!.map((option, index) => ( | ||
<div | ||
key={option.id} | ||
aria-hidden="true" | ||
className={`dropdown-item ${ | ||
props.optionSelectedIds.includes(option.id) ? 'selected-options' : '' | ||
}`} | ||
onClick={() => selectOption(option)} | ||
> | ||
{option.name} | ||
</div> | ||
))} | ||
</div> | ||
)} | ||
</div> | ||
|
||
{optionSelecteds.length > 0 && ( | ||
<div className="mt-3"> | ||
<span className="form-label">Selected {props.labelText}</span> | ||
{optionSelecteds.map((option, index) => ( | ||
<div | ||
className="d-flex align-items-center" | ||
style={{ | ||
marginBottom: '5px', | ||
borderRadius: '5px', | ||
border: '1px solid #ccc', | ||
padding: '5px', | ||
}} | ||
key={option.id} | ||
> | ||
<div className="mr-3" style={{ display: 'inline', marginRight: '15px' }}> | ||
{option.name} | ||
</div> | ||
<span | ||
aria-hidden="true" | ||
className="fa fa-remove" | ||
onClick={() => removeOption(option)} | ||
></span> | ||
</div> | ||
))} | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default MultipleAutoComplete; |
Oops, something went wrong.