Skip to content

Commit

Permalink
EditDialog: show CharacterCount only for >= 150 chars
Browse files Browse the repository at this point in the history
  • Loading branch information
zbycz committed Oct 11, 2024
1 parent 3d6a268 commit bb08449
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import React, { useEffect, useState } from 'react';

import { Box, Button, Stack, TextField, Typography } from '@mui/material';

import { Button, TextField, Typography } from '@mui/material';
import { t } from '../../../../services/intl';
import {
getNextWikimediaCommonsIndex,
Expand All @@ -11,6 +9,7 @@ import { useEditDialogContext } from '../../helpers/EditDialogContext';
import { useEditContext } from '../EditContext';
import { OpeningHoursEditor } from './OpeningHoursEditor/OpeningHoursEditor';
import styled from '@emotion/styled';
import { CharacterCount } from './helpers';

export const majorKeys = [
'name',
Expand Down Expand Up @@ -47,30 +46,6 @@ const getData = (numberOfWikimediaItems) => {
const InputContainer = styled.div`
position: relative;
`;
const CharacterCountContainer = styled.div`
position: absolute;
right: 0;
`;

const CharacterCount = ({
count = 0,
max,
isVisible,
}: {
count?: number;
max: number;
isVisible: boolean;
}) => (
<CharacterCountContainer>
{isVisible && (
<Stack direction="row" justifyContent={'flex-end'} whiteSpace="nowrap">
<Box color={count >= max ? 'error.main' : undefined}>
{count} / {max}
</Box>
</Stack>
)}
</CharacterCountContainer>
);

const TextFieldWithCharacterCount = ({
k,
Expand All @@ -79,7 +54,7 @@ const TextFieldWithCharacterCount = ({
onChange,
value,
}) => {
const [isCharacterCountVisible, setIsCharacterCountVisible] = useState(false);
const [isFocused, setIsFocused] = useState(false);
return (
<InputContainer>
<TextField
Expand All @@ -94,13 +69,13 @@ const TextFieldWithCharacterCount = ({
fullWidth
autoFocus={autoFocus}
inputProps={{ maxLength: MAX_INPUT_LENGTH }}
onFocus={() => setIsCharacterCountVisible(true)}
onBlur={() => setIsCharacterCountVisible(false)}
onFocus={() => setIsFocused(true)}
onBlur={() => setIsFocused(false)}
helperText={
<CharacterCount
count={value?.length}
max={MAX_INPUT_LENGTH}
isVisible={isCharacterCountVisible}
isInputFocused={isFocused}
/>
}
/>
Expand Down
29 changes: 29 additions & 0 deletions src/components/FeaturePanel/EditDialog/EditContent/helpers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import styled from '@emotion/styled';
import { Box, Stack } from '@mui/material';
import React from 'react';

const CharacterCountContainer = styled.div`
position: absolute;
right: 0;
`;

type CharacterCountProps = {
count?: number;
max: number;
isInputFocused: boolean;
};

export const CharacterCount = ({
count = 0,
max,
isInputFocused,
}: CharacterCountProps) =>
isInputFocused && count > 150 ? (
<CharacterCountContainer>
<Stack direction="row" justifyContent={'flex-end'} whiteSpace="nowrap">
<Box color={count >= max ? 'error.main' : undefined}>
{count} / {max}
</Box>
</Stack>
</CharacterCountContainer>
) : null;

0 comments on commit bb08449

Please sign in to comment.