@destyler/checkbox 0.2.11 → 0.2.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +6 -9
- package/dist/index.mjs +57 -48
- package/package.json +7 -7
package/dist/index.d.mts
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import * as _destyler_anatomy0 from "@destyler/anatomy";
|
|
2
|
-
import * as _destyler_xstate0 from "@destyler/xstate";
|
|
3
1
|
import { AnyEventObject, Machine, XSend, XState } from "@destyler/xstate";
|
|
4
2
|
import { CommonProperties, DirectionProperty, NormalizeProps, PropTypes, RequiredBy } from "@destyler/types";
|
|
5
|
-
|
|
6
3
|
//#region src/anatomy.d.ts
|
|
7
|
-
declare const anatomy:
|
|
4
|
+
export declare const anatomy: import("@destyler/anatomy").AnatomyInstance<"root" | "control" | "label" | "indicator">;
|
|
8
5
|
//#endregion
|
|
9
6
|
//#region src/types.d.ts
|
|
10
7
|
type CheckedState = boolean | 'indeterminate';
|
|
@@ -152,13 +149,13 @@ interface MachineApi<T extends PropTypes = PropTypes> {
|
|
|
152
149
|
}
|
|
153
150
|
//#endregion
|
|
154
151
|
//#region src/connect.d.ts
|
|
155
|
-
declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
|
|
152
|
+
export declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
|
|
156
153
|
//#endregion
|
|
157
154
|
//#region src/machine.d.ts
|
|
158
|
-
declare function machine(userContext: UserDefinedContext):
|
|
155
|
+
export declare function machine(userContext: UserDefinedContext): import("@destyler/xstate").Machine<MachineContext, MachineState, import("@destyler/xstate").AnyEventObject>;
|
|
159
156
|
//#endregion
|
|
160
157
|
//#region src/props.d.ts
|
|
161
|
-
declare const props: ("value" | "ids" | "disabled" | "invalid" | "required" | "checked" | "defaultChecked" | "readOnly" | "onCheckedChange" | "name" | "form" | "dir" | "id" | "getRootNode")[];
|
|
162
|
-
declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "value" | "ids" | "disabled" | "invalid" | "required" | "checked" | "defaultChecked" | "readOnly" | "onCheckedChange" | "name" | "form" | "dir" | "id" | "getRootNode">];
|
|
158
|
+
export declare const props: ("value" | "ids" | "disabled" | "invalid" | "required" | "checked" | "defaultChecked" | "readOnly" | "onCheckedChange" | "name" | "form" | "dir" | "id" | "getRootNode")[];
|
|
159
|
+
export declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "value" | "ids" | "disabled" | "invalid" | "required" | "checked" | "defaultChecked" | "readOnly" | "onCheckedChange" | "name" | "form" | "dir" | "id" | "getRootNode">];
|
|
163
160
|
//#endregion
|
|
164
|
-
export {
|
|
161
|
+
export type { MachineApi as Api, CheckedChangeDetails, CheckedState, UserDefinedContext as Context, ElementIds, MachineState, Service, State };
|
package/dist/index.mjs
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import { createAnatomy } from "@destyler/anatomy";
|
|
2
|
-
import { createScope, dataAttr, dispatchInputCheckedEvent, getEventTarget, setElementChecked, trackFormControl, trackPress, visuallyHiddenStyle } from "@destyler/dom";
|
|
2
|
+
import { createScope, dataAttr, dispatchInputCheckedEvent, getEventTarget, setElementChecked, trackCanceledInputClick, trackFormControl, trackPress, visuallyHiddenStyle } from "@destyler/dom";
|
|
3
3
|
import { isFocusVisible, trackFocusVisible } from "@destyler/focus-visible";
|
|
4
4
|
import { compact, createSplitProps, isControlled, isEqual, isPropUserProvided, resolveControllableProp, withControllableProvided } from "@destyler/utils";
|
|
5
5
|
import { createMachine, guards } from "@destyler/xstate";
|
|
6
6
|
import { createProps } from "@destyler/types";
|
|
7
|
-
|
|
8
7
|
//#region src/anatomy.ts
|
|
9
8
|
const anatomy = createAnatomy("checkbox").parts("root", "label", "control", "indicator");
|
|
10
9
|
const parts = anatomy.build();
|
|
11
|
-
|
|
12
10
|
//#endregion
|
|
13
11
|
//#region src/dom.ts
|
|
14
12
|
const dom = createScope({
|
|
@@ -19,7 +17,6 @@ const dom = createScope({
|
|
|
19
17
|
getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
|
|
20
18
|
getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx))
|
|
21
19
|
});
|
|
22
|
-
|
|
23
20
|
//#endregion
|
|
24
21
|
//#region src/connect.ts
|
|
25
22
|
function connect(state, send, normalize) {
|
|
@@ -45,10 +42,10 @@ function connect(state, send, normalize) {
|
|
|
45
42
|
indeterminate,
|
|
46
43
|
focused,
|
|
47
44
|
checkedState: state.context.checked,
|
|
48
|
-
setChecked(checked
|
|
45
|
+
setChecked(checked) {
|
|
49
46
|
send({
|
|
50
47
|
type: "CHECKED.SET",
|
|
51
|
-
checked
|
|
48
|
+
checked,
|
|
52
49
|
isTrusted: false
|
|
53
50
|
});
|
|
54
51
|
},
|
|
@@ -146,10 +143,9 @@ function connect(state, send, normalize) {
|
|
|
146
143
|
event.preventDefault();
|
|
147
144
|
return;
|
|
148
145
|
}
|
|
149
|
-
const checked$1 = event.currentTarget.checked;
|
|
150
146
|
send({
|
|
151
147
|
type: "CHECKED.SET",
|
|
152
|
-
checked: checked
|
|
148
|
+
checked: event.currentTarget.indeterminate ? "indeterminate" : event.currentTarget.checked,
|
|
153
149
|
isTrusted: true
|
|
154
150
|
});
|
|
155
151
|
}
|
|
@@ -157,7 +153,6 @@ function connect(state, send, normalize) {
|
|
|
157
153
|
}
|
|
158
154
|
};
|
|
159
155
|
}
|
|
160
|
-
|
|
161
156
|
//#endregion
|
|
162
157
|
//#region src/machine.ts
|
|
163
158
|
const { not } = guards;
|
|
@@ -203,6 +198,7 @@ function machine(userContext) {
|
|
|
203
198
|
checked: "syncInputElement"
|
|
204
199
|
},
|
|
205
200
|
activities: [
|
|
201
|
+
"trackCanceledInputClick",
|
|
206
202
|
"trackFormControlState",
|
|
207
203
|
"trackPressEvent",
|
|
208
204
|
"trackFocusVisible"
|
|
@@ -215,77 +211,91 @@ function machine(userContext) {
|
|
|
215
211
|
"CHECKED.SET": [{
|
|
216
212
|
guard: not("isTrusted"),
|
|
217
213
|
actions: ["setChecked", "dispatchChangeEvent"]
|
|
218
|
-
}, { actions: ["setChecked"] }],
|
|
214
|
+
}, { actions: ["setChecked", "syncInputElement"] }],
|
|
219
215
|
"CONTEXT.SET": { actions: ["setContext"] }
|
|
220
216
|
},
|
|
221
217
|
computed: {
|
|
222
|
-
isIndeterminate: (ctx
|
|
223
|
-
isChecked: (ctx
|
|
224
|
-
isDisabled: (ctx
|
|
218
|
+
isIndeterminate: (ctx) => isIndeterminate(ctx.checked),
|
|
219
|
+
isChecked: (ctx) => isChecked(ctx.checked),
|
|
220
|
+
isDisabled: (ctx) => !!ctx.disabled || ctx.fieldsetDisabled
|
|
225
221
|
},
|
|
226
|
-
states: { ready: {} }
|
|
222
|
+
states: { ready: { entry: "syncInputIndeterminate" } }
|
|
227
223
|
}, {
|
|
228
224
|
guards: { isTrusted: (_ctx, evt) => !!evt.isTrusted },
|
|
229
225
|
activities: {
|
|
230
|
-
|
|
231
|
-
|
|
226
|
+
trackCanceledInputClick(ctx) {
|
|
227
|
+
const input = dom.getHiddenInputEl(ctx);
|
|
228
|
+
return trackCanceledInputClick(input, (target) => {
|
|
229
|
+
if (target !== input) return;
|
|
230
|
+
setElementChecked(target, ctx.isChecked);
|
|
231
|
+
target.indeterminate = ctx.isIndeterminate;
|
|
232
|
+
});
|
|
233
|
+
},
|
|
234
|
+
trackPressEvent(ctx) {
|
|
235
|
+
if (ctx.isDisabled) return;
|
|
232
236
|
return trackPress({
|
|
233
|
-
pointerNode: dom.getRootEl(ctx
|
|
234
|
-
keyboardNode: dom.getHiddenInputEl(ctx
|
|
237
|
+
pointerNode: dom.getRootEl(ctx),
|
|
238
|
+
keyboardNode: dom.getHiddenInputEl(ctx),
|
|
235
239
|
isValidKey: (event) => event.key === " ",
|
|
236
|
-
onPress: () => ctx
|
|
237
|
-
onPressStart: () => ctx
|
|
238
|
-
onPressEnd: () => ctx
|
|
240
|
+
onPress: () => ctx.active = false,
|
|
241
|
+
onPressStart: () => ctx.active = true,
|
|
242
|
+
onPressEnd: () => ctx.active = false
|
|
239
243
|
});
|
|
240
244
|
},
|
|
241
|
-
trackFocusVisible(ctx
|
|
242
|
-
if (ctx
|
|
243
|
-
return trackFocusVisible({ root: dom.getRootNode(ctx
|
|
245
|
+
trackFocusVisible(ctx) {
|
|
246
|
+
if (ctx.isDisabled) return;
|
|
247
|
+
return trackFocusVisible({ root: dom.getRootNode(ctx) });
|
|
244
248
|
},
|
|
245
|
-
trackFormControlState(ctx
|
|
246
|
-
return trackFormControl(dom.getHiddenInputEl(ctx
|
|
249
|
+
trackFormControlState(ctx, _evt, { send, initialContext }) {
|
|
250
|
+
return trackFormControl(dom.getHiddenInputEl(ctx), {
|
|
247
251
|
onFieldsetDisabledChange(disabled) {
|
|
248
|
-
ctx
|
|
252
|
+
ctx.fieldsetDisabled = disabled;
|
|
249
253
|
},
|
|
250
254
|
onFormReset() {
|
|
251
255
|
send({
|
|
252
256
|
type: "CHECKED.SET",
|
|
253
|
-
checked:
|
|
257
|
+
checked: initialContext.checked
|
|
254
258
|
});
|
|
255
259
|
}
|
|
256
260
|
});
|
|
257
261
|
}
|
|
258
262
|
},
|
|
259
263
|
actions: {
|
|
260
|
-
setContext(ctx
|
|
261
|
-
Object.assign(ctx
|
|
264
|
+
setContext(ctx, evt) {
|
|
265
|
+
Object.assign(ctx, evt.context);
|
|
266
|
+
},
|
|
267
|
+
syncInputIndeterminate(ctx, evt, { getState }) {
|
|
268
|
+
const inputEl = dom.getHiddenInputEl(ctx);
|
|
269
|
+
const state = getState();
|
|
270
|
+
if (inputEl && state.matches("ready") && state.event === evt) inputEl.indeterminate = ctx.isIndeterminate;
|
|
262
271
|
},
|
|
263
|
-
syncInputElement(ctx
|
|
264
|
-
const inputEl = dom.getHiddenInputEl(ctx
|
|
272
|
+
syncInputElement(ctx) {
|
|
273
|
+
const inputEl = dom.getHiddenInputEl(ctx);
|
|
265
274
|
if (!inputEl) return;
|
|
266
|
-
setElementChecked(inputEl, ctx
|
|
267
|
-
inputEl.indeterminate = ctx
|
|
275
|
+
setElementChecked(inputEl, ctx.isChecked);
|
|
276
|
+
inputEl.indeterminate = ctx.isIndeterminate;
|
|
268
277
|
},
|
|
269
|
-
removeFocusIfNeeded(ctx
|
|
270
|
-
if (ctx
|
|
271
|
-
ctx
|
|
272
|
-
ctx
|
|
278
|
+
removeFocusIfNeeded(ctx) {
|
|
279
|
+
if (ctx.disabled && ctx.focused) {
|
|
280
|
+
ctx.focused = false;
|
|
281
|
+
ctx.focusVisible = false;
|
|
273
282
|
}
|
|
274
283
|
},
|
|
275
|
-
setChecked(ctx
|
|
276
|
-
set.checked(ctx
|
|
284
|
+
setChecked(ctx, evt) {
|
|
285
|
+
set.checked(ctx, evt.checked);
|
|
277
286
|
},
|
|
278
|
-
toggleChecked(ctx
|
|
279
|
-
const checked = isIndeterminate(ctx
|
|
280
|
-
set.checked(ctx
|
|
287
|
+
toggleChecked(ctx) {
|
|
288
|
+
const checked = isIndeterminate(ctx.checked) ? true : !ctx.checked;
|
|
289
|
+
set.checked(ctx, checked);
|
|
281
290
|
},
|
|
282
|
-
dispatchChangeEvent(ctx
|
|
283
|
-
|
|
291
|
+
dispatchChangeEvent(ctx) {
|
|
292
|
+
const inputEl = dom.getHiddenInputEl(ctx);
|
|
293
|
+
if (inputEl) inputEl.indeterminate = ctx.isIndeterminate;
|
|
294
|
+
dispatchInputCheckedEvent(inputEl, { checked: isChecked(ctx.checked) });
|
|
284
295
|
}
|
|
285
296
|
}
|
|
286
297
|
});
|
|
287
298
|
}
|
|
288
|
-
|
|
289
299
|
//#endregion
|
|
290
300
|
//#region src/props.ts
|
|
291
301
|
const props = createProps()([
|
|
@@ -305,6 +315,5 @@ const props = createProps()([
|
|
|
305
315
|
"value"
|
|
306
316
|
]);
|
|
307
317
|
const splitProps = createSplitProps(props);
|
|
308
|
-
|
|
309
318
|
//#endregion
|
|
310
|
-
export { anatomy, connect, machine, props, splitProps };
|
|
319
|
+
export { anatomy, connect, machine, props, splitProps };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@destyler/checkbox",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.12",
|
|
5
5
|
"description": "a control that allows the user to toggle between checked and not checked.",
|
|
6
6
|
"author": "Elone Hoo <elonehoo@gmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -22,12 +22,12 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@destyler/
|
|
26
|
-
"@destyler/
|
|
27
|
-
"@destyler/
|
|
28
|
-
"@destyler/types": "0.2.
|
|
29
|
-
"@destyler/
|
|
30
|
-
"@destyler/
|
|
25
|
+
"@destyler/anatomy": "0.2.12",
|
|
26
|
+
"@destyler/dom": "0.2.12",
|
|
27
|
+
"@destyler/focus-visible": "0.2.12",
|
|
28
|
+
"@destyler/types": "0.2.12",
|
|
29
|
+
"@destyler/utils": "0.2.12",
|
|
30
|
+
"@destyler/xstate": "0.2.12"
|
|
31
31
|
},
|
|
32
32
|
"publishConfig": {
|
|
33
33
|
"access": "public"
|