-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: create baseOptionsEditor and delete duplicates (#4041)
- Loading branch information
Showing
7 changed files
with
182 additions
and
212 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
132 changes: 37 additions & 95 deletions
132
editor.planx.uk/src/@planx/components/Checklist/Editor/OptionsEditor.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 |
---|---|---|
@@ -1,104 +1,46 @@ | ||
import { | ||
BaseOptionsEditor, | ||
BaseOptionsEditorProps, | ||
} from "@planx/components/shared/BaseOptionsEditor"; | ||
import React from "react"; | ||
import ImgInput from "ui/editor/ImgInput/ImgInput"; | ||
import SimpleMenu from "ui/editor/SimpleMenu"; | ||
import Input from "ui/shared/Input/Input"; | ||
import InputRow from "ui/shared/InputRow"; | ||
import InputRowItem from "ui/shared/InputRowItem"; | ||
|
||
import { FlagsSelect } from "../../shared/FlagsSelect"; | ||
import { OptionEditorProps } from "../types"; | ||
export type ChecklistOptionsEditorProps = BaseOptionsEditorProps & { | ||
index: number; | ||
groupIndex?: number; | ||
groups?: Array<string>; | ||
onMoveToGroup?: (itemIndex: number, groupIndex: number) => void; | ||
showValueField?: boolean; | ||
}; | ||
|
||
export const OptionEditor: React.FC<OptionEditorProps> = (props) => { | ||
const ChecklistOptionsEditor: React.FC<ChecklistOptionsEditorProps> = ({ | ||
value, | ||
onChange, | ||
showValueField = false, | ||
groups, | ||
onMoveToGroup, | ||
index, | ||
}) => { | ||
return ( | ||
<div style={{ width: "100%" }}> | ||
<InputRow> | ||
{props.value.id ? ( | ||
<input type="hidden" value={props.value.id} readOnly /> | ||
) : null} | ||
<InputRowItem width="200%"> | ||
<Input | ||
required | ||
format="bold" | ||
multiline | ||
value={props.value.data.text || ""} | ||
onChange={(ev) => { | ||
props.onChange({ | ||
...props.value, | ||
data: { | ||
...props.value.data, | ||
text: ev.target.value, | ||
}, | ||
}); | ||
}} | ||
placeholder="Option" | ||
/> | ||
</InputRowItem> | ||
|
||
<ImgInput | ||
img={props.value.data.img} | ||
onChange={(img) => { | ||
props.onChange({ | ||
...props.value, | ||
data: { | ||
...props.value.data, | ||
img, | ||
}, | ||
}); | ||
}} | ||
<BaseOptionsEditor | ||
value={value} | ||
onChange={onChange} | ||
showValueField={showValueField} | ||
> | ||
{typeof index !== "undefined" && groups && onMoveToGroup && ( | ||
<SimpleMenu | ||
items={groups.map((group, groupIndex) => ({ | ||
label: `Move to ${group || `group ${groupIndex}`}`, | ||
onClick: () => { | ||
if (onMoveToGroup && typeof index === "number") | ||
onMoveToGroup(index, groupIndex); | ||
}, | ||
disabled: groupIndex === groupIndex, | ||
}))} | ||
/> | ||
|
||
{typeof props.index !== "undefined" && | ||
props.groups && | ||
props.onMoveToGroup && ( | ||
<SimpleMenu | ||
items={props.groups.map((group, groupIndex) => ({ | ||
label: `Move to ${group || `group ${groupIndex}`}`, | ||
onClick: () => { | ||
props.onMoveToGroup && | ||
typeof props.index === "number" && | ||
props.onMoveToGroup(props.index, groupIndex); | ||
}, | ||
disabled: groupIndex === props.groupIndex, | ||
}))} | ||
/> | ||
)} | ||
</InputRow> | ||
|
||
{props.showValueField && ( | ||
<InputRow> | ||
<Input | ||
format="data" | ||
value={props.value.data.val || ""} | ||
placeholder="Data Value" | ||
onChange={(ev) => { | ||
props.onChange({ | ||
...props.value, | ||
data: { | ||
...props.value.data, | ||
val: ev.target.value, | ||
}, | ||
}); | ||
}} | ||
/> | ||
</InputRow> | ||
)} | ||
|
||
<FlagsSelect | ||
value={ | ||
Array.isArray(props.value.data.flag) | ||
? props.value.data.flag | ||
: [props.value.data.flag] | ||
} | ||
onChange={(ev) => { | ||
props.onChange({ | ||
...props.value, | ||
data: { | ||
...props.value.data, | ||
flag: ev, | ||
}, | ||
}); | ||
}} | ||
/> | ||
</div> | ||
</BaseOptionsEditor> | ||
); | ||
}; | ||
|
||
export default ChecklistOptionsEditor; |
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
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
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.