Skip to content

Commit

Permalink
Dynamic textarea (#50)
Browse files Browse the repository at this point in the history
* Add dynamically sized textarea

* Fix layout of uenigheter
  • Loading branch information
stephenramthun committed Jul 31, 2019
1 parent d935779 commit 017cdf0
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 54 deletions.
3 changes: 2 additions & 1 deletion src/components/pages/Oppsummering/Innrapportering.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ const Innrapportering = withBehandlingContext(({ behandling }) => {
<>
{innrapportering.uenigheter.map((uenighet, i) => (
<Normaltekst key={`uenighet-${i}`}>
{uenighet.label} - <span>{uenighet.value}</span>
<span>{uenighet.label}:</span>
<span>{uenighet.value}</span>
</Normaltekst>
))}
{innrapportering.hasSendt ? (
Expand Down
31 changes: 29 additions & 2 deletions src/components/pages/Oppsummering/Innrapportering.less
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
@sendtColor: #38a161;

.Innrapportering.panel {
display: flex;
flex-direction: column;

> .typo-normal {
display: flex;
align-items: flex-start;

> * {
margin-left: 0.5rem;
}
}

> .typo-normal:not(:last-child) {
margin-bottom: 0.5rem;
}

.typo-normal span:first-child {
@width: 15rem;
min-width: @width;
max-width: @width;
}

.typo-normal span:last-child {
font-style: italic;
min-width: 15rem;
}

.knapp {
width: max-content;
display: flex;
margin-top: 1rem;

&.sendt {
@sendtColor: #38a161;
border-color: @sendtColor;
color: @sendtColor;
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/pages/Oppsummering/Oppsummering.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ListeItem from '../../widgets/ListeItem';
import ListeSeparator from '../../widgets/ListeSeparator';
import Navigasjonsknapper from '../../widgets/Navigasjonsknapper';
import Innrapportering from './Innrapportering';
import './Oppsummering.css';
import './Oppsummering.less';

const Oppsummering = withBehandlingContext(({ behandling }) => (
<div className="Oppsummering">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,4 @@

.Oppsummering + .Navigasjonsknapper {
margin-left: 1rem;
}

.Oppsummering__right-col .panel {
display: flex;
flex-direction: column;
}

.Oppsummering__right-col .panel > .typo-normal:not(:last-child) {
margin-bottom: 0.5rem;
}

.Oppsummering__right-col .panel .typo-normal span {
font-style: italic;
}

.Oppsummering__right-col .panel .knapp {
margin-top: 1rem;
}
}
3 changes: 2 additions & 1 deletion src/components/widgets/Bolk/ListeItemBolk.css
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
.ListeItemBolk {
display: flex;
flex-wrap: nowrap;
align-items: center;
align-items: flex-start;
}

.ListeItemBolk .ListeItem {
flex: 1;
display: flex;
align-items: center;
margin-top: 0.85rem;
max-width: 18rem;
min-width: 18rem;
}
Expand Down
58 changes: 58 additions & 0 deletions src/components/widgets/DynamicTextarea.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';

const lineHeight = 22;

const DynamicTextarea = ({
name,
value,
placeholder,
maxCharacters,
disabled,
forwardedRef,
...rest
}) => {
const [rows, setRows] = useState(1);

useEffect(() => {
if (forwardedRef && forwardedRef.current) {
setRows(Math.floor(forwardedRef.current.scrollHeight / lineHeight));
}
}, [value, forwardedRef]);

return (
<textarea
name={name}
value={value}
placeholder={placeholder}
maxLength={maxCharacters}
disabled={disabled}
rows={rows}
ref={forwardedRef}
style={{
marginLeft: '0.5rem',
resize: disabled ? 'none' : ''
}}
{...rest}
/>
);
};

DynamicTextarea.propTypes = {
name: PropTypes.string.isRequired,
value: PropTypes.string.isRequired,
placeholder: PropTypes.string,
maxCharacters: PropTypes.number,
disabled: PropTypes.bool,
forwardedRef: PropTypes.any
};

DynamicTextarea.defaultProps = {
placeholder: '',
maxCharacters: 2000,
disabled: false
};

export default React.forwardRef((props, ref) => (
<DynamicTextarea {...props} forwardedRef={ref} />
));
19 changes: 7 additions & 12 deletions src/components/widgets/Uenigboks.css
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
.Uenigboks {
display: flex;
flex: 1;
align-content: stretch;
align-items: center;
align-items: flex-start;
padding: 0.3rem;
width: 66%;
}

.Uenigboks__input {
.Uenigboks .Uenigboks__input {
flex: 1;
}

.Uenigboks__input input {
font-size: 1rem;
background-color: #fff;
color: #000;
}

.Uenigboks .skjemaelement {
margin: 0;
}
Expand All @@ -24,10 +19,10 @@
margin: 0;
}

.Uenigboks .skjemaelement:last-child {
margin-left: 1rem;
.Uenigboks .skjemaelement:first-child {
margin-top: 0.5rem;
}

.Uenigboks__input label {
display: none;
.Uenigboks .skjemaelement:last-child {
margin-left: 1rem;
}
19 changes: 7 additions & 12 deletions src/components/widgets/Uenigboks.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import React, { useContext, useEffect, useMemo, useState } from 'react';
import PropTypes from 'prop-types';
import { Checkbox, Input } from 'nav-frontend-skjema';
import DynamicTextarea from './DynamicTextarea';
import { Checkbox } from 'nav-frontend-skjema';
import { InnrapporteringContext } from '../../context/InnrapporteringContext';
import './Uenigboks.css';
import { useFocusRef } from '../../hooks/useFocusRef';

// Override proptypes til Input for å kunne gi en ref som parameter til inputRef
Input.propTypes = {
...Input.propTypes,
inputRef: PropTypes.any
};
import './Uenigboks.css';

const Uenigboks = ({ label }) => {
const id = label + decodeURIComponent(window.location.pathname);
Expand Down Expand Up @@ -55,14 +50,14 @@ const Uenigboks = ({ label }) => {
checked={checked}
onChange={onCheckboxChange}
/>
<Input
className="Uenigboks__input"
label="Årsak"
<DynamicTextarea
name="Årsak"
className="Uenigboks__input skjemaelement skjemaelement__input"
placeholder="Skriv inn årsak til uenighet"
inputRef={ref}
disabled={!checked}
onChange={onInputChange}
value={inputValue}
ref={ref}
/>
</div>
);
Expand Down
8 changes: 1 addition & 7 deletions src/hooks/useKeyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ export const Keys = {
};

const shouldDisableKeyboard = () => {
return (
document.activeElement.tagName.toLowerCase() === 'input' &&
document.activeElement.attributes.getNamedItem('type') &&
document.activeElement.attributes
.getNamedItem('type')
.value.toLowerCase() === 'text'
);
return document.activeElement.tagName.toLowerCase() === 'textarea';
};

export const useKeyboard = actionMappings => {
Expand Down

0 comments on commit 017cdf0

Please sign in to comment.