Skip to content

Commit e296f83

Browse files
authored
refactor: fixed up(#235) and adding docs to the sourceState prop (#237)
* fix: fixed up ts error on `/website` * feat(deps): 增加@types react|react-dom to ^18.2.0 version * feat: Add an additional 'sourceState' props attribute to the component. * fix: fixed up eslint error * fix: Option 'emitDeclarationOnly' cannot be specified with option 'noEmit'. * refactor: fixed up(#235) and adding docs to the `sourceState` prop
1 parent ce93a5e commit e296f83

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ interface CodePreviewProps extends SplitProps {
131131
* @default light
132132
*/
133133
theme?: ReactCodeMirrorProps['theme'];
134+
/**
135+
* Specifies the initial state of the source panel.
136+
*/
137+
sourceState?: 'hidden' | 'shown';
134138
}
135139
```
136140

src/index.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ const CodePreview = React.forwardRef<CodePreviewRef, CodePreviewProps>((props, r
109109
noPreview = false,
110110
noScroll = false,
111111
bgWhite = false,
112+
sourceState,
112113
...otherProps
113114
} = props;
114115
const {
@@ -183,6 +184,13 @@ const CodePreview = React.forwardRef<CodePreviewRef, CodePreviewProps>((props, r
183184
setWidth(width === 1 ? '50%' : 1);
184185
setShowEdit(true);
185186
};
187+
// 通过状态props属性判断是否切换源码
188+
const isShown = sourceState === 'shown';
189+
useEffect(() => {
190+
setWidth(isShown ? '50%' : 1);
191+
setShowEdit(isShown);
192+
// eslint-disable-next-line react-hooks/exhaustive-deps
193+
}, [isShown]);
186194
const onCopyCode = () => {
187195
copyTextToClipboard(code || '', (isCopy) => setCopied(isCopy));
188196
setTimeout(() => setCopied(false), 2000);
@@ -196,7 +204,7 @@ const CodePreview = React.forwardRef<CodePreviewRef, CodePreviewProps>((props, r
196204
};
197205
return (
198206
<Split data-color-mode={theme} visiable={visiable} className={cls} style={{ flex: 1, ...style }} {...otherProps}>
199-
{!noPreview && !onlyEdit && (
207+
{!onlyEdit && (
200208
<div
201209
className={[
202210
`${prefixCls}-demo`,
@@ -208,6 +216,7 @@ const CodePreview = React.forwardRef<CodePreviewRef, CodePreviewProps>((props, r
208216
.trim()}
209217
style={{
210218
flex: 1,
219+
display: !noPreview ? 'unset' : 'none',
211220
...(width === 1 ? { width: '100%' } : {}),
212221
}}
213222
>

src/useCodePreview.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ export const getReactDOMClient = () => {
1616
};
1717

1818
export function useCodePreview(props: CodePreviewProps) {
19-
const isShowEdit = props.sourceState === 'shown';
2019
const [demoDom, setDemoDom] = useState<HTMLDivElement>();
2120
const playerId = useRef(`${parseInt(String(Math.random() * 1e9), 10).toString(36)}`);
2221
const [fullScreen, setFullScreen] = useState(false);
2322
const [errorMessage, setErrorMessage] = useState('');
24-
const [showEdit, setShowEdit] = useState(isShowEdit);
25-
const [width, setWidth] = useState<number | string>(isShowEdit ? '50%' : 1);
23+
const [showEdit, setShowEdit] = useState(false);
24+
const [width, setWidth] = useState<number | string>(1);
2625
const [copied, setCopied] = useState(false);
2726
const [code, setCode] = useState(props.code || '');
2827

website/Example.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const Example = () => {
2727
noScroll: false,
2828
noPreview: false,
2929
bordered: true,
30+
sourceState: false,
3031
codeSandbox: {
3132
files: {
3233
'sandbox.config.json': {
@@ -105,6 +106,7 @@ const Example = () => {
105106
noScroll={state.noScroll}
106107
bgWhite={state.bgWhite}
107108
noCode={state.noCode}
109+
sourceState={state.sourceState ? 'shown' : 'hidden'}
108110
editProps={{
109111
onChange: (value) => {
110112
setState({
@@ -166,6 +168,10 @@ const Example = () => {
166168
是否显示滚动条 `noScroll={state.noScroll.toString()}`
167169
</Switch>
168170
<br />
171+
<Switch checked={state.sourceState} onChange={handleChange.bind(this, 'sourceState')}>
172+
是否显示源码 `sourceState={state.sourceState ? 'shown' : 'hidden'}`
173+
</Switch>
174+
<br />
169175
<Switch checked={state.bordered} onChange={handleChange.bind(this, 'bordered')}>
170176
是否显示边框 `bordered={state.bordered.toString()}`
171177
</Switch>

0 commit comments

Comments
 (0)