|
| 1 | +import { FormValues, ParameterName } from "@azure/bonito-core/lib/form"; |
| 2 | +import { IconButton } from "@fluentui/react/lib/Button"; |
| 3 | +import { Stack } from "@fluentui/react/lib/Stack"; |
| 4 | +import { TextField } from "@fluentui/react/lib/TextField"; |
| 5 | +import * as React from "react"; |
| 6 | +import { useCallback, useMemo } from "react"; |
| 7 | +import { useFormParameter, useUniqueId } from "../../hooks"; |
| 8 | +import { FormControlProps } from "./form-control"; |
| 9 | +import { useAppTheme } from "../../theme"; |
| 10 | +import { translate } from "@azure/bonito-core"; |
| 11 | + |
| 12 | +export interface StringListValidationDetails { |
| 13 | + [key: number]: string; |
| 14 | +} |
| 15 | + |
| 16 | +export function StringList<V extends FormValues, K extends ParameterName<V>>( |
| 17 | + props: FormControlProps<V, K> |
| 18 | +): JSX.Element { |
| 19 | + const { className, style, param, onChange } = props; |
| 20 | + |
| 21 | + const id = useUniqueId("form-control", props.id); |
| 22 | + const validationDetails = useFormParameter(param) |
| 23 | + .validationDetails as StringListValidationDetails; |
| 24 | + |
| 25 | + const items = useMemo<string[]>(() => { |
| 26 | + const items: string[] = []; |
| 27 | + if (param.value && Array.isArray(param.value)) { |
| 28 | + for (const item of param.value) { |
| 29 | + items.push(item); |
| 30 | + } |
| 31 | + } |
| 32 | + // Add an empty item at the end |
| 33 | + items.push(""); |
| 34 | + return items; |
| 35 | + }, [param.value]); |
| 36 | + |
| 37 | + const onItemChange = useCallback( |
| 38 | + (index: number, value: string) => { |
| 39 | + const newItems = [...items]; |
| 40 | + if (index === items.length - 1) { |
| 41 | + // Last item, add a new one |
| 42 | + newItems.push(""); |
| 43 | + } |
| 44 | + newItems[index] = value; |
| 45 | + param.value = newItems.slice(0, newItems.length - 1) as V[K]; |
| 46 | + onChange?.(null, param.value); |
| 47 | + }, |
| 48 | + [items, param, onChange] |
| 49 | + ); |
| 50 | + |
| 51 | + const onItemDelete = useCallback( |
| 52 | + (index: number) => { |
| 53 | + const newItems = [...items]; |
| 54 | + newItems.splice(index, 1); |
| 55 | + param.value = newItems.slice(0, newItems.length - 1) as V[K]; |
| 56 | + onChange?.(null, param.value); |
| 57 | + }, |
| 58 | + [items, param, onChange] |
| 59 | + ); |
| 60 | + |
| 61 | + return ( |
| 62 | + <Stack key={id} style={style} className={className}> |
| 63 | + {items.map((item, index) => { |
| 64 | + const errorMsg = validationDetails?.[index]; |
| 65 | + return ( |
| 66 | + <StringListItem |
| 67 | + key={index} |
| 68 | + index={index} |
| 69 | + value={item} |
| 70 | + label={param.label} |
| 71 | + errorMsg={errorMsg} |
| 72 | + placeholder={param.placeholder} |
| 73 | + onChange={onItemChange} |
| 74 | + onDelete={onItemDelete} |
| 75 | + disableDelete={index === items.length - 1} |
| 76 | + ></StringListItem> |
| 77 | + ); |
| 78 | + })} |
| 79 | + </Stack> |
| 80 | + ); |
| 81 | +} |
| 82 | + |
| 83 | +interface StringListItemProps { |
| 84 | + index: number; |
| 85 | + value: string; |
| 86 | + label?: string; |
| 87 | + onChange: (index: number, value: string) => void; |
| 88 | + onDelete: (index: number) => void; |
| 89 | + placeholder?: string; |
| 90 | + disableDelete?: boolean; |
| 91 | + errorMsg?: string; |
| 92 | +} |
| 93 | + |
| 94 | +function StringListItem(props: StringListItemProps) { |
| 95 | + const { |
| 96 | + index, |
| 97 | + value, |
| 98 | + label, |
| 99 | + onChange, |
| 100 | + onDelete, |
| 101 | + disableDelete, |
| 102 | + errorMsg, |
| 103 | + placeholder, |
| 104 | + } = props; |
| 105 | + const styles = useStringListItemStyles(props); |
| 106 | + const ariaLabel = `${label || ""} ${index + 1}`; |
| 107 | + return ( |
| 108 | + <Stack |
| 109 | + key={index} |
| 110 | + horizontal |
| 111 | + verticalAlign="center" |
| 112 | + styles={styles.container} |
| 113 | + > |
| 114 | + <Stack.Item grow={1}> |
| 115 | + <TextField |
| 116 | + styles={styles.input} |
| 117 | + value={value} |
| 118 | + ariaLabel={ariaLabel} |
| 119 | + placeholder={placeholder} |
| 120 | + errorMessage={errorMsg} |
| 121 | + onChange={(_, newValue) => { |
| 122 | + onChange(index, newValue || ""); |
| 123 | + }} |
| 124 | + /> |
| 125 | + </Stack.Item> |
| 126 | + <IconButton |
| 127 | + styles={styles.button} |
| 128 | + iconProps={{ iconName: "Delete" }} |
| 129 | + ariaLabel={`${translate("bonito.ui.form.delete")} ${ariaLabel}`} |
| 130 | + onClick={() => { |
| 131 | + onDelete(index); |
| 132 | + }} |
| 133 | + disabled={disableDelete} |
| 134 | + /> |
| 135 | + </Stack> |
| 136 | + ); |
| 137 | +} |
| 138 | + |
| 139 | +function useStringListItemStyles(props: StringListItemProps) { |
| 140 | + const theme = useAppTheme(); |
| 141 | + const { disableDelete } = props; |
| 142 | + |
| 143 | + return React.useMemo(() => { |
| 144 | + const itemPadding = { |
| 145 | + padding: "11px 8px 11px 12px", |
| 146 | + }; |
| 147 | + return { |
| 148 | + container: { |
| 149 | + root: { |
| 150 | + ":hover": { |
| 151 | + backgroundColor: theme.palette.neutralLighter, |
| 152 | + }, |
| 153 | + }, |
| 154 | + }, |
| 155 | + input: { |
| 156 | + root: { |
| 157 | + ...itemPadding, |
| 158 | + }, |
| 159 | + field: { |
| 160 | + height: "24px", |
| 161 | + }, |
| 162 | + fieldGroup: { |
| 163 | + height: "24px", |
| 164 | + "box-sizing": "content-box", |
| 165 | + }, |
| 166 | + }, |
| 167 | + button: { |
| 168 | + root: { |
| 169 | + ...itemPadding, |
| 170 | + visibility: disableDelete ? "hidden" : "initial", |
| 171 | + }, |
| 172 | + }, |
| 173 | + }; |
| 174 | + }, [theme.palette.neutralLighter, disableDelete]); |
| 175 | +} |
0 commit comments