-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [WD-18264] CMS fields for storage pool source
Signed-off-by: Nkeiruka <[email protected]>
- Loading branch information
Showing
11 changed files
with
327 additions
and
54 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import { FC, Fragment, useEffect, useState } from "react"; | ||
import { CheckboxInput, Input } from "@canonical/react-components"; | ||
import ResourceLink from "components/ResourceLink"; | ||
import FormEditButton from "components/FormEditButton"; | ||
import { ClusterSpecificValues } from "components/ClusterSpecificSelect"; | ||
|
||
interface Props { | ||
id: string; | ||
isReadOnly: boolean; | ||
onChange: (value: ClusterSpecificValues) => void; | ||
toggleReadOnly: () => void; | ||
memberNames: string[]; | ||
values?: ClusterSpecificValues; | ||
canToggleSpecific?: boolean; | ||
isDefaultSpecific?: boolean; | ||
clusterMemberLinkTarget?: (member: string) => string; | ||
disabled?: boolean; | ||
helpText?: string; | ||
} | ||
|
||
const ClusterSpecificInput: FC<Props> = ({ | ||
values, | ||
id, | ||
isReadOnly, | ||
memberNames, | ||
onChange, | ||
toggleReadOnly, | ||
canToggleSpecific = true, | ||
isDefaultSpecific = null, | ||
clusterMemberLinkTarget = () => "/ui/cluster", | ||
disabled = false, | ||
helpText, | ||
}) => { | ||
const [isSpecific, setIsSpecific] = useState<boolean | null>( | ||
isDefaultSpecific, | ||
); | ||
const firstValue = Object.values(values ?? {})[0]; | ||
|
||
useEffect(() => { | ||
if (isSpecific === null && values) { | ||
const newDefaultSpecific = !Object.values(values).some( | ||
(item) => item !== Object.values(values)[0], | ||
); | ||
setIsSpecific(newDefaultSpecific); | ||
} | ||
}, [isSpecific, values]); | ||
|
||
const setValueForAllMembers = (value: string) => { | ||
const update: ClusterSpecificValues = {}; | ||
memberNames.forEach((member) => (update[member] = value)); | ||
onChange(update); | ||
}; | ||
|
||
const setValueForMember = (value: string, member: string) => { | ||
const update = { | ||
...values, | ||
[member]: value, | ||
}; | ||
onChange(update); | ||
}; | ||
|
||
return ( | ||
<div className="u-sv3"> | ||
{canToggleSpecific && !isReadOnly && ( | ||
<CheckboxInput | ||
id={`${id}-same-for-all-toggle`} | ||
label="Same for all cluster members" | ||
checked={!isSpecific} | ||
onChange={() => { | ||
setIsSpecific((val) => !val); | ||
}} | ||
/> | ||
)} | ||
{isSpecific && ( | ||
<div className="cluster-specific-input"> | ||
{memberNames.map((item) => { | ||
const activeValue = values?.[item]; | ||
|
||
return ( | ||
<Fragment key={item}> | ||
<div className="cluster-specific-member"> | ||
<ResourceLink | ||
type="cluster-member" | ||
value={item} | ||
to={clusterMemberLinkTarget(item)} | ||
/> | ||
</div> | ||
<div className="cluster-specific-value"> | ||
{isReadOnly ? ( | ||
<> | ||
{activeValue} | ||
<FormEditButton toggleReadOnly={toggleReadOnly} /> | ||
</> | ||
) : ( | ||
<Input | ||
id={ | ||
memberNames.indexOf(item) === 0 ? id : `${id}-${item}` | ||
} | ||
type="text" | ||
className="u-no-margin--bottom" | ||
value={activeValue} | ||
onChange={(e) => setValueForMember(e.target.value, item)} | ||
disabled={disabled} | ||
// help={ | ||
// disabled && | ||
// memberNames.indexOf(item) === memberNames.length - 1 && | ||
// helpText | ||
// } | ||
/> | ||
)} | ||
</div> | ||
</Fragment> | ||
); | ||
})} | ||
<div className="p-form-help-text">{helpText}</div> | ||
</div> | ||
)} | ||
{!isSpecific && ( | ||
<div> | ||
{isReadOnly ? ( | ||
<> | ||
{firstValue} | ||
<FormEditButton toggleReadOnly={toggleReadOnly} /> | ||
</> | ||
) : ( | ||
<Input | ||
id={id} | ||
type="text" | ||
value={firstValue} | ||
onChange={(e) => setValueForAllMembers(e.target.value)} | ||
disabled={disabled} | ||
help={disabled && helpText} | ||
/> | ||
)} | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default ClusterSpecificInput; |
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.