Skip to content

Commit

Permalink
feat: use text area as input field
Browse files Browse the repository at this point in the history
  • Loading branch information
juancarlosfarah committed Feb 14, 2024
1 parent b6ac4f3 commit 0ed2374
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 15 deletions.
11 changes: 11 additions & 0 deletions src/config/appSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,14 @@ export type QuestionSettingsType = {
export type AnswerSettings = {
content: string;
};

export type RowsSettings = {
numRows: number;
maxRows: number;
minRows: number;
};

export type CharsSettings = {
minChars: number;
maxChars: number;
};
3 changes: 2 additions & 1 deletion src/langs/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
},
"PLAYER": {
"SAVED_MESSAGE": "Saved",
"SAVE_BUTTON": "Save"
"SAVE_BUTTON": "Save",
"CHARACTERS": "characters"
}
}
}
22 changes: 21 additions & 1 deletion src/modules/context/SettingsContext.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { FC, ReactElement, createContext, useContext } from 'react';

import { AnswerSettings, QuestionSettingsType } from '@/config/appSettings';
import {
AnswerSettings,
CharsSettings,
QuestionSettingsType,
RowsSettings,
} from '@/config/appSettings';
import { hooks, mutations } from '@/config/queryClient';

import Loader from '../common/Loader';
Expand All @@ -10,6 +15,8 @@ import Loader from '../common/Loader';
type AllSettingsType = {
question: QuestionSettingsType;
answer: AnswerSettings;
rows: RowsSettings;
chars: CharsSettings;
};

// default values for the data property of settings by name
Expand All @@ -20,13 +27,26 @@ const defaultSettingsValues: AllSettingsType = {
answer: {
content: '',
},
// zero values means the setting is unset
rows: {
minRows: 0,
maxRows: 0,
numRows: 0,
},
// zero values means the setting is unset
chars: {
minChars: 0,
maxChars: 0,
},
};

// list of the settings names
const ALL_SETTING_NAMES = [
// name of your settings
'question',
'answer',
'rows',
'chars',
] as const;

// automatically generated types
Expand Down
64 changes: 51 additions & 13 deletions src/modules/main/PlayerView.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { ChangeEvent, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';

import { Box, Grid, TextField, Typography } from '@mui/material';
import {
Box,
FormControl,
FormHelperText,
Grid,
OutlinedInput,
Typography,
} from '@mui/material';

import { useLocalContext } from '@graasp/apps-query-client';
import { AppData, PermissionLevel } from '@graasp/sdk';
Expand All @@ -26,6 +33,8 @@ const PlayerView = (): JSX.Element => {
const {
question,
// answer: answerSavedState,
rows: { maxRows, minRows, numRows },
chars: { maxChars, minChars },
} = useSettings();
const { data: appData } = hooks.useAppData();
const { mutate: postAppData } = mutations.usePostAppData();
Expand All @@ -51,8 +60,17 @@ const PlayerView = (): JSX.Element => {
return true;
}
// disable if answer is equal
return isEqual(savedAnswer, answer);
}, [answer, savedAnswer, permission]);
if (isEqual(savedAnswer, answer)) {
return true;
}

// disable if minimum length is not achieved
if (answer.length < minChars) {
return true;
}

return false;
}, [answer, savedAnswer, permission, minChars]);

const disabledMessage = useMemo(() => {
if (permission === PermissionLevel.Read) {
Expand All @@ -74,6 +92,10 @@ const PlayerView = (): JSX.Element => {
});
};

// todo: figure out how to handle min/max/num rows

// todo: figure out how to handle min/max chars

return (
<div data-cy={PLAYER_VIEW_CY}>
<Box p={2}>
Expand All @@ -86,17 +108,33 @@ const PlayerView = (): JSX.Element => {
alignItems="center"
justifyContent="space-around"
>
<Grid item xs={9} sm={10}>
<TextField
inputProps={{
'data-cy': ANSWER_CY,
}}
fullWidth
value={answer}
onChange={handleChangeAnswer}
/>
<Grid item xs={12}>
<FormControl fullWidth>
<OutlinedInput
id="input-component"
inputProps={{
'data-cy': ANSWER_CY,
maxlength: maxChars || undefined,
minlength: minChars || undefined,
}}
multiline
minRows={minRows}
maxRows={maxRows}
rows={numRows}
value={answer}
onChange={handleChangeAnswer}
/>
<FormHelperText
id="input-component-helper-text"
error={answer.length === maxChars}
>
{maxChars
? `${answer.length} / ${maxChars} ${t('CHARACTERS')}`
: ''}
</FormHelperText>
</FormControl>
</Grid>
<Grid item xs={3} sm={2}>
<Grid item xs={12}>
<SubmitButton disabled={disableSave} handler={handleSubmitAnswer}>
{disableSave ? disabledMessage : t('SAVE_BUTTON')}
</SubmitButton>
Expand Down

0 comments on commit 0ed2374

Please sign in to comment.