File size: 2,540 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import React, { useState } from 'react';

// The editor core
import type { Value } from '@react-page/editor';
import { createValue } from '@react-page/editor';
import { Button } from '@mui/material';
import Editor from '@react-page/editor';

import slate from '@react-page/plugins-slate';

import { pluginFactories } from '@react-page/plugins-slate';
import PageLayout from '../../components/PageLayout';

const formFieldPlugin = pluginFactories.createComponentPlugin<{
  fieldName: string;
  placeholder: string;
}>({
  Component: (props) => {
    return (
      <input
        placeholder={props.placeholder}
        type="text"
        onChange={(e) =>
          console.log(
            'filled out field ' + props.fieldName + ', with ' + e.target.value
          )
        }
      />
    );
  },
  controls: {
    type: 'autoform',
    schema: {
      properties: {
        fieldName: {
          type: 'string',
        },
        placeholder: {
          type: 'string',
        },
      },
    },
  },
  addHoverButton: true,
  addToolbarButton: true,
  type: 'FormField',
  object: 'inline',
  isVoid: true, // <--- makes it a void plugin

  icon: <span>FormField</span>,
  label: 'FormField',
});
// customize slate to add the custom slate plugin
const customSlate = slate((config) => ({
  ...config,
  plugins: {
    ...config.plugins,
    form: {
      formField: formFieldPlugin,
    },
  },
}));
const cellPlugins = [customSlate];
// prettier-ignore
const SAMPLE_VALUE: Value = createValue( { rows: [ [ { plugin: customSlate, data: { slate: [ { type: 'PARAGRAPH/PARAGRAPH', children: [ { text: 'Hello, my Name is ', }, { type: 'FormField', data: { fieldName: 'firstname', placeholder: 'fill Firstname', }, children: [ { text: '', }, ], }, { text: ' ', }, { type: 'FormField', data: { fieldName: 'lastname', placeholder: 'fill Lastname', }, children: [ { text: '', }, ], }, { text: ' and i work as a ', }, { type: 'FormField', data: { fieldName: 'jobDescription', placeholder: 'Job Description', }, children: [ { text: '', }, ], },{ text: '.', } ], }, ], }, }, ], ], }, { cellPlugins, lang: 'default' } );

export default function SimpleExample() {
  const [readOnly, setReadOnly] = useState(false);
  const [value, setValue] = useState<Value>(SAMPLE_VALUE);

  return (
    <PageLayout>
      <Button onClick={() => setReadOnly(!readOnly)}>Toggle read only</Button>
      <Editor
        readOnly={readOnly}
        cellPlugins={cellPlugins}
        value={value}
        onChange={setValue}
      />
    </PageLayout>
  );
}