@fremtind/jkl-feedback-react
Version:
Jøkul react feedback component
64 lines (63 loc) • 1.99 kB
JavaScript
import { TextArea } from "@fremtind/jkl-text-input-react";
import React, { useEffect, useMemo, useRef } from "react";
import { useFeedbackContext } from "../feedbackContext";
import { useFollowUpContext } from "../followup/followupContext";
import { useMainQuestionContext } from "../main-question/mainQuestionContext";
const TextQuestion = ({
label,
name,
helpLabel = "Ikke skriv personlige opplysninger. Tilbakemeldinger som kommer inn her blir ikke besvart, men brukt i videre arbeid med \xE5 forbedre tjenestene v\xE5re.",
autoFocus = false
}) => {
const { counter } = useFeedbackContext();
const followupContext = useFollowUpContext();
const feedbackContext = useMainQuestionContext();
const context = followupContext || feedbackContext;
const ref = useRef(null);
useEffect(() => {
if (autoFocus && ref.current) {
ref.current.focus();
}
}, [autoFocus, ref]);
const handleChange = (e) => {
const value = {
label,
name: name || label,
type: "text",
value: e.target.value
};
context == null ? void 0 : context.setCurrentValue(value);
};
const currentValue = useMemo(
() => {
var _a;
return Array.isArray(context == null ? void 0 : context.currentValue) ? context == null ? void 0 : context.currentValue[0].value.toString() : (_a = context == null ? void 0 : context.currentValue) == null ? void 0 : _a.value.toString();
},
[context == null ? void 0 : context.currentValue]
);
if (!context) {
console.error(
"Questions must be used inside a Followup or Feedback context provider"
);
return null;
}
return /* @__PURE__ */ React.createElement(
TextArea,
{
ref,
label,
labelProps: { variant: "large" },
name: name || label,
startOpen: true,
rows: 5,
value: currentValue,
onChange: handleChange,
helpLabel,
counter
}
);
};
export {
TextQuestion
};
//# sourceMappingURL=TextQuestion.js.map