@@ -6,11 +6,15 @@ import {isDefined, useCurrentQuestionAttempt} from "../../services";
66import { IsaacQuestionProps } from "../../../IsaacAppTypes" ;
77import { Immutable } from "immer" ;
88import QuestionInputValidation from "../elements/inputs/QuestionInputValidation" ;
9+ import { Markup } from "../elements/markup" ;
910
1011// Custom input component for coordinates
1112interface CoordinateInputProps {
1213 value : Immutable < CoordinateItemDTO > ;
1314 placeholderValues : string [ ] ;
15+ useBrackets : boolean ;
16+ separator : string ;
17+ suffixes ?: string [ ] ;
1418 numberOfDimensions : number ;
1519 onChange : ( value : Immutable < CoordinateItemDTO > ) => void ;
1620 readonly ?: boolean ;
@@ -85,20 +89,24 @@ const cleanItem = function (item: Immutable<CoordinateItemDTO>) {
8589} ;
8690
8791const CoordinateInput = ( props : CoordinateInputProps ) => {
88- const { value, placeholderValues, numberOfDimensions, onChange, readonly, remove} = props ;
89- return < span className = "coordinate-input" > ({ [ ...Array ( numberOfDimensions ) ] . map ( ( _ , i ) =>
90- < span key = { i } >
91- < Input
92- type = "text"
93- className = "force-print"
94- placeholder = { placeholderValues [ i ] ?? "" }
95- value = { coordItemToValue ( value , i ) }
96- onChange = { event => onChange ( updateCoordItem ( value , event . target . value , i , numberOfDimensions ) ) }
97- readOnly = { readonly }
98- />
99- { ( i < numberOfDimensions - 1 ) && < span className = "coordinate-input-separator" > , </ span > }
100- </ span > ) } )
101- { remove && < Button className = "ms-3" size = "sm" onClick = { remove } > Delete</ Button > }
92+ const { value, placeholderValues, useBrackets, separator, suffixes, numberOfDimensions, onChange, readonly, remove} = props ;
93+ return < span className = "coordinate-input" >
94+ { useBrackets ? "(" : "" }
95+ { [ ...Array ( numberOfDimensions ) ] . map ( ( _ , i ) =>
96+ < span key = { i } >
97+ < Input
98+ type = "text"
99+ className = "force-print"
100+ placeholder = { placeholderValues [ i ] ?? "" }
101+ value = { coordItemToValue ( value , i ) }
102+ onChange = { event => onChange ( updateCoordItem ( value , event . target . value , i , numberOfDimensions ) ) }
103+ readOnly = { readonly }
104+ />
105+ { suffixes && suffixes [ i ] && < Markup encoding = "latex" > { suffixes [ i ] } </ Markup > }
106+ { ( i < numberOfDimensions - 1 ) && < Markup encoding = "latex" className = "coordinate-input-separator" > { separator } </ Markup > }
107+ </ span > ) }
108+ { useBrackets ? ")" : "" }
109+ { remove && < Button className = "ms-3" size = "sm" onClick = { remove } > Delete</ Button > }
102110 </ span > ;
103111} ;
104112
@@ -107,6 +115,7 @@ const IsaacCoordinateQuestion = ({doc, questionId, readonly}: IsaacQuestionProps
107115 const { currentAttempt, dispatchSetCurrentAttempt } = useCurrentQuestionAttempt < CoordinateChoiceDTO > ( questionId ) ;
108116
109117 const numberOfDimensions = doc . numberOfDimensions ?? 2 ;
118+ const buttonText = doc . buttonText ?? "Add coordinate" ;
110119
111120 const getEmptyCoordItem = useCallback ( ( ) : CoordinateItemDTO => {
112121 return { type : "coordinateItem" , coordinates : Array < string > ( numberOfDimensions ) . fill ( "" ) } ;
@@ -141,6 +150,9 @@ const IsaacCoordinateQuestion = ({doc, questionId, readonly}: IsaacQuestionProps
141150 < CoordinateInput
142151 key = { index }
143152 placeholderValues = { doc . placeholderValues ?? [ ] }
153+ useBrackets = { doc . useBrackets ?? true }
154+ separator = { doc . separator ?? "," }
155+ suffixes = { doc . suffixes }
144156 numberOfDimensions = { numberOfDimensions }
145157 value = { currentAttempt ?. items ?. [ index ] ?? getEmptyCoordItem ( ) }
146158 readonly = { readonly }
@@ -152,6 +164,9 @@ const IsaacCoordinateQuestion = ({doc, questionId, readonly}: IsaacQuestionProps
152164 < CoordinateInput
153165 key = { index }
154166 placeholderValues = { doc . placeholderValues ?? [ ] }
167+ useBrackets = { doc . useBrackets ?? true }
168+ separator = { doc . separator ?? "," }
169+ suffixes = { doc . suffixes }
155170 numberOfDimensions = { numberOfDimensions }
156171 value = { item }
157172 readonly = { readonly }
@@ -163,14 +178,19 @@ const IsaacCoordinateQuestion = ({doc, questionId, readonly}: IsaacQuestionProps
163178 : < CoordinateInput
164179 key = { 0 }
165180 placeholderValues = { doc . placeholderValues ?? [ ] }
181+ useBrackets = { doc . useBrackets ?? true }
182+ separator = { doc . separator ?? "," }
183+ suffixes = { doc . suffixes }
166184 numberOfDimensions = { numberOfDimensions }
167185 value = { getEmptyCoordItem ( ) }
168186 readonly = { readonly }
169187 onChange = { value => updateItem ( 0 , value ) }
170188 />
171189 }
172190 < QuestionInputValidation userInput = { currentAttempt ?. items ?. map ( answer => answer . coordinates ?? [ ] ) ?? [ ] } validator = { coordinateInputValidator } />
173- { ! doc . numberOfCoordinates && < Button color = "secondary" size = "sm" className = "mt-3" onClick = { ( ) => updateItem ( currentAttempt ?. items ?. length ?? 1 , getEmptyCoordItem ( ) ) } > Add coordinate</ Button > }
191+ { ! doc . numberOfCoordinates && < Button color = "secondary" size = "sm" className = "mt-3" onClick = { ( ) => updateItem ( currentAttempt ?. items ?. length ?? 1 , getEmptyCoordItem ( ) ) } >
192+ < Markup encoding = "latex" > { buttonText } </ Markup >
193+ </ Button > }
174194 </ div > ;
175195} ;
176196export default IsaacCoordinateQuestion ;
0 commit comments