@destyler/radio 0.0.2 → 0.0.4

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.
@@ -1,64 +1,93 @@
1
- import { defineComponent, toRefs, h, computed, withDirectives, mergeProps, withModifiers, ref } from 'vue';
2
- import { DestylerPrimitive } from '@destyler/primitive';
1
+ import { defineComponent, toRefs, h, computed, mergeProps, withModifiers, ref, withKeys } from 'vue';
2
+ import { primitiveProps, Primitive } from '@destyler/primitive';
3
3
  import { createContext } from '@destyler/shared';
4
4
  import { useVModel, useDirection, useForwardExpose, useFormControl } from '@destyler/composition';
5
- import { DestylerRovingFocusGroup, DestylerRovingFocusItem } from '@destyler/roving-focus';
5
+ import { RovingFocusGroup, RovingFocusItem } from '@destyler/roving-focus';
6
6
  import { useEventListener } from '@vueuse/core';
7
- import { BindOnceDirective } from '@destyler/directives';
7
+ import { Presence } from '@destyler/presence';
8
8
 
9
- const destylerRadioGroupRootProps = {
10
- as: {
11
- type: [String, Object],
12
- required: false,
13
- default: "div"
14
- },
15
- asChild: {
16
- type: Boolean,
17
- required: false,
18
- default: false
19
- },
9
+ const radioGroupRootProps = {
10
+ ...primitiveProps,
11
+ /**
12
+ * The controlled value of the radio item to check. Can be binded as `v-model`.
13
+ */
20
14
  modelValue: {
21
15
  type: String,
22
16
  required: false
23
17
  },
18
+ /**
19
+ * The value of the radio item that should be checked when initially rendered.
20
+ *
21
+ * Use when you do not need to control the state of the radio items.
22
+ */
24
23
  defaultValue: {
25
24
  type: String,
26
25
  required: false
27
26
  },
27
+ /**
28
+ * When `true`, prevents the user from interacting with radio items.
29
+ */
28
30
  disabled: {
29
31
  type: Boolean,
30
32
  required: false,
31
33
  default: false
32
34
  },
35
+ /**
36
+ * The name of the group. Submitted with its owning form as part of a name/value pair.
37
+ */
33
38
  name: {
34
39
  type: String,
35
40
  required: false
36
41
  },
42
+ /**
43
+ * When `true`, indicates that the user must check a radio item before the owning form can be submitted.
44
+ *
45
+ * @default false
46
+ */
37
47
  required: {
38
48
  type: Boolean,
39
49
  required: false,
40
50
  default: false
41
51
  },
52
+ /**
53
+ * The orientation of the component.
54
+ *
55
+ * @default undefined
56
+ */
42
57
  orientation: {
43
58
  type: String,
44
59
  required: false,
45
60
  default: void 0
46
61
  },
62
+ /**
63
+ * The reading direction of the radio when applicable.
64
+ */
47
65
  dir: {
48
66
  type: String,
49
67
  required: false
50
68
  },
69
+ /**
70
+ * When `true`, keyboard navigation will loop from last item to first, and vice versa.
71
+ *
72
+ * @default true
73
+ */
51
74
  loop: {
52
75
  type: Boolean,
53
76
  required: false,
54
77
  default: true
55
78
  }
56
79
  };
80
+ const radioGroupRootEmits = {
81
+ /**
82
+ * Event handler called when the radio group value changes
83
+ */
84
+ "update:modelValue": (_value) => true
85
+ };
57
86
  const [injectRadioGroupRootContext, provideRadioGroupRootContext] = createContext("DestylerRadioGroupRoot");
58
- const DestylerRadioGroupRoot = defineComponent({
87
+ const RadioGroupRoot = defineComponent({
59
88
  name: "DestylerRadioGroupRoot",
60
- props: destylerRadioGroupRootProps,
61
- emits: ["update:modelValue"],
89
+ props: radioGroupRootProps,
90
+ emits: radioGroupRootEmits,
62
91
  setup(props, { emit }) {
63
92
  const modelValue = useVModel(props, "modelValue", emit, {
64
93
  defaultValue: props.defaultValue,
@@ -87,66 +116,88 @@ const DestylerRadioGroupRoot = defineComponent({
87
116
  };
88
117
  },
89
118
  render() {
90
- return h(DestylerRovingFocusGroup, {
119
+ return h(RovingFocusGroup, {
91
120
  asChild: true,
92
121
  orientation: this.orientation,
93
122
  dir: this.dir,
94
123
  loop: this.loop
95
- }, h(DestylerPrimitive, {
124
+ }, () => h(Primitive, {
96
125
  "role": "radiogroup",
97
126
  "data-disabled": this.disabled ? "" : void 0,
98
127
  "asChild": this.$props.asChild,
99
128
  "aria-required": this.required,
100
129
  "dir": this.dir,
101
130
  "name": this.name
102
- }, {
103
- default: () => this.$slots.default?.()
104
- }));
131
+ }, () => this.$slots.default?.()));
105
132
  }
106
133
  });
107
134
 
108
- const destylerRadioProps = {
135
+ const radioProps = {
136
+ ...primitiveProps,
137
+ /**
138
+ * @default button
139
+ */
109
140
  as: {
110
- type: [String, Object],
111
- required: false,
141
+ ...primitiveProps.as,
112
142
  default: "button"
113
143
  },
114
- asChild: {
115
- type: Boolean,
116
- required: false,
117
- default: false
118
- },
144
+ /**
145
+ * The name of the input fields in the radio (Useful for form submission).
146
+ */
119
147
  id: {
120
148
  type: String,
121
149
  required: false
122
150
  },
151
+ /**
152
+ * The value given as data when submitted with a `name`.
153
+ */
123
154
  value: {
124
155
  type: String,
125
156
  required: false
126
157
  },
158
+ /**
159
+ * When `true`, prevents the user from interacting with the radio item.
160
+ */
127
161
  disabled: {
128
162
  type: Boolean,
129
163
  required: false,
130
164
  default: false
131
165
  },
166
+ /**
167
+ * When `true`, indicates that the user must check the radio item before the owning form can be submitted.
168
+ */
132
169
  required: {
133
170
  type: Boolean,
134
171
  required: false
135
172
  },
173
+ /**
174
+ * The controlled value of the radio item to check. Can be binded as `v-model`.
175
+ *
176
+ * @default undefined
177
+ */
136
178
  checked: {
137
179
  type: Boolean,
138
180
  required: false,
139
181
  default: void 0
140
182
  },
183
+ /**
184
+ * The name of the input fields in the radio (Useful for form submission).
185
+ */
141
186
  name: {
142
187
  type: String,
143
188
  required: false
144
189
  }
145
190
  };
146
- const DestylerRadio = defineComponent({
191
+ const radioEmits = {
192
+ /**
193
+ * Event handler called when the radio item is checked.
194
+ */
195
+ "update:checked": (_value) => true
196
+ };
197
+ const Radio = defineComponent({
147
198
  name: "DestylerRadio",
148
- props: destylerRadioProps,
149
- emits: ["update:checked"],
199
+ props: radioProps,
200
+ emits: radioEmits,
150
201
  setup(props, { emit }) {
151
202
  const checkedRef = useVModel(props, "checked", emit, {
152
203
  passive: props.checked === void 0
@@ -170,9 +221,10 @@ const DestylerRadio = defineComponent({
170
221
  };
171
222
  },
172
223
  render() {
173
- return withDirectives(h(DestylerPrimitive, mergeProps(this.$attrs, {
224
+ return h(Primitive, mergeProps(this.$attrs, {
225
+ "id": this.$props.id,
174
226
  "role": "radio",
175
- "ref": (el) => this.forwardRef(el),
227
+ "ref": this.forwardRef,
176
228
  "type": this.$props.as === "button" ? "button" : void 0,
177
229
  "as": this.$props.as,
178
230
  "aria-checked": this.checkedRef,
@@ -187,15 +239,18 @@ const DestylerRadio = defineComponent({
187
239
  "onClick": withModifiers((event) => {
188
240
  this.handleClick(event);
189
241
  }, ["stop"])
190
- }), [
242
+ }), () => [
191
243
  this.$slots.default?.(),
192
244
  this.isFormControl ? h("input", {
193
- role: "radio",
194
- checked: this.checkedRef,
195
- name: this.$props.name,
196
- disabled: this.disabled,
197
- required: this.required,
198
- style: {
245
+ "type": "radio",
246
+ "tabindex": -1,
247
+ "aria-hidden": "",
248
+ "value": this.value,
249
+ "checked": this.checkedRef,
250
+ "name": this.$props.name,
251
+ "disabled": this.disabled,
252
+ "required": this.required,
253
+ "style": {
199
254
  transform: "translateX(-100%)",
200
255
  position: "absolute",
201
256
  pointerEvents: "none",
@@ -203,50 +258,53 @@ const DestylerRadio = defineComponent({
203
258
  margin: 0
204
259
  }
205
260
  }) : null
206
- ]), [
207
- [BindOnceDirective, { id: this.$props.id }]
208
261
  ]);
209
262
  }
210
263
  });
211
264
 
212
- const destylerRadioGroupItemProps = {
213
- as: {
214
- type: [String, Object],
215
- required: false,
216
- default: "button"
217
- },
265
+ const radioGroupItemProps = {
218
266
  asChild: {
219
- type: Boolean,
220
- required: false,
221
- default: false
267
+ ...radioProps.asChild
268
+ },
269
+ as: {
270
+ ...radioProps.as
222
271
  },
272
+ /**
273
+ * The name of the input fields in the radio (Useful for form submission).
274
+ */
223
275
  id: {
224
- type: String,
225
- required: false
276
+ ...radioProps.id
226
277
  },
278
+ /**
279
+ * The value given as data when submitted with a `name`.
280
+ */
227
281
  value: {
228
- type: String,
229
- required: false
282
+ ...radioProps.value
230
283
  },
284
+ /**
285
+ * When `true`, prevents the user from interacting with the radio item.
286
+ */
231
287
  disabled: {
232
- type: Boolean,
233
- required: false,
234
- default: false
288
+ ...radioProps.disabled
235
289
  },
290
+ /**
291
+ * When `true`, indicates that the user must check the radio item before the owning form can be submitted.
292
+ */
236
293
  required: {
237
- type: Boolean,
238
- required: false
294
+ ...radioProps.required
239
295
  },
296
+ /**
297
+ * The name of the input fields in the radio (Useful for form submission).
298
+ */
240
299
  name: {
241
- type: String,
242
- required: false
300
+ ...radioProps.name
243
301
  }
244
302
  };
245
- const [injectRadioGroupItemContext, provideRadiogroupItemContext] = createContext("DestylerRadioGroupItem");
246
- const DestylerRadioGroupItem = defineComponent({
247
- name: "DestylerRadioGroupItem",
303
+ const [injectRadioGroupItemContext, provideRadiogroupItemContext] = createContext("RadioGroupItem");
304
+ const RadioGroupItem = defineComponent({
305
+ name: "RadioGroupItem",
248
306
  inheritAttrs: false,
249
- props: destylerRadioGroupItemProps,
307
+ props: radioGroupItemProps,
250
308
  setup(props) {
251
309
  const { forwardRef, currentElement } = useForwardExpose();
252
310
  const rootContext = injectRadioGroupRootContext();
@@ -279,59 +337,69 @@ const DestylerRadioGroupItem = defineComponent({
279
337
  };
280
338
  },
281
339
  render() {
282
- return h(DestylerRovingFocusItem, {
340
+ return h(RovingFocusItem, {
283
341
  asChild: true,
284
342
  checked: this.checked,
285
343
  disabled: this.disabled,
286
344
  focusable: !this.disabled,
287
345
  active: this.checked
288
- }, h(DestylerRadio, mergeProps(this.$attrs, this.$props, {
346
+ }, () => h(Radio, mergeProps(this.$attrs, this.$props, {
289
347
  "ref": (el) => this.forwardRef(el),
290
348
  "checked": this.checked,
291
349
  "required": this.required,
292
350
  "onUpdate:checked": () => {
293
351
  this.rootContext.changeModelValue(this.$props.value);
294
352
  },
353
+ "onKeydown": withKeys((event) => {
354
+ event.preventDefault();
355
+ }, ["enter"]),
295
356
  "onFocus": () => {
296
357
  this.handleFocus();
297
358
  }
298
- }), {
299
- default: () => this.$slots.default?.()
300
- }));
359
+ }), () => this.$slots.default?.()));
301
360
  }
302
361
  });
303
362
 
304
- const destylerRadioGroupIndicatorProps = {
363
+ const radioGroupIndicatorProps = {
364
+ ...primitiveProps,
365
+ /**
366
+ * @default span
367
+ */
305
368
  as: {
306
- type: [String, Object],
307
- required: false,
369
+ ...primitiveProps.as,
308
370
  default: "span"
309
371
  },
310
- asChild: {
372
+ /**
373
+ * Used to force mounting when more control is needed. Useful when
374
+ * controlling animation with Vue animation libraries.
375
+ */
376
+ forceMount: {
311
377
  type: Boolean,
312
- required: false,
313
- default: false
378
+ required: false
314
379
  }
315
380
  };
316
- const DestylerRadioGroupIndicator = defineComponent({
381
+ const RadioGroupIndicator = defineComponent({
317
382
  name: "DestylerRadioGroupindicator",
318
- props: destylerRadioGroupIndicatorProps,
383
+ props: radioGroupIndicatorProps,
319
384
  setup() {
320
385
  const itemContext = injectRadioGroupItemContext();
386
+ const { forwardRef } = useForwardExpose();
321
387
  return {
388
+ forwardRef,
322
389
  itemContext
323
390
  };
324
391
  },
325
392
  render() {
326
- return this.itemContext.checked.value ? h(DestylerPrimitive, {
393
+ return h(Presence, {
394
+ present: this.$props.forceMount || this.itemContext.checked.value
395
+ }, () => h(Primitive, mergeProps(this.$attrs, {
396
+ "ref": (el) => this.forwardRef(el),
327
397
  "data-state": this.itemContext.checked.value ? "checked" : "unchecked",
328
398
  "data-disabled": this.itemContext.disabled.value ? "" : void 0,
329
- "as": this.$props.as,
330
- "asChild": this.$props.asChild
331
- }, {
332
- default: () => this.$slots.default?.()
333
- }) : null;
399
+ "asChild": this.$props.asChild,
400
+ "as": this.$props.as
401
+ }), () => this.$slots.default?.()));
334
402
  }
335
403
  });
336
404
 
337
- export { DestylerRadio, DestylerRadioGroupIndicator, DestylerRadioGroupItem, DestylerRadioGroupRoot };
405
+ export { Radio, RadioGroupIndicator, RadioGroupItem, RadioGroupRoot, radioEmits, radioGroupIndicatorProps, radioGroupItemProps, radioGroupRootEmits, radioGroupRootProps, radioProps };
package/dist/index.cjs CHANGED
@@ -7,10 +7,13 @@ require('@destyler/shared');
7
7
  require('@destyler/composition');
8
8
  require('@destyler/roving-focus');
9
9
  require('@vueuse/core');
10
- require('@destyler/directives');
10
+ require('@destyler/presence');
11
11
 
12
12
 
13
13
 
14
- exports.DestylerRadioGroupIndicator = index.DestylerRadioGroupIndicator;
15
- exports.DestylerRadioGroupItem = index.DestylerRadioGroupItem;
16
- exports.DestylerRadioGroupRoot = index.DestylerRadioGroupRoot;
14
+ exports.RadioGroupIndicator = index.RadioGroupIndicator;
15
+ exports.RadioGroupItem = index.RadioGroupItem;
16
+ exports.RadioGroupRoot = index.RadioGroupRoot;
17
+ exports.radioGroupIndicatorProps = index.radioGroupIndicatorProps;
18
+ exports.radioGroupItemProps = index.radioGroupItemProps;
19
+ exports.radioGroupRootProps = index.radioGroupRootProps;