Skip to content

Commit

Permalink
chore(DateInput): extract abstract class tor Custom Control (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
vtsvetkov-splunk authored Nov 28, 2024
1 parent 5125210 commit 5be26e3
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 60 deletions.
7 changes: 2 additions & 5 deletions ui/src/ucc-ui-extensions/DateInput/DateInput.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import React from "react";
import DateSuiInput, { DatePropsBaseUncontrolled } from "@splunk/react-ui/Date";
import DateSuiInput, { DateChangeHandler } from "@splunk/react-ui/Date";

function DateInput(props: {
value?: string;
onChange: DatePropsBaseUncontrolled["onChange"];
}) {
function DateInput(props: { value?: string; onChange: DateChangeHandler }) {
const today = props.value ?? new Date().toISOString().split("T")[0];

return <DateSuiInput defaultValue={today} onChange={props.onChange} />;
Expand Down
50 changes: 0 additions & 50 deletions ui/src/ucc-ui-extensions/DateInput/DateInputClass.tsx

This file was deleted.

1 change: 0 additions & 1 deletion ui/src/ucc-ui-extensions/DateInput/index.ts

This file was deleted.

25 changes: 25 additions & 0 deletions ui/src/ucc-ui-extensions/DateInput/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from "react";
import ReactDOM from "react-dom";
import { CustomControlBase } from "../../utils/CustomControl.ts";
import DateInput from "./DateInput";

import { DateChangeHandler } from "@splunk/react-ui/Date";

export default class DateInputClass extends CustomControlBase {
onDateChange: DateChangeHandler = (_event, data) => {
this.setValue(data.value);
};

render() {
const dateValue = this.data.value;
const date =
typeof dateValue === "string" && dateValue.length !== 0
? dateValue
: undefined;

ReactDOM.render(
<DateInput value={date} onChange={this.onDateChange} />,
this.el,
);
}
}
3 changes: 1 addition & 2 deletions ui/src/ucc-ui-extensions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
1. Create a new directory in `ui/src/ucc-ui-extensions` with the name of your extension.
2. Add your extension's files to the directory.
3. Add your default export to the `ui/src/ucc-ui-extensions/index.ts` file. This the only file that will be included in the final bundle with all their dependencies.
4. Add the following in your service in globalConfig.json
4. Add the following in your service in globalConfig.json:

```json
{
// ...
"type": "custom",
"options": {
"src": "FolderName",
Expand Down
18 changes: 16 additions & 2 deletions ui/src/utils/types.ts → ui/src/utils/CustomControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ export type AcceptableFormValueOrNullish =
| AcceptableFormValueOrNull
| undefined;

export type AcceptableFormRecord = Record<string, AcceptableFormValueOrNull>;

export type StandardPages = "configuration" | "inputs";

export interface BaseFormStateData {
Expand Down Expand Up @@ -54,3 +52,19 @@ export interface ControlData {
mode: Mode;
serviceName: string;
}

type ValueSetter = (newValue: AcceptableFormValueOrNullish) => void;

export abstract class CustomControlBase {
protected constructor(
public globalConfig: object,
public el: HTMLElement,
public data: ControlData,
public setValue: ValueSetter,
public util: UtilBaseForm,
) {}

render?(): void;

validation?(field: string, value: ControlData["value"]): string | undefined;
}

0 comments on commit 5be26e3

Please sign in to comment.