ahooks#useCounter TypeScript Examples

The following examples show how to use ahooks#useCounter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: config-setting.tsx    From bext with MIT License 4 votes vote down vote up
ConfigSetting: FC = () => {
  const [formKey, { inc: resetForm }] = useCounter(0);
  const { draft, setDraft } = useDraft();
  const [modalVisible, { setTrue: showModal, setFalse: hideModal }] =
    useBoolean(false);

  useEffect(() => {
    if (!modalVisible) {
      resetForm();
    }
  }, [modalVisible]);

  return (
    <div className="p-4">
      <div className="flex items-center justify-between">
        <Toggle
          label="启用安装配置"
          inlineLabel
          checked={!!draft?.configSchema}
          className="mb-0"
          onChange={() =>
            setDraft({
              configSchema: draft?.configSchema ? undefined : DEFAULT_SCHEMA,
              defaultConfig: undefined,
            })
          }
        />
        {draft?.configSchema ? <Link onClick={showModal}>配置表单</Link> : null}
      </div>
      {draft?.configSchema ? (
        <>
          <Panel
            isOpen={modalVisible}
            onDismiss={hideModal}
            type={PanelType.smallFluid}
            headerText="配置表单"
            styles={{
              content: {
                padding: 0,
                flex: 1,
              },
              scrollableContent: {
                minHeight: '100%',
                display: 'flex',
                flexDirection: 'column',
              },
            }}
            isLightDismiss
          >
            {modalVisible ? <SchemaEditor /> : null}
          </Panel>
          <Editor
            value={`// 使用示例
import config from '@bext/config';
console.log(config);
/* config = ${JSON.stringify(draft.defaultConfig, null, 2)}
具体使用方式可查看
https://bext.ketra.fun/meta/648648
*/
`}
            height="200px"
            language="javascript"
            options={{
              readOnly: true,
              lineNumbers: 'off',
              minimap: { enabled: false },
              folding: false,
            }}
          />

          <Separator>配置默认值</Separator>
          {draft?.configSchema ? (
            <JsonSchemaForm
              key={String(formKey)}
              schema={draft.configSchema}
              omitExtraData
              liveOmit
              formData={draft.defaultConfig}
              onChange={({ formData }) => setDraft({ defaultConfig: formData })}
              liveValidate
            >
              <></>
            </JsonSchemaForm>
          ) : null}
        </>
      ) : null}
    </div>
  );
}