Skip to content

Commit 4dcbbc7

Browse files
author
Matteo Miotello
committed
miglioramento textEditor
1 parent 2e238e1 commit 4dcbbc7

File tree

4 files changed

+56
-24
lines changed

4 files changed

+56
-24
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@shellrent/shelly-ui",
33
"private": false,
4-
"version": "1.2.0",
4+
"version": "1.2.1",
55
"type": "module",
66
"module": "./dist/shelly-ui.es.js",
77
"types": "./dist/index.d.ts",

src/Form/Form.stories.tsx

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Meta, StoryObj } from '@storybook/react';
22
import Form from './Form';
33
import useForm from './useForm';
4-
import {Button, Input, InputValidationHandler} from '..';
4+
import { Button, Input, InputValidationHandler, TextEditor } from '..';
55
import React from 'react';
66
import { validators } from '..';
77

@@ -18,48 +18,56 @@ export const Default: Story = {
1818
// eslint-disable-next-line react-hooks/rules-of-hooks
1919
const form = useForm();
2020

21-
const validateRepeatPassword: InputValidationHandler = ( value, formData ) => {
22-
const repeat =form.state.formValues?.getFormValue( 'password' );
23-
24-
if ( !repeat ) {
21+
const validateRepeatPassword: InputValidationHandler = (value, formData) => {
22+
const repeat = form.state.formValues?.getFormValue('password');
23+
24+
if (!repeat) {
2525
return null;
2626
}
2727

28-
if ( repeat && !value ) {
28+
if (repeat && !value) {
2929
return 'E\' necessario ripetere la password.';
3030
}
31-
32-
if ( value !== repeat ) {
31+
32+
if (value !== repeat) {
3333
return 'Le due password non corrispondono';
3434
}
35-
35+
3636
return null;
3737
};
3838

3939
const validatePassword: InputValidationHandler = (value: any) => {
4040
return null;
4141

4242
const err = validators.minCharacters(8, 'La Password deve contenere almeno 8 caratteri')(value);
43-
43+
4444
if (err) {
4545
return err;
4646
}
47-
47+
4848
return validators.isRequired('La password è richiesta')(value);
4949
};
5050

51-
return <Form form={form} saveForm={() => true}>
51+
return <Form form={form} saveForm={(data) => {
52+
console.log(data);
53+
return true;
54+
}}>
5255
<Input.FormControl>
5356
<Input.Label>Password</Input.Label>
54-
<Input {...form.registerInput( {name: 'password', validators: [validatePassword]} )} />
57+
<Input {...form.registerInput({ name: 'password', validators: [validatePassword] })} />
5558
</Input.FormControl>
5659
<Input.FormControl>
5760
<Input.Label>Test</Input.Label>
58-
<Input {...form.registerInput( {name: 'test', validators: [validateRepeatPassword]} )} />
61+
<Input {...form.registerInput({ name: 'test', validators: [validateRepeatPassword] })} />
62+
</Input.FormControl>
63+
64+
<Input.FormControl>
65+
<Input.Label>Text editor</Input.Label>
66+
<TextEditor {...form.registerInput({ name: 'editor' })} />
5967
</Input.FormControl>
6068
<Button type="submit">
6169
Save
6270
</Button>
63-
</Form>;
71+
</Form >;
6472
}
6573
};

src/TextEditor/TextEditor.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
import React, { useEffect, useState } from 'react';
1+
import React, { useCallback, useEffect, useState } from 'react';
22
import { InputProps } from '../Form';
33
import ReactQuill from 'react-quill';
4-
import 'react-quill/dist/quill.snow.css';
4+
import './style.css';
55
import FieldError from '../Common/FieldError';
66

7-
type TextEditorProps = InputProps
7+
type TextEditorProps = {
8+
placeholder?: string;
9+
} & InputProps
810

9-
const TextEditor: React.FC<TextEditorProps> = ( { value, onValueChange, error, validators, inputSize, ...props } ) => {
11+
const TextEditor: React.FC<TextEditorProps> = ( { value, onValueChange, placeholder, error, validators, inputSize, ...props } ) => {
1012
const [err, setErr] = useState( error );
1113
const [htmlValue, setHtmlValue] = useState<string | undefined>( '' );
1214

13-
const onEditorChange = ( val: string | undefined ) => {
14-
15+
const onEditorChange = useCallback( ( val: string | undefined ) => {
1516
setHtmlValue( val );
16-
};
17+
}, [] );
1718

1819
useEffect( () => {
1920
if ( value === '' ) {
@@ -56,7 +57,7 @@ const TextEditor: React.FC<TextEditorProps> = ( { value, onValueChange, error, v
5657

5758
return <div>
5859
<input type='hidden' value={value} {...props}/>
59-
<ReactQuill className='!border-0 !rounded-box' theme="snow" value={htmlValue} onChange={onEditorChange} />
60+
<ReactQuill className={`rounded-btn ${err ? '!border border-error' : '!border-0'}`} theme="snow" placeholder={placeholder} value={htmlValue} onChange={onEditorChange} />
6061
<FieldError error={err}></FieldError>
6162
</div>;
6263
};

src/TextEditor/style.css

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@import 'react-quill/dist/quill.snow.css';
2+
3+
.ql-toolbar {
4+
border-radius: var(--rounded-btn, 0.5rem) var(--rounded-btn, 0.5rem) 0px 0px !important;
5+
border-color: var(--fallback-bc,oklch(var(--bc)/0.2)) !important;
6+
}
7+
8+
.ql-container {
9+
border-radius: 0px 0px var(--rounded-btn, 0.5rem) var(--rounded-btn, 0.5rem) !important;
10+
border-color: var(--fallback-bc,oklch(var(--bc)/0.2)) !important;
11+
}
12+
13+
.ql-editor {
14+
min-height: 10rem;
15+
}
16+
17+
.border-error > .ql-toolbar {
18+
border-color: transparent transparent var(--fallback-bc,oklch(var(--bc)/0.2)) transparent !important;
19+
}
20+
21+
.border-error > .ql-container {
22+
border-color: transparent !important;
23+
}

0 commit comments

Comments
 (0)